fpu_explode.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* $NetBSD: fpu_explode.c,v 1.6 2005/12/11 12:18:42 christos Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (c) 1992, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This software was developed by the Computer Systems Engineering group
  9. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  10. * contributed to Berkeley.
  11. *
  12. * All advertising materials mentioning features or use of this software
  13. * must display the following acknowledgement:
  14. * This product includes software developed by the University of
  15. * California, Lawrence Berkeley Laboratory.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * 3. Neither the name of the University nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE.
  40. *
  41. * @(#)fpu_explode.c 8.1 (Berkeley) 6/11/93
  42. */
  43. /*
  44. * FPU subroutines: `explode' the machine's `packed binary' format numbers
  45. * into our internal format.
  46. */
  47. #include <sys/cdefs.h>
  48. __FBSDID("$FreeBSD$");
  49. #include <sys/types.h>
  50. #include <sys/systm.h>
  51. #include <machine/fpu.h>
  52. #include <machine/ieee.h>
  53. #include <machine/pcb.h>
  54. #include <powerpc/fpu/fpu_arith.h>
  55. #include <powerpc/fpu/fpu_emu.h>
  56. #include <powerpc/fpu/fpu_extern.h>
  57. #include <powerpc/fpu/fpu_instr.h>
  58. /*
  59. * N.B.: in all of the following, we assume the FP format is
  60. *
  61. * ---------------------------
  62. * | s | exponent | fraction |
  63. * ---------------------------
  64. *
  65. * (which represents -1**s * 1.fraction * 2**exponent), so that the
  66. * sign bit is way at the top (bit 31), the exponent is next, and
  67. * then the remaining bits mark the fraction. A zero exponent means
  68. * zero or denormalized (0.fraction rather than 1.fraction), and the
  69. * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
  70. *
  71. * Since the sign bit is always the topmost bit---this holds even for
  72. * integers---we set that outside all the *tof functions. Each function
  73. * returns the class code for the new number (but note that we use
  74. * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
  75. */
  76. /*
  77. * int -> fpn.
  78. */
  79. int
  80. fpu_itof(struct fpn *fp, u_int i)
  81. {
  82. if (i == 0)
  83. return (FPC_ZERO);
  84. /*
  85. * The value FP_1 represents 2^FP_LG, so set the exponent
  86. * there and let normalization fix it up. Convert negative
  87. * numbers to sign-and-magnitude. Note that this relies on
  88. * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
  89. */
  90. fp->fp_exp = FP_LG;
  91. fp->fp_mant[0] = (int)i < 0 ? -i : i;
  92. fp->fp_mant[1] = 0;
  93. fp->fp_mant[2] = 0;
  94. fp->fp_mant[3] = 0;
  95. fpu_norm(fp);
  96. return (FPC_NUM);
  97. }
  98. /*
  99. * 64-bit int -> fpn.
  100. */
  101. int
  102. fpu_xtof(struct fpn *fp, u_int64_t i)
  103. {
  104. if (i == 0)
  105. return (FPC_ZERO);
  106. /*
  107. * The value FP_1 represents 2^FP_LG, so set the exponent
  108. * there and let normalization fix it up. Convert negative
  109. * numbers to sign-and-magnitude. Note that this relies on
  110. * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
  111. */
  112. fp->fp_exp = FP_LG2;
  113. *((int64_t*)fp->fp_mant) = (int64_t)i < 0 ? -i : i;
  114. fp->fp_mant[2] = 0;
  115. fp->fp_mant[3] = 0;
  116. fpu_norm(fp);
  117. return (FPC_NUM);
  118. }
  119. #define mask(nbits) ((1L << (nbits)) - 1)
  120. /*
  121. * All external floating formats convert to internal in the same manner,
  122. * as defined here. Note that only normals get an implied 1.0 inserted.
  123. */
  124. #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
  125. if (exp == 0) { \
  126. if (allfrac == 0) \
  127. return (FPC_ZERO); \
  128. fp->fp_exp = 1 - expbias; \
  129. fp->fp_mant[0] = f0; \
  130. fp->fp_mant[1] = f1; \
  131. fp->fp_mant[2] = f2; \
  132. fp->fp_mant[3] = f3; \
  133. fpu_norm(fp); \
  134. return (FPC_NUM); \
  135. } \
  136. if (exp == (2 * expbias + 1)) { \
  137. if (allfrac == 0) \
  138. return (FPC_INF); \
  139. fp->fp_mant[0] = f0; \
  140. fp->fp_mant[1] = f1; \
  141. fp->fp_mant[2] = f2; \
  142. fp->fp_mant[3] = f3; \
  143. return (FPC_QNAN); \
  144. } \
  145. fp->fp_exp = exp - expbias; \
  146. fp->fp_mant[0] = FP_1 | f0; \
  147. fp->fp_mant[1] = f1; \
  148. fp->fp_mant[2] = f2; \
  149. fp->fp_mant[3] = f3; \
  150. return (FPC_NUM)
  151. /*
  152. * 32-bit single precision -> fpn.
  153. * We assume a single occupies at most (64-FP_LG) bits in the internal
  154. * format: i.e., needs at most fp_mant[0] and fp_mant[1].
  155. */
  156. int
  157. fpu_stof(struct fpn *fp, u_int i)
  158. {
  159. int exp;
  160. u_int frac, f0, f1;
  161. #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
  162. exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
  163. frac = i & mask(SNG_FRACBITS);
  164. f0 = frac >> SNG_SHIFT;
  165. f1 = frac << (32 - SNG_SHIFT);
  166. FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
  167. }
  168. /*
  169. * 64-bit double -> fpn.
  170. * We assume this uses at most (96-FP_LG) bits.
  171. */
  172. int
  173. fpu_dtof(struct fpn *fp, u_int i, u_int j)
  174. {
  175. int exp;
  176. u_int frac, f0, f1, f2;
  177. #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
  178. exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
  179. frac = i & mask(DBL_FRACBITS - 32);
  180. f0 = frac >> DBL_SHIFT;
  181. f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
  182. f2 = j << (32 - DBL_SHIFT);
  183. frac |= j;
  184. FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
  185. }
  186. /*
  187. * Explode the contents of a register / regpair / regquad.
  188. * If the input is a signalling NaN, an NV (invalid) exception
  189. * will be set. (Note that nothing but NV can occur until ALU
  190. * operations are performed.)
  191. */
  192. void
  193. fpu_explode(struct fpemu *fe, struct fpn *fp, int type, int reg)
  194. {
  195. u_int s, *space;
  196. u_int64_t l, *xspace;
  197. xspace = (u_int64_t *)&fe->fe_fpstate->fpr[reg].fpr;
  198. l = xspace[0];
  199. space = (u_int *)&fe->fe_fpstate->fpr[reg].fpr;
  200. s = space[0];
  201. fp->fp_sign = s >> 31;
  202. fp->fp_sticky = 0;
  203. switch (type) {
  204. case FTYPE_LNG:
  205. s = fpu_xtof(fp, l);
  206. break;
  207. case FTYPE_INT:
  208. s = fpu_itof(fp, space[1]);
  209. break;
  210. case FTYPE_SNG:
  211. s = fpu_stof(fp, s);
  212. break;
  213. case FTYPE_DBL:
  214. s = fpu_dtof(fp, s, space[1]);
  215. break;
  216. default:
  217. panic("fpu_explode");
  218. panic("fpu_explode: invalid type %d", type);
  219. }
  220. if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
  221. /*
  222. * Input is a signalling NaN. All operations that return
  223. * an input NaN operand put it through a ``NaN conversion'',
  224. * which basically just means ``turn on the quiet bit''.
  225. * We do this here so that all NaNs internally look quiet
  226. * (we can tell signalling ones by their class).
  227. */
  228. fp->fp_mant[0] |= FP_QUIETBIT;
  229. fe->fe_cx = FPSCR_VXSNAN; /* assert invalid operand */
  230. s = FPC_SNAN;
  231. }
  232. fp->fp_class = s;
  233. DPRINTF(FPE_REG, ("fpu_explode: %%%c%d => ", (type == FTYPE_LNG) ? 'x' :
  234. ((type == FTYPE_INT) ? 'i' :
  235. ((type == FTYPE_SNG) ? 's' :
  236. ((type == FTYPE_DBL) ? 'd' : '?'))),
  237. reg));
  238. DUMPFPN(FPE_REG, fp);
  239. DPRINTF(FPE_REG, ("\n"));
  240. }