memoryOperation.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * linux/arch/arm/vfp/vfpdouble.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_double vfp_double_default_qnan = {
  40. .exponent = 2047,
  41. .sign = 0,
  42. .significand = VFP_DOUBLE_SIGNIFICAND_QNAN,
  43. };
  44. static void vfp_double_dump(const char *str, struct vfp_double *d)
  45. {
  46. pr_debug("VFP: %s: sign=%d exponent=%d significand=%016llx\n",
  47. str, d->sign != 0, d->exponent, d->significand);
  48. }
  49. static void vfp_double_normalise_denormal(struct vfp_double *vd)
  50. {
  51. int bits = 31 - fls(vd->significand >> 32);
  52. if (bits == 31)
  53. bits = 63 - fls(vd->significand);
  54. vfp_double_dump("normalise_denormal: in", vd);
  55. if (bits) {
  56. vd->exponent -= bits - 1;
  57. vd->significand <<= bits;
  58. }
  59. vfp_double_dump("normalise_denormal: out", vd);
  60. }
  61. u32 vfp_double_normaliseround(int dd, struct vfp_double *vd, u32 fpscr, u32 exceptions, const char *func)
  62. {
  63. u64 significand, incr;
  64. int exponent, shift, underflow;
  65. u32 rmode;
  66. vfp_double_dump("pack: in", vd);
  67. /*
  68. * Infinities and NaNs are a special case.
  69. */
  70. if (vd->exponent == 2047 && (vd->significand == 0 || exceptions))
  71. goto pack;
  72. /*
  73. * Special-case zero.
  74. */
  75. if (vd->significand == 0) {
  76. vd->exponent = 0;
  77. goto pack;
  78. }
  79. exponent = vd->exponent;
  80. significand = vd->significand;
  81. shift = 32 - fls(significand >> 32);
  82. if (shift == 32)
  83. shift = 64 - fls(significand);
  84. if (shift) {
  85. exponent -= shift;
  86. significand <<= shift;
  87. }
  88. #ifdef DEBUG
  89. vd->exponent = exponent;
  90. vd->significand = significand;
  91. vfp_double_dump("pack: normalised", vd);
  92. #endif
  93. /*
  94. * Tiny number?
  95. */
  96. underflow = exponent < 0;
  97. if (underflow) {
  98. significand = vfp_shiftright64jamming(significand, -exponent);
  99. exponent = 0;
  100. #ifdef DEBUG
  101. vd->exponent = exponent;
  102. vd->significand = significand;
  103. vfp_double_dump("pack: tiny number", vd);
  104. #endif
  105. if (!(significand & ((1ULL << (VFP_DOUBLE_LOW_BITS + 1)) - 1)))
  106. underflow = 0;
  107. }
  108. /*
  109. * Select rounding increment.
  110. */
  111. incr = 0;
  112. rmode = fpscr & FPSCR_RMODE_MASK;
  113. if (rmode == FPSCR_ROUND_NEAREST) {
  114. incr = 1ULL << VFP_DOUBLE_LOW_BITS;
  115. if ((significand & (1ULL << (VFP_DOUBLE_LOW_BITS + 1))) == 0)
  116. incr -= 1;
  117. } else if (rmode == FPSCR_ROUND_TOZERO) {
  118. incr = 0;
  119. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vd->sign != 0))
  120. incr = (1ULL << (VFP_DOUBLE_LOW_BITS + 1)) - 1;
  121. pr_debug("VFP: rounding increment = 0x%08llx\n", incr);
  122. /*
  123. * Is our rounding going to overflow?
  124. */
  125. if ((significand + incr) < significand) {
  126. exponent += 1;
  127. significand = (significand >> 1) | (significand & 1);
  128. incr >>= 1;
  129. #ifdef DEBUG
  130. vd->exponent = exponent;
  131. vd->significand = significand;
  132. vfp_double_dump("pack: overflow", vd);
  133. #endif
  134. }
  135. /*
  136. * If any of the low bits (which will be shifted out of the
  137. * number) are non-zero, the result is inexact.
  138. */
  139. if (significand & ((1 << (VFP_DOUBLE_LOW_BITS + 1)) - 1))
  140. exceptions |= FPSCR_IXC;
  141. /*
  142. * Do our rounding.
  143. */
  144. significand += incr;
  145. /*
  146. * Infinity?
  147. */
  148. if (exponent >= 2046) {
  149. exceptions |= FPSCR_OFC | FPSCR_IXC;
  150. if (incr == 0) {
  151. vd->exponent = 2045;
  152. vd->significand = 0x7fffffffffffffffULL;
  153. } else {
  154. vd->exponent = 2047; /* infinity */
  155. vd->significand = 0;
  156. }
  157. } else {
  158. if (significand >> (VFP_DOUBLE_LOW_BITS + 1) == 0)
  159. exponent = 0;
  160. if (exponent || significand > 0x8000000000000000ULL)
  161. underflow = 0;
  162. if (underflow)
  163. exceptions |= FPSCR_UFC;
  164. vd->exponent = exponent;
  165. vd->significand = significand >> 1;
  166. }
  167. pack:
  168. vfp_double_dump("pack: final", vd);
  169. {
  170. s64 d = vfp_double_pack(vd);
  171. pr_debug("VFP: %s: d(d%d)=%016llx exceptions=%08x\n", func,
  172. dd, d, exceptions);
  173. vfp_put_double(d, dd);
  174. }
  175. return exceptions;
  176. }
  177. /*
  178. * Propagate the NaN, setting exceptions if it is signalling.
  179. * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
  180. */
  181. static u32
  182. vfp_propagate_nan(struct vfp_double *vdd, struct vfp_double *vdn,
  183. struct vfp_double *vdm, u32 fpscr)
  184. {
  185. struct vfp_double *nan;
  186. int tn, tm = 0;
  187. tn = vfp_double_type(vdn);
  188. if (vdm)
  189. tm = vfp_double_type(vdm);
  190. if (fpscr & FPSCR_DEFAULT_NAN)
  191. /*
  192. * Default NaN mode - always returns a quiet NaN
  193. */
  194. nan = &vfp_double_default_qnan;
  195. else {
  196. /*
  197. * Contemporary mode - select the first signalling
  198. * NAN, or if neither are signalling, the first
  199. * quiet NAN.
  200. */
  201. if (tn == VFP_SNAN || (tm != VFP_SNAN && tn == VFP_QNAN))
  202. nan = vdn;
  203. else
  204. nan = vdm;
  205. /*
  206. * Make the NaN quiet.
  207. */
  208. nan->significand |= VFP_DOUBLE_SIGNIFICAND_QNAN;
  209. }
  210. *vdd = *nan;
  211. /*
  212. * If one was a signalling NAN, raise invalid operation.
  213. */
  214. return tn == VFP_SNAN || tm == VFP_SNAN ? FPSCR_IOC : VFP_NAN_FLAG;
  215. }
  216. /*
  217. * Extended operations
  218. */
  219. static u32 vfp_double_fabs(int dd, int unused, int dm, u32 fpscr)
  220. {
  221. vfp_put_double(vfp_double_packed_abs(vfp_get_double(dm)), dd);
  222. return 0;
  223. }
  224. static u32 vfp_double_fcpy(int dd, int unused, int dm, u32 fpscr)