correlativeCalculationWaterTankFireHydrant.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * linux/arch/arm/vfp/vfpsingle.c
  3. *
  4. * This code is derived in part from John R. Housers softfloat library, which
  5. * carries the following notice:
  6. *
  7. * ===========================================================================
  8. * This C source file is part of the SoftFloat IEC/IEEE Floating-point
  9. * Arithmetic Package, Release 2.
  10. *
  11. * Written by John R. Hauser. This work was made possible in part by the
  12. * International Computer Science Institute, located at Suite 600, 1947 Center
  13. * Street, Berkeley, California 94704. Funding was partially provided by the
  14. * National Science Foundation under grant MIP-9311980. The original version
  15. * of this code was written as part of a project to build a fixed-point vector
  16. * processor in collaboration with the University of California at Berkeley,
  17. * overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  18. * is available through the web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  19. * arithmetic/softfloat.html'.
  20. *
  21. * THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  22. * has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  23. * TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  24. * PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  25. * AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  26. *
  27. * Derivative works are acceptable, even for commercial purposes, so long as
  28. * (1) they include prominent notice that the work is derivative, and (2) they
  29. * include prominent notice akin to these three paragraphs for those parts of
  30. * this code that are retained.
  31. * ===========================================================================
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/bitops.h>
  35. #include <asm/div64.h>
  36. #include <asm/vfp.h>
  37. #include "vfpinstr.h"
  38. #include "vfp.h"
  39. static struct vfp_single vfp_single_default_qnan = {
  40. .exponent = 255,
  41. .sign = 0,
  42. .significand = VFP_SINGLE_SIGNIFICAND_QNAN,
  43. };
  44. static void vfp_single_dump(const char *str, struct vfp_single *s)
  45. {
  46. pr_debug("VFP: %s: sign=%d exponent=%d significand=%08x\n",
  47. str, s->sign != 0, s->exponent, s->significand);
  48. }
  49. static void vfp_single_normalise_denormal(struct vfp_single *vs)
  50. {
  51. int bits = 31 - fls(vs->significand);
  52. vfp_single_dump("normalise_denormal: in", vs);
  53. if (bits) {
  54. vs->exponent -= bits - 1;
  55. vs->significand <<= bits;
  56. }
  57. vfp_single_dump("normalise_denormal: out", vs);
  58. }
  59. #ifndef DEBUG
  60. #define vfp_single_normaliseround(sd,vsd,fpscr,except,func) __vfp_single_normaliseround(sd,vsd,fpscr,except)
  61. u32 __vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions)
  62. #else
  63. u32 vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions, const char *func)
  64. #endif
  65. {
  66. u32 significand, incr, rmode;
  67. int exponent, shift, underflow;
  68. vfp_single_dump("pack: in", vs);
  69. /*
  70. * Infinities and NaNs are a special case.
  71. */
  72. if (vs->exponent == 255 && (vs->significand == 0 || exceptions))
  73. goto pack;
  74. /*
  75. * Special-case zero.
  76. */
  77. if (vs->significand == 0) {
  78. vs->exponent = 0;
  79. goto pack;
  80. }
  81. exponent = vs->exponent;
  82. significand = vs->significand;
  83. /*
  84. * Normalise first. Note that we shift the significand up to
  85. * bit 31, so we have VFP_SINGLE_LOW_BITS + 1 below the least
  86. * significant bit.
  87. */
  88. shift = 32 - fls(significand);
  89. if (shift < 32 && shift) {
  90. exponent -= shift;
  91. significand <<= shift;
  92. }
  93. #ifdef DEBUG
  94. vs->exponent = exponent;
  95. vs->significand = significand;
  96. vfp_single_dump("pack: normalised", vs);
  97. #endif
  98. /*
  99. * Tiny number?
  100. */
  101. underflow = exponent < 0;
  102. if (underflow) {
  103. significand = vfp_shiftright32jamming(significand, -exponent);
  104. exponent = 0;
  105. #ifdef DEBUG
  106. vs->exponent = exponent;
  107. vs->significand = significand;
  108. vfp_single_dump("pack: tiny number", vs);
  109. #endif
  110. if (!(significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1)))
  111. underflow = 0;
  112. }
  113. /*
  114. * Select rounding increment.
  115. */
  116. incr = 0;
  117. rmode = fpscr & FPSCR_RMODE_MASK;
  118. if (rmode == FPSCR_ROUND_NEAREST) {
  119. incr = 1 << VFP_SINGLE_LOW_BITS;
  120. if ((significand & (1 << (VFP_SINGLE_LOW_BITS + 1))) == 0)
  121. incr -= 1;
  122. } else if (rmode == FPSCR_ROUND_TOZERO) {
  123. incr = 0;
  124. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vs->sign != 0))
  125. incr = (1 << (VFP_SINGLE_LOW_BITS + 1)) - 1;
  126. pr_debug("VFP: rounding increment = 0x%08x\n", incr);
  127. /*
  128. * Is our rounding going to overflow?
  129. */
  130. if ((significand + incr) < significand) {
  131. exponent += 1;
  132. significand = (significand >> 1) | (significand & 1);
  133. incr >>= 1;
  134. #ifdef DEBUG
  135. vs->exponent = exponent;
  136. vs->significand = significand;
  137. vfp_single_dump("pack: overflow", vs);
  138. #endif
  139. }
  140. /*
  141. * If any of the low bits (which will be shifted out of the
  142. * number) are non-zero, the result is inexact.
  143. */
  144. if (significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1))
  145. exceptions |= FPSCR_IXC;
  146. /*
  147. * Do our rounding.
  148. */
  149. significand += incr;
  150. /*
  151. * Infinity?
  152. */
  153. if (exponent >= 254) {
  154. exceptions |= FPSCR_OFC | FPSCR_IXC;
  155. if (incr == 0) {
  156. vs->exponent = 253;
  157. vs->significand = 0x7fffffff;
  158. } else {
  159. vs->exponent = 255; /* infinity */
  160. vs->significand = 0;
  161. }
  162. } else {
  163. if (significand >> (VFP_SINGLE_LOW_BITS + 1) == 0)
  164. exponent = 0;
  165. if (exponent || significand > 0x80000000)
  166. underflow = 0;
  167. if (underflow)
  168. exceptions |= FPSCR_UFC;
  169. vs->exponent = exponent;
  170. vs->significand = significand >> 1;
  171. }
  172. pack:
  173. vfp_single_dump("pack: final", vs);
  174. {
  175. s32 d = vfp_single_pack(vs);
  176. #ifdef DEBUG
  177. pr_debug("VFP: %s: d(s%d)=%08x exceptions=%08x\n", func,
  178. sd, d, exceptions);
  179. #endif
  180. vfp_put_float(d, sd);
  181. }
  182. return exceptions;
  183. }
  184. /*
  185. * Propagate the NaN, setting exceptions if it is signalling.
  186. * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
  187. */
  188. static u32
  189. vfp_propagate_nan(struct vfp_single *vsd, struct vfp_single *vsn,
  190. struct vfp_single *vsm, u32 fpscr)
  191. {