xdr_float.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * xdr_float.c, Generic XDR routines implementation.
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * These are the "floating point" xdr routines used to (de)serialize
  34. * most common data items. See xdr.h for more info on the interface to
  35. * xdr.
  36. */
  37. #include <stdio.h>
  38. #include <endian.h>
  39. #include <rpc/types.h>
  40. #include <rpc/xdr.h>
  41. #include <shlib-compat.h>
  42. /*
  43. * NB: Not portable.
  44. * This routine works on Suns (Sky / 68000's) and Vaxen.
  45. */
  46. #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  47. #ifdef vax
  48. /* What IEEE single precision floating point looks like on a Vax */
  49. struct ieee_single {
  50. unsigned int mantissa: 23;
  51. unsigned int exp : 8;
  52. unsigned int sign : 1;
  53. };
  54. /* Vax single precision floating point */
  55. struct vax_single {
  56. unsigned int mantissa1 : 7;
  57. unsigned int exp : 8;
  58. unsigned int sign : 1;
  59. unsigned int mantissa2 : 16;
  60. };
  61. #define VAX_SNG_BIAS 0x81
  62. #define IEEE_SNG_BIAS 0x7f
  63. static struct sgl_limits {
  64. struct vax_single s;
  65. struct ieee_single ieee;
  66. } sgl_limits[2] = {
  67. {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  68. { 0x0, 0xff, 0x0 }}, /* Max IEEE */
  69. {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  70. { 0x0, 0x0, 0x0 }} /* Min IEEE */
  71. };
  72. #endif /* vax */
  73. bool_t
  74. xdr_float (XDR *xdrs, float *fp)
  75. {
  76. #ifdef vax
  77. struct ieee_single is;
  78. struct vax_single vs, *vsp;
  79. struct sgl_limits *lim;
  80. int i;
  81. #endif
  82. switch (xdrs->x_op) {
  83. case XDR_ENCODE:
  84. #ifdef vax
  85. vs = *((struct vax_single *)fp);
  86. for (i = 0, lim = sgl_limits;
  87. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  88. i++, lim++) {
  89. if ((vs.mantissa2 == lim->s.mantissa2) &&
  90. (vs.exp == lim->s.exp) &&
  91. (vs.mantissa1 == lim->s.mantissa1)) {
  92. is = lim->ieee;
  93. goto shipit;
  94. }
  95. }
  96. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  97. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  98. shipit:
  99. is.sign = vs.sign;
  100. return (XDR_PUTLONG(xdrs, (long *)&is));
  101. #else
  102. if (sizeof(float) == sizeof(long))
  103. return (XDR_PUTLONG(xdrs, (long *)fp));
  104. else if (sizeof(float) == sizeof(int)) {
  105. long tmp = *(int *)fp;
  106. return (XDR_PUTLONG(xdrs, &tmp));
  107. }
  108. break;
  109. #endif
  110. case XDR_DECODE:
  111. #ifdef vax
  112. vsp = (struct vax_single *)fp;
  113. if (!XDR_GETLONG(xdrs, (long *)&is))
  114. return (FALSE);
  115. for (i = 0, lim = sgl_limits;
  116. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  117. i++, lim++) {
  118. if ((is.exp == lim->ieee.exp) &&
  119. (is.mantissa == lim->ieee.mantissa)) {
  120. *vsp = lim->s;
  121. goto doneit;
  122. }
  123. }
  124. vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  125. vsp->mantissa2 = is.mantissa;
  126. vsp->mantissa1 = (is.mantissa >> 16);
  127. doneit:
  128. vsp->sign = is.sign;
  129. return (TRUE);
  130. #else
  131. if (sizeof(float) == sizeof(long))
  132. return (XDR_GETLONG(xdrs, (long *)fp));
  133. else if (sizeof(float) == sizeof(int)) {
  134. long tmp;
  135. if (XDR_GETLONG(xdrs, &tmp)) {
  136. *(int *)fp = tmp;
  137. return (TRUE);
  138. }
  139. }
  140. break;
  141. #endif
  142. case XDR_FREE:
  143. return (TRUE);
  144. }
  145. return (FALSE);
  146. }
  147. libc_hidden_nolink_sunrpc (xdr_float, GLIBC_2_0)
  148. /*
  149. * This routine works on Suns (Sky / 68000's) and Vaxen.
  150. */
  151. #ifdef vax
  152. /* What IEEE double precision floating point looks like on a Vax */
  153. struct ieee_double {
  154. unsigned int mantissa1 : 20;
  155. unsigned int exp : 11;
  156. unsigned int sign : 1;
  157. unsigned int mantissa2 : 32;
  158. };
  159. /* Vax double precision floating point */
  160. struct vax_double {
  161. unsigned int mantissa1 : 7;
  162. unsigned int exp : 8;
  163. unsigned int sign : 1;
  164. unsigned int mantissa2 : 16;
  165. unsigned int mantissa3 : 16;
  166. unsigned int mantissa4 : 16;
  167. };
  168. #define VAX_DBL_BIAS 0x81
  169. #define IEEE_DBL_BIAS 0x3ff
  170. #define MASK(nbits) ((1 << nbits) - 1)
  171. static struct dbl_limits {
  172. struct vax_double d;
  173. struct ieee_double ieee;
  174. } dbl_limits[2] = {
  175. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  176. { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
  177. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  178. { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
  179. };
  180. #endif /* vax */
  181. bool_t
  182. xdr_double (XDR *xdrs, double *dp)
  183. {
  184. #ifdef vax
  185. struct ieee_double id;
  186. struct vax_double vd;
  187. register struct dbl_limits *lim;
  188. int i;
  189. #endif
  190. switch (xdrs->x_op) {
  191. case XDR_ENCODE:
  192. #ifdef vax
  193. vd = *((struct vax_double *)dp);
  194. for (i = 0, lim = dbl_limits;
  195. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  196. i++, lim++) {
  197. if ((vd.mantissa4 == lim->d.mantissa4) &&
  198. (vd.mantissa3 == lim->d.mantissa3) &&
  199. (vd.mantissa2 == lim->d.mantissa2) &&
  200. (vd.mantissa1 == lim->d.mantissa1) &&
  201. (vd.exp == lim->d.exp)) {
  202. id = lim->ieee;
  203. goto shipit;
  204. }
  205. }
  206. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  207. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  208. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  209. (vd.mantissa3 << 13) |
  210. ((vd.mantissa4 >> 3) & MASK(13));
  211. shipit:
  212. id.sign = vd.sign;
  213. dp = (double *)&id;
  214. #endif
  215. if (2*sizeof(long) == sizeof(double)) {
  216. long *lp = (long *)dp;
  217. return (XDR_PUTLONG(xdrs, lp+!LSW) &&
  218. XDR_PUTLONG(xdrs, lp+LSW));
  219. } else if (2*sizeof(int) == sizeof(double)) {
  220. int *ip = (int *)dp;
  221. long tmp[2];
  222. tmp[0] = ip[!LSW];
  223. tmp[1] = ip[LSW];
  224. return (XDR_PUTLONG(xdrs, tmp) &&
  225. XDR_PUTLONG(xdrs, tmp+1));
  226. }
  227. break;
  228. case XDR_DECODE:
  229. #ifdef vax
  230. lp = (long *)&id;
  231. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  232. return (FALSE);
  233. for (i = 0, lim = dbl_limits;
  234. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  235. i++, lim++) {
  236. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  237. (id.mantissa1 == lim->ieee.mantissa1) &&
  238. (id.exp == lim->ieee.exp)) {
  239. vd = lim->d;
  240. goto doneit;
  241. }
  242. }
  243. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  244. vd.mantissa1 = (id.mantissa1 >> 13);
  245. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  246. (id.mantissa2 >> 29);
  247. vd.mantissa3 = (id.mantissa2 >> 13);
  248. vd.mantissa4 = (id.mantissa2 << 3);
  249. doneit:
  250. vd.sign = id.sign;
  251. *dp = *((double *)&vd);
  252. return (TRUE);
  253. #else
  254. if (2*sizeof(long) == sizeof(double)) {
  255. long *lp = (long *)dp;
  256. return (XDR_GETLONG(xdrs, lp+!LSW) &&
  257. XDR_GETLONG(xdrs, lp+LSW));
  258. } else if (2*sizeof(int) == sizeof(double)) {
  259. int *ip = (int *)dp;
  260. long tmp[2];
  261. if (XDR_GETLONG(xdrs, tmp+!LSW) &&
  262. XDR_GETLONG(xdrs, tmp+LSW)) {
  263. ip[0] = tmp[0];
  264. ip[1] = tmp[1];
  265. return (TRUE);
  266. }
  267. }
  268. break;
  269. #endif
  270. case XDR_FREE:
  271. return (TRUE);
  272. }
  273. return (FALSE);
  274. }
  275. libc_hidden_nolink_sunrpc (xdr_double, GLIBC_2_0)