xdr_float.c 7.5 KB

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