xdr_float.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include <stdio.h>
  43. #include <endian.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. /*
  47. * NB: Not portable.
  48. * This routine works on Suns (Sky / 68000's) and Vaxen.
  49. */
  50. #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  51. #ifdef vax
  52. /* What IEEE single precision floating point looks like on a Vax */
  53. struct ieee_single {
  54. unsigned int mantissa: 23;
  55. unsigned int exp : 8;
  56. unsigned int sign : 1;
  57. };
  58. /* Vax single precision floating point */
  59. struct vax_single {
  60. unsigned int mantissa1 : 7;
  61. unsigned int exp : 8;
  62. unsigned int sign : 1;
  63. unsigned int mantissa2 : 16;
  64. };
  65. #define VAX_SNG_BIAS 0x81
  66. #define IEEE_SNG_BIAS 0x7f
  67. static struct sgl_limits {
  68. struct vax_single s;
  69. struct ieee_single ieee;
  70. } sgl_limits[2] = {
  71. {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  72. { 0x0, 0xff, 0x0 }}, /* Max IEEE */
  73. {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  74. { 0x0, 0x0, 0x0 }} /* Min IEEE */
  75. };
  76. #endif /* vax */
  77. bool_t
  78. xdr_float(XDR *xdrs, float *fp)
  79. {
  80. #ifdef vax
  81. struct ieee_single is;
  82. struct vax_single vs, *vsp;
  83. struct sgl_limits *lim;
  84. int i;
  85. #endif
  86. switch (xdrs->x_op) {
  87. case XDR_ENCODE:
  88. #ifdef vax
  89. vs = *((struct vax_single *)fp);
  90. for (i = 0, lim = sgl_limits;
  91. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  92. i++, lim++) {
  93. if ((vs.mantissa2 == lim->s.mantissa2) &&
  94. (vs.exp == lim->s.exp) &&
  95. (vs.mantissa1 == lim->s.mantissa1)) {
  96. is = lim->ieee;
  97. goto shipit;
  98. }
  99. }
  100. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  101. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  102. shipit:
  103. is.sign = vs.sign;
  104. return (XDR_PUTLONG(xdrs, (long *)&is));
  105. #else
  106. if (sizeof(float) == sizeof(long))
  107. return (XDR_PUTLONG(xdrs, (long *)fp));
  108. else if (sizeof(float) == sizeof(int)) {
  109. long tmp = *(int *)fp;
  110. return (XDR_PUTLONG(xdrs, &tmp));
  111. }
  112. break;
  113. #endif
  114. case XDR_DECODE:
  115. #ifdef vax
  116. vsp = (struct vax_single *)fp;
  117. if (!XDR_GETLONG(xdrs, (long *)&is))
  118. return (FALSE);
  119. for (i = 0, lim = sgl_limits;
  120. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  121. i++, lim++) {
  122. if ((is.exp == lim->ieee.exp) &&
  123. (is.mantissa == lim->ieee.mantissa)) {
  124. *vsp = lim->s;
  125. goto doneit;
  126. }
  127. }
  128. vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  129. vsp->mantissa2 = is.mantissa;
  130. vsp->mantissa1 = (is.mantissa >> 16);
  131. doneit:
  132. vsp->sign = is.sign;
  133. return (TRUE);
  134. #else
  135. if (sizeof(float) == sizeof(long))
  136. return (XDR_GETLONG(xdrs, (long *)fp));
  137. else if (sizeof(float) == sizeof(int)) {
  138. long tmp;
  139. if (XDR_GETLONG(xdrs, &tmp)) {
  140. *(int *)fp = tmp;
  141. return (TRUE);
  142. }
  143. }
  144. break;
  145. #endif
  146. case XDR_FREE:
  147. return (TRUE);
  148. }
  149. return (FALSE);
  150. }
  151. /*
  152. * This routine works on Suns (Sky / 68000's) and Vaxen.
  153. */
  154. #ifdef vax
  155. /* What IEEE double precision floating point looks like on a Vax */
  156. struct ieee_double {
  157. unsigned int mantissa1 : 20;
  158. unsigned int exp : 11;
  159. unsigned int sign : 1;
  160. unsigned int mantissa2 : 32;
  161. };
  162. /* Vax double precision floating point */
  163. struct vax_double {
  164. unsigned int mantissa1 : 7;
  165. unsigned int exp : 8;
  166. unsigned int sign : 1;
  167. unsigned int mantissa2 : 16;
  168. unsigned int mantissa3 : 16;
  169. unsigned int mantissa4 : 16;
  170. };
  171. #define VAX_DBL_BIAS 0x81
  172. #define IEEE_DBL_BIAS 0x3ff
  173. #define MASK(nbits) ((1 << nbits) - 1)
  174. static struct dbl_limits {
  175. struct vax_double d;
  176. struct ieee_double ieee;
  177. } dbl_limits[2] = {
  178. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  179. { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
  180. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  181. { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
  182. };
  183. #endif /* vax */
  184. bool_t
  185. xdr_double(XDR *xdrs, double *dp)
  186. {
  187. #ifdef vax
  188. struct ieee_double id;
  189. struct vax_double vd;
  190. register struct dbl_limits *lim;
  191. int i;
  192. #endif
  193. switch (xdrs->x_op) {
  194. case XDR_ENCODE:
  195. #ifdef vax
  196. vd = *((struct vax_double *)dp);
  197. for (i = 0, lim = dbl_limits;
  198. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  199. i++, lim++) {
  200. if ((vd.mantissa4 == lim->d.mantissa4) &&
  201. (vd.mantissa3 == lim->d.mantissa3) &&
  202. (vd.mantissa2 == lim->d.mantissa2) &&
  203. (vd.mantissa1 == lim->d.mantissa1) &&
  204. (vd.exp == lim->d.exp)) {
  205. id = lim->ieee;
  206. goto shipit;
  207. }
  208. }
  209. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  210. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  211. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  212. (vd.mantissa3 << 13) |
  213. ((vd.mantissa4 >> 3) & MASK(13));
  214. shipit:
  215. id.sign = vd.sign;
  216. dp = (double *)&id;
  217. #endif
  218. if (2*sizeof(long) == sizeof(double)) {
  219. long *lp = (long *)dp;
  220. return (XDR_PUTLONG(xdrs, lp+!LSW) &&
  221. XDR_PUTLONG(xdrs, lp+LSW));
  222. } else if (2*sizeof(int) == sizeof(double)) {
  223. int *ip = (int *)dp;
  224. long tmp[2];
  225. tmp[0] = ip[!LSW];
  226. tmp[1] = ip[LSW];
  227. return (XDR_PUTLONG(xdrs, tmp) &&
  228. XDR_PUTLONG(xdrs, tmp+1));
  229. }
  230. break;
  231. case XDR_DECODE:
  232. #ifdef vax
  233. lp = (long *)&id;
  234. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  235. return (FALSE);
  236. for (i = 0, lim = dbl_limits;
  237. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  238. i++, lim++) {
  239. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  240. (id.mantissa1 == lim->ieee.mantissa1) &&
  241. (id.exp == lim->ieee.exp)) {
  242. vd = lim->d;
  243. goto doneit;
  244. }
  245. }
  246. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  247. vd.mantissa1 = (id.mantissa1 >> 13);
  248. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  249. (id.mantissa2 >> 29);
  250. vd.mantissa3 = (id.mantissa2 >> 13);
  251. vd.mantissa4 = (id.mantissa2 << 3);
  252. doneit:
  253. vd.sign = id.sign;
  254. *dp = *((double *)&vd);
  255. return (TRUE);
  256. #else
  257. if (2*sizeof(long) == sizeof(double)) {
  258. long *lp = (long *)dp;
  259. return (XDR_GETLONG(xdrs, lp+!LSW) &&
  260. XDR_GETLONG(xdrs, lp+LSW));
  261. } else if (2*sizeof(int) == sizeof(double)) {
  262. int *ip = (int *)dp;
  263. long tmp[2];
  264. if (XDR_GETLONG(xdrs, tmp+!LSW) &&
  265. XDR_GETLONG(xdrs, tmp+LSW)) {
  266. ip[0] = tmp[0];
  267. ip[1] = tmp[1];
  268. return (TRUE);
  269. }
  270. }
  271. break;
  272. #endif
  273. case XDR_FREE:
  274. return (TRUE);
  275. }
  276. return (FALSE);
  277. }