summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/rpc/xdr_float.c
blob: d28f7679b6361df3f6815fee80a93f11cdd3d2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
 * Portions of this source code were derived from Berkeley
 * 4.3 BSD under license from the Regents of the University of
 * California.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Generic XDR routines impelmentation.
 *
 * These are the "floating point" xdr routines used to (de)serialize
 * most common data items.  See xdr.h for more info on the interface to
 * xdr.
 */

#include "mt.h"
#include <sys/types.h>
#include <stdio.h>
#include <rpc/types.h>
#include <rpc/xdr.h>

/*
 * This routine works on Suns, 3b2, 68000s, 386 and Vaxen in a manner
 * which is very efficient as bit twiddling is all that is needed.  All
 * other machines can use this code but the code is inefficient as
 * various mathematical operations are used to generate the ieee format.
 * In addition rounding errors may occur due to the calculations involved.
 * To be most efficient, new machines should have their own ifdefs.
 * The encoding routines will fail if the machines try to encode a
 * float/double whose value can not be represented by the ieee format,
 * e.g. the exponent is too big/small.
 *	ieee largest  float  = (2 ^ 128)  * 0x1.fffff
 *	ieee smallest float  = (2 ^ -127) * 0x1.00000
 *	ieee largest  double = (2 ^ 1024)  * 0x1.fffff
 *	ieee smallest double = (2 ^ -1023) * 0x1.00000
 * The decoding routines assumes that the receiving machine can handle
 * floats/doubles as large/small as the values stated above.  If you
 * use a machine which can not represent these values, you will need
 * to put ifdefs in the decode sections to identify areas of failure.
 */

#if defined(vax)

/*
 * What IEEE single precision floating point looks like this on a
 * vax.
 */

struct	ieee_single {
	unsigned int	mantissa: 23;
	unsigned int	exp	: 8;
	unsigned int	sign    : 1;
};

#define	IEEE_SNG_BIAS	0x7f
#define	VAX_SNG_BIAS    0x81


/* Vax single precision floating point */
struct	vax_single {
	unsigned int	mantissa1 : 7;
	unsigned int	exp	: 8;
	unsigned int	sign	: 1;
	unsigned int	mantissa2 : 16;
};

#define	VAX_SNG_BIAS	0x81

static struct sgl_limits {
	struct vax_single s;
	struct ieee_single ieee;
} sgl_limits[2] = {
	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
};
#endif /* vax */

