xdr_float.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. #if 0
  31. static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_float.c, Generic XDR routines implementation.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * These are the "floating point" xdr routines used to (de)serialize
  39. * most common data items. See xdr.h for more info on the interface to
  40. * xdr.
  41. */
  42. #define __FORCE_GLIBC
  43. #include <features.h>
  44. #include <stdio.h>
  45. #include <endian.h>
  46. #include <rpc/types.h>
  47. #include <rpc/xdr.h>
  48. /*
  49. * NB: Not portable.
  50. * This routine works on Suns (Sky / 68000's) and Vaxen.
  51. */
  52. #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  53. #ifdef vax
  54. /* What IEEE single precision floating point looks like on a Vax */
  55. struct ieee_single {
  56. unsigned int mantissa: 23;
  57. unsigned int exp : 8;
  58. unsigned int sign : 1;
  59. };
  60. /* Vax single precision floating point */
  61. struct vax_single {
  62. unsigned int mantissa1 : 7;
  63. unsigned int exp : 8;
  64. unsigned int sign : 1;
  65. unsigned int mantissa2 : 16;
  66. };
  67. #define VAX_SNG_BIAS 0x81
  68. #define IEEE_SNG_BIAS 0x7f
  69. static struct sgl_limits {
  70. struct vax_single s;
  71. struct ieee_single ieee;
  72. } sgl_limits[2] = {
  73. {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  74. { 0x0, 0xff, 0x0 }}, /* Max IEEE */
  75. {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  76. { 0x0, 0x0, 0x0 }} /* Min IEEE */
  77. };
  78. #endif /* vax */
  79. bool_t
  80. xdr_float(XDR *xdrs, float *fp)
  81. {
  82. #ifdef vax
  83. struct ieee_single is;
  84. struct vax_single vs, *vsp;
  85. struct sgl_limits *lim;
  86. int i;
  87. #endif
  88. switch (xdrs->x_op) {
  89. case XDR_ENCODE:
  90. #ifdef vax
  91. vs = *((struct vax_single *)fp);
  92. for (i = 0, lim = sgl_limits;
  93. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  94. i++, lim++) {
  95. if ((vs.mantissa2 == lim->s.mantissa2) &&
  96. (vs.exp == lim->s.exp) &&
  97. (vs.mantissa1 == lim->s.mantissa1)) {
  98. is = lim->ieee;
  99. goto shipit;
  100. }
  101. }
  102. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  103. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  104. shipit:
  105. is.sign = vs.sign;
  106. return (XDR_PUTLONG(xdrs, (long *)&is));
  107. #else
  108. if (sizeof(float) == sizeof(long))
  109. return (XDR_PUTLONG(xdrs, (long *)fp));
  110. else if (sizeof(float) == sizeof(int)) {
  111. long tmp = *(int *)fp;
  112. return (XDR_PUTLONG(xdrs, &tmp));
  113. }
  114. break;
  115. #endif
  116. case XDR_DECODE:
  117. #ifdef vax
  118. vsp = (struct vax_single *)fp;
  119. if (!XDR_GETLONG(xdrs, (long *)&is))
  120. return (FALSE);
  121. for (i = 0, lim = sgl_limits;
  122. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  123. i++, lim++) {
  124. if ((is.exp == lim->ieee.exp) &&
  125. (is.mantissa == lim->ieee.mantissa)) {
  126. *vsp = lim->s;
  127. goto doneit;
  128. }
  129. }
  130. vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  131. vsp->mantissa2 = is.mantissa;
  132. vsp->mantissa1 = (is.mantissa >> 16);
  133. doneit:
  134. vsp->sign = is.sign;
  135. return (TRUE);
  136. #else
  137. if (sizeof(float) == sizeof(long))
  138. return (XDR_GETLONG(xdrs, (long *)fp));
  139. else if (sizeof(float) == sizeof(int)) {
  140. long tmp;
  141. if (XDR_GETLONG(xdrs, &tmp)) {
  142. *(int *)fp = tmp;
  143. return (TRUE);
  144. }
  145. }
  146. break;
  147. #endif
  148. case XDR_FREE:
  149. return (TRUE);
  150. }
  151. return (FALSE);
  152. }
  153. /*
  154. * This routine works on Suns (Sky / 68000's) and Vaxen.
  155. */
  156. #ifdef vax
  157. /* What IEEE double precision floating point looks like on a Vax */
  158. struct ieee_double {
  159. unsigned int mantissa1 : 20;
  160. unsigned int exp : 11;
  161. unsigned int sign : 1;
  162. unsigned int mantissa2 : 32;
  163. };
  164. /* Vax double precision floating point */
  165. struct vax_double {
  166. unsigned int mantissa1 : 7;
  167. unsigned int exp : 8;
  168. unsigned int sign : 1;
  169. unsigned int mantissa2 : 16;
  170. unsigned int mantissa3 : 16;
  171. unsigned int mantissa4 : 16;
  172. };
  173. #define VAX_DBL_BIAS 0x81
  174. #define IEEE_DBL_BIAS 0x3ff
  175. #define MASK(nbits) ((1 << nbits) - 1)
  176. static struct dbl_limits {
  177. struct vax_double d;
  178. struct ieee_double ieee;
  179. } dbl_limits[2] = {
  180. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  181. { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
  182. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  183. { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
  184. };
  185. #endif /* vax */
  186. bool_t
  187. xdr_double(XDR *xdrs, double *dp)
  188. {
  189. #ifdef vax
  190. struct ieee_double id;
  191. struct vax_double vd;
  192. register struct dbl_limits *lim;
  193. int i;
  194. #endif
  195. switch (xdrs->x_op) {
  196. case XDR_ENCODE:
  197. #ifdef vax
  198. vd = *((struct vax_double *)dp);
  199. for (i = 0, lim = dbl_limits;
  200. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  201. i++, lim++) {
  202. if ((vd.mantissa4 == lim->d.mantissa4) &&
  203. (vd.mantissa3 == lim->d.mantissa3) &&
  204. (vd.mantissa2 == lim->d.mantissa2) &&
  205. (vd.mantissa1 == lim->d.mantissa1) &&
  206. (vd.exp == lim->d.exp)) {
  207. id = lim->ieee;
  208. goto shipit;
  209. }
  210. }
  211. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  212. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  213. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  214. (vd.mantissa3 << 13) |
  215. ((vd.mantissa4 >> 3) & MASK(13));
  216. shipit:
  217. id.sign = vd.sign;
  218. dp = (double *)&id;
  219. #endif
  220. if (2*sizeof(long) == sizeof(double)) {
  221. long *lp = (long *)dp;
  222. return (XDR_PUTLONG(xdrs, lp+!LSW) &&
  223. XDR_PUTLONG(xdrs, lp+LSW));
  224. } else if (2*sizeof(int) == sizeof(double)) {
  225. int *ip = (int *)dp;
  226. long tmp[2];
  227. tmp[0] = ip[!LSW];
  228. tmp[1] = ip[LSW];
  229. return (XDR_PUTLONG(xdrs, tmp) &&
  230. XDR_PUTLONG(xdrs, tmp+1));
  231. }
  232. break;
  233. case XDR_DECODE:
  234. #ifdef vax
  235. lp = (long *)&id;
  236. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  237. return (FALSE);
  238. for (i = 0, lim = dbl_limits;
  239. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  240. i++, lim++) {
  241. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  242. (id.mantissa1 == lim->ieee.mantissa1) &&
  243. (id.exp == lim->ieee.exp)) {
  244. vd = lim->d;
  245. goto doneit;
  246. }
  247. }
  248. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  249. vd.mantissa1 = (id.mantissa1 >> 13);
  250. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  251. (id.mantissa2 >> 29);
  252. vd.mantissa3 = (id.mantissa2 >> 13);
  253. vd.mantissa4 = (id.mantissa2 << 3);
  254. doneit:
  255. vd.sign = id.sign;
  256. *dp = *((double *)&vd);
  257. return (TRUE);
  258. #else
  259. if (2*sizeof(long) == sizeof(double)) {
  260. long *lp = (long *)dp;
  261. return (XDR_GETLONG(xdrs, lp+!LSW) &&
  262. XDR_GETLONG(xdrs, lp+LSW));
  263. } else if (2*sizeof(int) == sizeof(double)) {
  264. int *ip = (int *)dp;
  265. long tmp[2];
  266. if (XDR_GETLONG(xdrs, tmp+!LSW) &&
  267. XDR_GETLONG(xdrs, tmp+LSW)) {
  268. ip[0] = tmp[0];
  269. ip[1] = tmp[1];
  270. return (TRUE);
  271. }
  272. }
  273. break;
  274. #endif
  275. case XDR_FREE:
  276. return (TRUE);
  277. }
  278. return (FALSE);
  279. }