xref: /titanic_52/usr/src/lib/libnsl/rpc/xdr_float.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * xdr_float.c, Generic XDR routines impelmentation.
39  *
40  * These are the "floating point" xdr routines used to (de)serialize
41  * most common data items.  See xdr.h for more info on the interface to
42  * xdr.
43  */
44 
45 #include <sys/types.h>
46 #include <stdio.h>
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
49 
50 /*
51  * This routine works on Suns, 3b2, 68000s, 386 and Vaxen in a manner
52  * which is very efficient as bit twiddling is all that is needed.  All
53  * other machines can use this code but the code is inefficient as
54  * various mathematical operations are used to generate the ieee format.
55  * In addition rounding errors may occur due to the calculations involved.
56  * To be most efficient, new machines should have their own ifdefs.
57  * The encoding routines will fail if the machines try to encode a
58  * float/double whose value can not be represented by the ieee format,
59  * e.g. the exponent is too big/small.
60  *	ieee largest  float  = (2 ^ 128)  * 0x1.fffff
61  *	ieee smallest float  = (2 ^ -127) * 0x1.00000
62  *	ieee largest  double = (2 ^ 1024)  * 0x1.fffff
63  *	ieee smallest double = (2 ^ -1023) * 0x1.00000
64  * The decoding routines assumes that the receiving machine can handle
65  * floats/doubles as large/small as the values stated above.  If you
66  * use a machine which can not represent these values, you will need
67  * to put ifdefs in the decode sections to identify areas of failure.
68  */
69 
70 #if defined(vax)
71 
72 /*
73  * What IEEE single precision floating point looks like this on a
74  * vax.
75  */
76 
77 struct	ieee_single {
78 	unsigned int	mantissa: 23;
79 	unsigned int	exp	: 8;
80 	unsigned int	sign    : 1;
81 };
82 
83 #define	IEEE_SNG_BIAS	0x7f
84 #define	VAX_SNG_BIAS    0x81
85 
86 
87 /* Vax single precision floating point */
88 struct	vax_single {
89 	unsigned int	mantissa1 : 7;
90 	unsigned int	exp	: 8;
91 	unsigned int	sign	: 1;
92 	unsigned int	mantissa2 : 16;
93 };
94 
95 #define	VAX_SNG_BIAS	0x81
96 
97 static struct sgl_limits {
98 	struct vax_single s;
99 	struct ieee_single ieee;
100 } sgl_limits[2] = {
101 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
102 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
103 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
104 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
105 };
106 #endif /* vax */
107 
108 bool_t
109 xdr_float(XDR *xdrs, float *fp)
110 {
111 #if defined(vax)
112 	struct ieee_single is;
113 	struct vax_single vs, *vsp;
114 	struct sgl_limits *lim;
115 	size_t i;
116 #endif
117 
118 	switch (xdrs->x_op) {
119 
120 	case XDR_ENCODE:
121 #if defined(mc68000) || defined(sparc) || defined(u3b2) || \
122 	defined(u3b15) || defined(i386)
123 		return (XDR_PUTINT32(xdrs, (int *)fp));
124 #else
125 #if defined(vax)
126 		vs = *((struct vax_single *)fp);
127 		if ((vs.exp == 1) || (vs.exp == 2)) {
128 			/* map these to subnormals */
129 			is.exp = 0;
130 			is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
131 			/* lose some precision */
132 			is.mantissa >>= 3 - vs.exp;
133 			is.mantissa += (1 << (20 + vs.exp));
134 			goto shipit;
135 		}
136 		for (i = 0, lim = sgl_limits;
137 			i < (int)(sizeof (sgl_limits) /
138 					sizeof (struct sgl_limits));
139 			i++, lim++) {
140 			if ((vs.mantissa2 == lim->s.mantissa2) &&
141 				(vs.exp == lim->s.exp) &&
142 				(vs.mantissa1 == lim->s.mantissa1)) {
143 				is = lim->ieee;
144 				goto shipit;
145 			}
146 		}
147 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
148 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
149 	shipit:
150 		is.sign = vs.sign;
151 		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
152 #else
153 		{
154 		/*
155 		 * Every machine can do this, its just not very efficient.
156 		 * In addtion, some rounding errors may occur do to the
157 		 * calculations involved.
158 		 */
159 		float f;
160 		int neg = 0;
161 		int exp = 0;
162 		int32_t val;
163 
164 		f = *fp;
165 		if (f == 0) {
166 			val = 0;
167 			return (XDR_PUTINT32(xdrs, &val));
168 		}
169 		if (f < 0) {
170 			f = 0 - f;
171 			neg = 1;
172 		}
173 		while (f < 1) {
174 			f = f * 2;
175 			--exp;
176 		}
177 		while (f >= 2) {
178 			f = f/2;
179 			++exp;
180 		}
181 		if ((exp > 128) || (exp < -127)) {
182 			/* over or under flowing ieee exponent */
183 			return (FALSE);
184 		}
185 		val = neg;
186 		val = val << 8;		/* for the exponent */
187 		val += 127 + exp;	/* 127 is the bias */
188 		val = val << 23;	/* for the mantissa */
189 		val += (int32_t)((f - 1) * 8388608);	/* 2 ^ 23 */
190 		return (XDR_PUTINT32(xdrs, &val));
191 		}
192 #endif
193 #endif
194 
195 	case XDR_DECODE:
196 #if defined(mc68000) || defined(sparc) || defined(u3b2) || \
197 	defined(u3b15) || defined(i386)
198 		return (XDR_GETINT32(xdrs, (int *)fp));
199 #else
200 #if defined(vax)
201 		vsp = (struct vax_single *)fp;
202 		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
203 			return (FALSE);
204 
205 		for (i = 0, lim = sgl_limits;
206 			i < (int)(sizeof (sgl_limits) /
207 					sizeof (struct sgl_limits));
208 			i++, lim++) {
209 			if ((is.exp == lim->ieee.exp) &&
210 				(is.mantissa == lim->ieee.mantissa)) {
211 				*vsp = lim->s;
212 				goto doneit;
213 			} else if ((is.exp == 0) && (lim->ieee.exp == 0)) {
214 			    /* Special Case */
215 			    unsigned tmp = is.mantissa >> 20;
216 			    if (tmp >= 4) {
217 			    vsp->exp = 2;
218 			    } else if (tmp >= 2) {
219 			    vsp->exp = 1;
220 			    } else {
221 				*vsp = min.s;
222 				break;
223 			    }	/* else */
224 			    tmp = is.mantissa - (1 << (20 + vsp->exp));
225 			    tmp <<= 3 - vsp->exp;
226 			    vsp->mantissa2 = tmp;
227 			    vsp->mantissa1 = (tmp >> 16);
228 			    goto doneit;
229 		    }
230 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
231 		vsp->mantissa2 = is.mantissa;
232 		vsp->mantissa1 = (is.mantissa >> 16);
233 	doneit:
234 		vsp->sign = is.sign;
235 		return (TRUE);
236 #else
237 		{
238 		/*
239 		 * Every machine can do this, its just not very
240 		 * efficient.  It assumes that the decoding machine's
241 		 * float can represent any value in the range of
242 		 *	ieee largest  float  = (2 ^ 128)  * 0x1.fffff
243 		 *	to
244 		 *	ieee smallest float  = (2 ^ -127) * 0x1.00000
245 		 * In addtion, some rounding errors may occur do to the
246 		 * calculations involved.
247 		 */
248 		float f;
249 		int neg = 0;
250 		int exp = 0;
251 		int32_t val;
252 
253 		if (!XDR_GETINT32(xdrs, (int32_t *)&val))
254 			return (FALSE);
255 		neg = val & 0x80000000;
256 		exp = (val & 0x7f800000) >> 23;
257 		exp -= 127;		/* subtract exponent base */
258 		f = (val & 0x007fffff) * 0.00000011920928955078125;
259 		/* 2 ^ -23 */
260 		f++;
261 		while (exp != 0) {
262 			if (exp < 0) {
263 				f = f/2.0;
264 				++exp;
265 			} else {
266 				f = f * 2.0;
267 				--exp;
268 			}
269 		}
270 		if (neg)
271 			f = 0 - f;
272 		*fp = f;
273 		}
274 		return (TRUE);
275 #endif
276 #endif
277 
278 	case XDR_FREE:
279 		return (TRUE);
280 	}
281 	return (FALSE);
282 }
283 
284 /*
285  * This routine works on Suns (Sky / 68000's) and Vaxen.
286  */
287 
288 #if defined(vax)
289 /* What IEEE double precision floating point looks like on a Vax */
290 struct	ieee_double {
291 	unsigned int	mantissa1 : 20;
292 	unsigned int	exp	  : 11;
293 	unsigned int	sign	  : 1;
294 	unsigned int	mantissa2 : 32;
295 };
296 
297 /* Vax double precision floating point */
298 struct  vax_double {
299 	unsigned int	mantissa1 : 7;
300 	unsigned int	exp	  : 8;
301 	unsigned int	sign	  : 1;
302 	unsigned int	mantissa2 : 16;
303 	unsigned int	mantissa3 : 16;
304 	unsigned int	mantissa4 : 16;
305 };
306 
307 #define	VAX_DBL_BIAS	0x81
308 #define	IEEE_DBL_BIAS	0x3ff
309 #define	MASK(nbits)	((1 << nbits) - 1)
310 
311 static struct dbl_limits {
312 	struct	vax_double d;
313 	struct	ieee_double ieee;
314 } dbl_limits[2] = {
315 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
316 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
317 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
318 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
319 };
320 
321 #endif /* vax */
322 
323 
324 bool_t
325 xdr_double(XDR *xdrs, double *dp)
326 {
327 	int *lp;
328 #if defined(vax)
329 	struct	ieee_double id;
330 	struct	vax_double vd;
331 	struct dbl_limits *lim;
332 	size_t i;
333 #endif
334 
335 	switch (xdrs->x_op) {
336 
337 	case XDR_ENCODE:
338 #if defined(mc68000) || defined(u3b2) || defined(u3b15) || \
339 	defined(_LONG_LONG_HTOL)
340 		lp = (int *)dp;
341 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
342 #else
343 #if defined(_LONG_LONG_LTOH)
344 		lp = (int *)dp;
345 		lp++;
346 		return (XDR_PUTINT32(xdrs, lp--) && XDR_PUTINT32(xdrs, lp));
347 #else
348 #if defined(vax)
349 		vd = *((struct vax_double *)dp);
350 		for (i = 0, lim = dbl_limits;
351 			i < (int)(sizeof (dbl_limits) /
352 					sizeof (struct dbl_limits));
353 			i++, lim++) {
354 			if ((vd.mantissa4 == lim->d.mantissa4) &&
355 				(vd.mantissa3 == lim->d.mantissa3) &&
356 				(vd.mantissa2 == lim->d.mantissa2) &&
357 				(vd.mantissa1 == lim->d.mantissa1) &&
358 				(vd.exp == lim->d.exp)) {
359 				id = lim->ieee;
360 				goto shipit;
361 			}
362 		}
363 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
364 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
365 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
366 				(vd.mantissa3 << 13) |
367 				((vd.mantissa4 >> 3) & MASK(13));
368 	shipit:
369 		id.sign = vd.sign;
370 		lp = (int32_t *)&id;
371 #else
372 		{
373 		/*
374 		 * Every machine can do this, its just not very efficient.
375 		 * In addtion, some rounding errors may occur do to the
376 		 * calculations involved.
377 		 */
378 		double d;
379 		int neg = 0;
380 		int exp = 0;
381 		int32_t val[2];
382 
383 		d = *dp;
384 		if (d == 0) {
385 			val[0] = 0;
386 			val[1] = 0;
387 			lp = val;
388 			return (XDR_PUTINT32(xdrs, lp++) &&
389 				XDR_PUTINT32(xdrs, lp));
390 		}
391 		if (d < 0) {
392 			d = 0 - d;
393 			neg = 1;
394 		}
395 		while (d < 1) {
396 			d = d * 2;
397 			--exp;
398 		}
399 		while (d >= 2) {
400 			d = d/2;
401 			++exp;
402 		}
403 		if ((exp > 1024) || (exp < -1023)) {
404 			/* over or under flowing ieee exponent */
405 			return (FALSE);
406 		}
407 		val[0] = neg;
408 		val[0] = val[0] << 11;	/* for the exponent */
409 		val[0] += 1023 + exp;	/* 1023 is the bias */
410 		val[0] = val[0] << 20;	/* for the mantissa */
411 		val[0] += (int32_t)((d - 1) * 1048576);	/* 2 ^ 20 */
412 		val[1] += (int32_t)((((d - 1) * 1048576) - val[0])
413 							* 4294967296);
414 		/* 2 ^ 32 */
415 		lp = val;
416 		}
417 #endif
418 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
419 #endif
420 #endif
421 
422 	case XDR_DECODE:
423 #if defined(mc68000) || defined(u3b2) || defined(u3b15) || \
424 	defined(_LONG_LONG_HTOL)
425 		lp = (int *)dp;
426 		return (XDR_GETINT32(xdrs, lp++) && XDR_GETINT32(xdrs, lp));
427 #else
428 #if defined(_LONG_LONG_LTOH)
429 		lp = (int *)dp;
430 		lp++;
431 		return (XDR_GETINT32(xdrs, lp--) && XDR_GETINT32(xdrs, lp));
432 #else
433 #if defined(vax)
434 		lp = (int32_t *)&id;
435 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
436 			return (FALSE);
437 		for (i = 0, lim = dbl_limits;
438 			i < sizeof (dbl_limits)/sizeof (struct dbl_limits);
439 			i++, lim++) {
440 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
441 				(id.mantissa1 == lim->ieee.mantissa1) &&
442 				(id.exp == lim->ieee.exp)) {
443 				vd = lim->d;
444 				goto doneit;
445 			}
446 		}
447 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
448 		vd.mantissa1 = (id.mantissa1 >> 13);
449 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
450 				(id.mantissa2 >> 29);
451 		vd.mantissa3 = (id.mantissa2 >> 13);
452 		vd.mantissa4 = (id.mantissa2 << 3);
453 	doneit:
454 		vd.sign = id.sign;
455 		*dp = *((double *)&vd);
456 		return (TRUE);
457 #else
458 		{
459 		/*
460 		 * Every machine can do this, its just not very
461 		 * efficient.  It assumes that the decoding machine's
462 		 * double can represent any value in the range of
463 		 *	ieee largest  double  = (2 ^ 1024)  * 0x1.fffffffffffff
464 		 *	to
465 		 *	ieee smallest double  = (2 ^ -1023) * 0x1.0000000000000
466 		 * In addtion, some rounding errors may occur do to the
467 		 * calculations involved.
468 		 */
469 		double d;
470 		int neg = 0;
471 		int exp = 0;
472 		int32_t val[2];
473 
474 		lp = val;
475 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
476 			return (FALSE);
477 		neg = val[0] & 0x80000000;
478 		exp = (val[0] & 0x7ff00000) >> 20;
479 		exp -= 1023;		/* subtract exponent base */
480 		d = (val[0] & 0x000fffff) * 0.00000095367431640625;
481 		/* 2 ^ -20 */
482 		d += (val[1] * 0.0000000000000002220446049250313);
483 		/* 2 ^ -52 */
484 		d++;
485 		while (exp != 0) {
486 			if (exp < 0) {
487 				d = d/2.0;
488 				++exp;
489 			} else {
490 				d = d * 2.0;
491 				--exp;
492 			}
493 		}
494 		if (neg)
495 			d = 0 - d;
496 		*dp = d;
497 		}
498 #endif
499 #endif
500 #endif
501 
502 	case XDR_FREE:
503 		return (TRUE);
504 	}
505 	return (FALSE);
506 }
507 
508 /* ARGSUSED */
509 bool_t
510 xdr_quadruple(XDR *xdrs, long double *fp)
511 {
512 /*
513  * The Sparc uses IEEE FP encoding, so just do a byte copy
514  */
515 
516 #if !defined(sparc)
517 	return (FALSE);
518 #else
519 	switch (xdrs->x_op) {
520 	case XDR_ENCODE:
521 		return (XDR_PUTBYTES(xdrs, (char *)fp, sizeof (long double)));
522 	case XDR_DECODE:
523 		return (XDR_GETBYTES(xdrs, (char *)fp, sizeof (long double)));
524 	case XDR_FREE:
525 		return (TRUE);
526 	}
527 	return (FALSE);
528 #endif
529 }
530