bool_t
xdr_float(XDR *xdrs, float *fp)
{
#if defined(vax)
	struct ieee_single is;
	struct vax_single vs, *vsp;
	struct sgl_limits *lim;
	size_t i;
#endif

	switch (xdrs->x_op) {

	case XDR_ENCODE:
#if defined(mc68000) || defined(sparc) || defined(u3b2) || \
	defined(u3b15) || defined(i386)
		return (XDR_PUTINT32(xdrs, (int *)fp));
#else
#if defined(vax)
		vs = *((struct vax_single *)fp);
		if ((vs.exp == 1) || (vs.exp == 2)) {
			/* map these to subnormals */
			is.exp = 0;
			is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
			/* lose some precision */
			is.mantissa >>= 3 - vs.exp;
			is.mantissa += (1 << (20 + vs.exp));
			goto shipit;
		}
		for (i = 0, lim = sgl_limits;
			i < (int)(sizeof (sgl_limits) /
					sizeof (struct sgl_limits));
			i++, lim++) {
			if ((vs.mantissa2 == lim->s.mantissa2) &&
				(vs.exp == lim->s.exp) &&
				(vs.mantissa1 == lim->s.mantissa1)) {
				is = lim->ieee;
				goto shipit;
			}
		}
		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
	shipit:
		is.sign = vs.sign;
		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
#else
		{
		/*
		 * Every machine can do this, its just not very efficient.
		 * In addtion, some rounding errors may occur do to the
		 * calculations involved.
		 */
		float f;
		int neg = 0;
		int exp = 0;
		int32_t val;

		f = *fp;
		if (f == 0) {
			val = 0;
			return (XDR_PUTINT32(xdrs, &val));
		}
		if (f < 0) {
			f = 0 - f;
			neg = 1;
		}
		while (f < 1) {
			f = f * 2;
			--exp;
		}
		while (f >= 2) {
			f = f/2;
			++exp;
		}
		if ((exp > 128) || (exp < -127)) {
			/* over or under flowing ieee exponent */
			return (FALSE);
		}
		val = neg;
		val = val << 8;		/* for the exponent */
		val += 127 + exp;	/* 127 is the bias */
		val = val << 23;	/* for the mantissa */
		val += (int32_t)((f - 1) * 8388608);	/* 2 ^ 23 */
		return (XDR_PUTINT32(xdrs, &val));
		}
#endif
#endif

	case XDR_DECODE:
#if defined(mc68000) || defined(sparc) || defined(u3b2) || \
	defined(u3b15) || defined(i386)
		return (XDR_GETINT32(xdrs, (int *)fp));
#else
#if defined(vax)
		vsp = (struct vax_single *)fp;
		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
			return (FALSE);

		for (i = 0, lim = sgl_limits;
			i < (int)(sizeof (sgl_limits) /
					sizeof (struct sgl_limits));
			i++, lim++) {
			if ((is.exp == lim->ieee.exp) &&
				(is.mantissa == lim->ieee.mantissa)) {
				*vsp = lim->s;
				goto doneit;
			} else if ((is.exp == 0) && (lim->ieee.exp == 0)) {
			    /* Special Case */
			    unsigned tmp = is.mantissa >> 20;
			    if (tmp >= 4) {
			    vsp->exp = 2;
			    } else if (tmp >= 2) {
			    vsp->exp = 1;
			    } else {
				*vsp = min.s;
				break;
			    }	/* else */
			    tmp = is.mantissa - (1 << (20 + vsp->exp));
			    tmp <<= 3 - vsp->exp;
			    vsp->mantissa2 = tmp;
			    vsp->mantissa1 = (tmp >> 16);
			    goto doneit;
		    }
		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
		vsp->mantissa2 = is.mantissa;
		vsp->mantissa1 = (is.mantissa >> 16);
	doneit:
		vsp->sign = is.sign;
		return (TRUE);
#else
		{
		/*
		 * Every machine can do this, its just not very
		 * efficient.  It assumes that the decoding machine's
		 * float can represent any value in the range of
		 *	ieee largest  float  = (2 ^ 128)  * 0x1.fffff
		 *	to
		 *	ieee smallest float  = (2 ^ -127) * 0x1.00000
		 * In addtion, some rounding errors may occur do to the
		 * calculations involved.
		 */
		float f;
		int neg = 0;
		int exp = 0;
		int32_t val;

		if (!XDR_GETINT32(xdrs, (int32_t *)&val))
			return (FALSE);
		neg = val & 0x80000000;
		exp = (val & 0x7f800000) >> 23;
		exp -= 127;		/* subtract exponent base */
		f = (val & 0x007fffff) * 0.00000011920928955078125;
		/* 2 ^ -23 */
		f++;
		while (exp != 0) {
			if (exp < 0) {
				f = f/2.0;
				++exp;
			} else {
				f = f * 2.0;
				--exp;
			}
		}
		if (neg)
			f = 0 - f;
		*fp = f;
		}
		return (TRUE);
#endif
#endif

	case XDR_FREE:
		return (TRUE);
	}
	return (FALSE);
}

/*
 * This routine works on Suns (Sky / 68000's) and Vaxen.
 */

#if defined(vax)
/* What IEEE double precision floating point looks like on a Vax */
struct	ieee_double {
	unsigned int	mantissa1 : 20;
	unsigned int	exp	  : 11;
	unsigned int	sign	  : 1;
	unsigned int	mantissa2 : 32;
};

/* Vax double precision floating point */
struct  vax_double {
	unsigned int	mantissa1 : 7;
	unsigned int	exp	  : 8;
	unsigned int	sign	  : 1;
	unsigned int	mantissa2 : 16;
	unsigned int	mantissa3 : 16;
	unsigned int	mantissa4 : 16;
};

#define	VAX_DBL_BIAS	0x81
#define	IEEE_DBL_BIAS	0x3ff
#define	MASK(nbits)	((1 << nbits) - 1)

static struct dbl_limits {
	struct	vax_double d;
	struct	ieee_double ieee;
} dbl_limits[2] = {
	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
};

#endif /* vax */


bool_t
xdr_double(XDR *xdrs, double *dp)
{
	int *lp;
#if defined(vax)
	struct	ieee_double id;
	struct	vax_double vd;
	struct dbl_limits *lim;
	size_t i;
#endif

	switch (xdrs->x_op) {

	case XDR_ENCODE:
#if defined(mc68000) || defined(u3b2) || defined(u3b15) || \
	defined(_LONG_LONG_HTOL)
		lp = (int *)dp;
		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
#else
#if defined(_LONG_LONG_LTOH)
		lp = (int *)dp;
		lp++;
		return (XDR_PUTINT32(xdrs, lp--) && XDR_PUTINT32(xdrs, lp));
#else
#if defined(vax)
		vd = *((struct vax_double *)dp);
		for (i = 0, lim = dbl_limits;
			i < (int)(sizeof (dbl_limits) /
					sizeof (struct dbl_limits));
			i++, lim++) {
			if ((vd.mantissa4 == lim->d.mantissa4) &&
				(vd.mantissa3 == lim->d.mantissa3) &&
				(vd.mantissa2 == lim->d.mantissa2) &&
				(vd.mantissa1 == lim->d.mantissa1) &&
				(vd.exp == lim->d.exp)) {
				id = lim->ieee;
				goto shipit;
			}
		}
		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
				(vd.mantissa3 << 13) |
				((vd.mantissa4 >> 3) & MASK(13));
	shipit:
		id.sign = vd.sign;
		lp = (int32_t *)&id;
#else
		{
		/*
		 * Every machine can do this, its just not very efficient.
		 * In addtion, some rounding errors may occur do to the
		 * calculations involved.
		 */
		double d;
		int neg = 0;
		int exp = 0;
		int32_t val[2];

		d = *dp;
		if (d == 0) {
			val[0] = 0;
			val[1] = 0;
			lp = val;
			return (XDR_PUTINT32(xdrs, lp++) &&
				XDR_PUTINT32(xdrs, lp));
		}
		if (d < 0) {
			d = 0 - d;
			neg = 1;
		}
		while (d < 1) {
			d = d * 2;
			--exp;
		}
		while (d >= 2) {
			d = d/2;
			++exp;
		}
		if ((exp > 1024) || (exp < -1023)) {
			/* over or under flowing ieee exponent */
			return (FALSE);
		}
		val[0] = neg;
		val[0] = val[0] << 11;	/* for the exponent */
		val[0] += 1023 + exp;	/* 1023 is the bias */
		val[0] = val[0] << 20;	/* for the mantissa */
		val[0] += (int32_t)((d - 1) * 1048576);	/* 2 ^ 20 */
		val[1] += (int32_t)((((d - 1) * 1048576) - val[0])
							* 4294967296);
		/* 2 ^ 32 */
		lp = val;
		}
#endif
		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
#endif
#endif

	case XDR_DECODE:
#if defined(mc68000) || defined(u3b2) || defined(u3b15) || \
	defined(_LONG_LONG_HTOL)
		lp = (int *)dp;
		return (XDR_GETINT32(xdrs, lp++) && XDR_GETINT32(xdrs, lp));
#else
#if defined(_LONG_LONG_LTOH)
		lp = (int *)dp;
		lp++;
		return (XDR_GETINT32(xdrs, lp--) && XDR_GETINT32(xdrs, lp));
#else
#if defined(vax)
		lp = (int32_t *)&id;
		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
			return (FALSE);
		for (i = 0, lim = dbl_limits;
			i < sizeof (dbl_limits)/sizeof (struct dbl_limits);
			i++, lim++) {
			if ((id.mantissa2 == lim->ieee.mantissa2) &&
				(id.mantissa1 == lim->ieee.mantissa1) &&
				(id.exp == lim->ieee.exp)) {
				vd = lim->d;
				goto doneit;
			}
		}
		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
		vd.mantissa1 = (id.mantissa1 >> 13);
		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
				(id.mantissa2 >> 29);
		vd.mantissa3 = (id.mantissa2 >> 13);
		vd.mantissa4 = (id.mantissa2 << 3);
	doneit:
		vd.sign = id.sign;
		*dp = *((double *)&vd);
		return (TRUE);
#else
		{
		/*
		 * Every machine can do this, its just not very
		 * efficient.  It assumes that the decoding machine's
		 * double can represent any value in the range of
		 *	ieee largest  double  = (2 ^ 1024)  * 0x1.fffffffffffff
		 *	to
		 *	ieee smallest double  = (2 ^ -1023) * 0x1.0000000000000
		 * In addtion, some rounding errors may occur do to the
		 * calculations involved.
		 */
		double d;
		int neg = 0;
		int exp = 0;
		int32_t val[2];

		lp = val;
		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
			return (FALSE);
		neg = val[0] & 0x80000000;
		exp = (val[0] & 0x7ff00000) >> 20;
		exp -= 1023;		/* subtract exponent base */
		d = (val[0] & 0x000fffff) * 0.00000095367431640625;
		/* 2 ^ -20 */
		d += (val[1] * 0.0000000000000002220446049250313);
		/* 2 ^ -52 */
		d++;
		while (exp != 0) {
			if (exp < 0) {
				d = d/2.0;
				++exp;
			} else {
				d = d * 2.0;
				--exp;
			}
		}
		if (neg)
			d = 0 - d;
		*dp = d;
		}
#endif
#endif
#endif

	case XDR_FREE:
		return (TRUE);
	}
	return (FALSE);
}

/* ARGSUSED */
bool_t
xdr_quadruple(XDR *xdrs, long double *fp)
{
/*
 * The Sparc uses IEEE FP encoding, so just do a byte copy
 */

#if !defined(sparc)
	return (FALSE);
#else
	switch (xdrs->x_op) {
	case XDR_ENCODE:
		return (XDR_PUTBYTES(xdrs, (char *)fp, sizeof (long double)));
	case XDR_DECODE:
		return (XDR_GETBYTES(xdrs, (char *)fp, sizeof (long double)));
	case XDR_FREE:
		return (TRUE);
	}
	return (FALSE);
#endif
}