fpu_emu.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* $NetBSD: fpu_emu.h,v 1.3 2005/12/11 12:18:42 christos Exp $ */
  2. /* $FreeBSD$ */
  3. /*-
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Copyright (c) 1992, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This software was developed by the Computer Systems Engineering group
  10. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  11. * contributed to Berkeley.
  12. *
  13. * All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Lawrence Berkeley Laboratory.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. * 3. Neither the name of the University nor the names of its contributors
  27. * may be used to endorse or promote products derived from this software
  28. * without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  31. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  34. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40. * SUCH DAMAGE.
  41. *
  42. * @(#)fpu_emu.h 8.1 (Berkeley) 6/11/93
  43. */
  44. /*
  45. * Floating point emulator (tailored for SPARC, but structurally
  46. * machine-independent).
  47. *
  48. * Floating point numbers are carried around internally in an `expanded'
  49. * or `unpacked' form consisting of:
  50. * - sign
  51. * - unbiased exponent
  52. * - mantissa (`1.' + 112-bit fraction + guard + round)
  53. * - sticky bit
  54. * Any implied `1' bit is inserted, giving a 113-bit mantissa that is
  55. * always nonzero. Additional low-order `guard' and `round' bits are
  56. * scrunched in, making the entire mantissa 115 bits long. This is divided
  57. * into four 32-bit words, with `spare' bits left over in the upper part
  58. * of the top word (the high bits of fp_mant[0]). An internal `exploded'
  59. * number is thus kept within the half-open interval [1.0,2.0) (but see
  60. * the `number classes' below). This holds even for denormalized numbers:
  61. * when we explode an external denorm, we normalize it, introducing low-order
  62. * zero bits, so that the rest of the code always sees normalized values.
  63. *
  64. * Note that a number of our algorithms use the `spare' bits at the top.
  65. * The most demanding algorithm---the one for sqrt---depends on two such
  66. * bits, so that it can represent values up to (but not including) 8.0,
  67. * and then it needs a carry on top of that, so that we need three `spares'.
  68. *
  69. * The sticky-word is 32 bits so that we can use `OR' operators to goosh
  70. * whole words from the mantissa into it.
  71. *
  72. * All operations are done in this internal extended precision. According
  73. * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is,
  74. * it is OK to do a+b in extended precision and then round the result to
  75. * single precision---provided single, double, and extended precisions are
  76. * `far enough apart' (they always are), but we will try to avoid any such
  77. * extra work where possible.
  78. */
  79. struct fpn {
  80. int fp_class; /* see below */
  81. int fp_sign; /* 0 => positive, 1 => negative */
  82. int fp_exp; /* exponent (unbiased) */
  83. int fp_sticky; /* nonzero bits lost at right end */
  84. u_int fp_mant[4]; /* 115-bit mantissa */
  85. };
  86. #define FP_NMANT 115 /* total bits in mantissa (incl g,r) */
  87. #define FP_NG 2 /* number of low-order guard bits */
  88. #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */
  89. #define FP_LG2 ((FP_NMANT - 1) & 63) /* log2(1.0) for fp_mant[0] and fp_mant[1] */
  90. #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */
  91. #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */
  92. #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */
  93. /*
  94. * Number classes. Since zero, Inf, and NaN cannot be represented using
  95. * the above layout, we distinguish these from other numbers via a class.
  96. * In addition, to make computation easier and to follow Appendix N of
  97. * the SPARC Version 8 standard, we give each kind of NaN a separate class.
  98. */
  99. #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */
  100. #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */
  101. #define FPC_ZERO 0 /* zero (sign matters) */
  102. #define FPC_NUM 1 /* number (sign matters) */
  103. #define FPC_INF 2 /* infinity (sign matters) */
  104. #define ISSNAN(fp) ((fp)->fp_class == FPC_SNAN)
  105. #define ISQNAN(fp) ((fp)->fp_class == FPC_QNAN)
  106. #define ISNAN(fp) ((fp)->fp_class < 0)
  107. #define ISZERO(fp) ((fp)->fp_class == 0)
  108. #define ISINF(fp) ((fp)->fp_class == FPC_INF)
  109. /*
  110. * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points
  111. * to the `more significant' operand for our purposes. Appendix N says that
  112. * the result of a computation involving two numbers are:
  113. *
  114. * If both are SNaN: operand 2, converted to Quiet
  115. * If only one is SNaN: the SNaN operand, converted to Quiet
  116. * If both are QNaN: operand 2
  117. * If only one is QNaN: the QNaN operand
  118. *
  119. * In addition, in operations with an Inf operand, the result is usually
  120. * Inf. The class numbers are carefully arranged so that if
  121. * (unsigned)class(op1) > (unsigned)class(op2)
  122. * then op1 is the one we want; otherwise op2 is the one we want.
  123. */
  124. #define ORDER(x, y) { \
  125. if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \
  126. SWAP(x, y); \
  127. }
  128. #define SWAP(x, y) { \
  129. struct fpn *swap; \
  130. swap = (x), (x) = (y), (y) = swap; \
  131. }
  132. /*
  133. * Emulator state.
  134. */
  135. struct fpemu {
  136. struct fpu *fe_fpstate; /* registers, etc */
  137. int fe_fpscr; /* fpscr copy (modified during op) */
  138. int fe_cx; /* keep track of exceptions */
  139. struct fpn fe_f1; /* operand 1 */
  140. struct fpn fe_f2; /* operand 2, if required */
  141. struct fpn fe_f3; /* available storage for result */
  142. };
  143. /*
  144. * Arithmetic functions.
  145. * Each of these may modify its inputs (f1,f2) and/or the temporary.
  146. * Each returns a pointer to the result and/or sets exceptions.
  147. */
  148. struct fpn *fpu_add(struct fpemu *);
  149. #define fpu_sub(fe) ((fe)->fe_f2.fp_sign ^= 1, fpu_add(fe))
  150. struct fpn *fpu_mul(struct fpemu *);
  151. struct fpn *fpu_div(struct fpemu *);
  152. struct fpn *fpu_sqrt(struct fpemu *);
  153. /*
  154. * Other functions.
  155. */
  156. /* Perform a compare instruction (with or without unordered exception). */
  157. void fpu_compare(struct fpemu *, int);
  158. /* Build a new Quiet NaN (sign=0, frac=all 1's). */
  159. struct fpn *fpu_newnan(struct fpemu *);
  160. void fpu_norm(struct fpn *);
  161. /*
  162. * Shift a number right some number of bits, taking care of round/sticky.
  163. * Note that the result is probably not a well-formed number (it will lack
  164. * the normal 1-bit mant[0]&FP_1).
  165. */
  166. int fpu_shr(struct fpn *, int);
  167. void fpu_explode(struct fpemu *, struct fpn *, int, int);
  168. void fpu_implode(struct fpemu *, struct fpn *, int, u_int *);
  169. #ifdef DEBUG
  170. #define FPE_EX 0x1
  171. #define FPE_INSN 0x2
  172. #define FPE_OP 0x4
  173. #define FPE_REG 0x8
  174. extern int fpe_debug;
  175. void fpu_dumpfpn(struct fpn *);
  176. #define DPRINTF(x, y) if (fpe_debug & (x)) printf y
  177. #define DUMPFPN(x, f) if (fpe_debug & (x)) fpu_dumpfpn((f))
  178. #else
  179. #define DPRINTF(x, y)
  180. #define DUMPFPN(x, f)
  181. #endif