softfloat-specialize 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. ===============================================================================
  3. This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
  4. Arithmetic Package, Release 2.
  5. Written by John R. Hauser. This work was made possible in part by the
  6. International Computer Science Institute, located at Suite 600, 1947 Center
  7. Street, Berkeley, California 94704. Funding was partially provided by the
  8. National Science Foundation under grant MIP-9311980. The original version
  9. of this code was written as part of a project to build a fixed-point vector
  10. processor in collaboration with the University of California at Berkeley,
  11. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  12. is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  13. arithmetic/softfloat.html'.
  14. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  15. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  16. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  17. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  18. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  19. Derivative works are acceptable, even for commercial purposes, so long as
  20. (1) they include prominent notice that the work is derivative, and (2) they
  21. include prominent notice akin to these three paragraphs for those parts of
  22. this code that are retained.
  23. ===============================================================================
  24. */
  25. /*
  26. -------------------------------------------------------------------------------
  27. Underflow tininess-detection mode, statically initialized to default value.
  28. (The declaration in `softfloat.h' must match the `int8' type here.)
  29. -------------------------------------------------------------------------------
  30. */
  31. int8 float_detect_tininess = float_tininess_after_rounding;
  32. /*
  33. -------------------------------------------------------------------------------
  34. Raises the exceptions specified by `flags'. Floating-point traps can be
  35. defined here if desired. It is currently not possible for such a trap to
  36. substitute a result value. If traps are not implemented, this routine
  37. should be simply `float_exception_flags |= flags;'.
  38. ScottB: November 4, 1998
  39. Moved this function out of softfloat-specialize into fpmodule.c.
  40. This effectively isolates all the changes required for integrating with the
  41. Linux kernel into fpmodule.c. Porting to NetBSD should only require modifying
  42. fpmodule.c to integrate with the NetBSD kernel (I hope!).
  43. -------------------------------------------------------------------------------
  44. void float_raise( int8 flags )
  45. {
  46. float_exception_flags |= flags;
  47. }
  48. */
  49. /*
  50. -------------------------------------------------------------------------------
  51. Internal canonical NaN format.
  52. -------------------------------------------------------------------------------
  53. */
  54. typedef struct {
  55. flag sign;
  56. bits64 high, low;
  57. } commonNaNT;
  58. /*
  59. -------------------------------------------------------------------------------
  60. The pattern for a default generated single-precision NaN.
  61. -------------------------------------------------------------------------------
  62. */
  63. #define float32_default_nan 0xFFFFFFFF
  64. /*
  65. -------------------------------------------------------------------------------
  66. Returns 1 if the single-precision floating-point value `a' is a NaN;
  67. otherwise returns 0.
  68. -------------------------------------------------------------------------------
  69. */
  70. flag float32_is_nan( float32 a )
  71. {
  72. return ( 0xFF000000 < (bits32) ( a<<1 ) );
  73. }
  74. /*
  75. -------------------------------------------------------------------------------
  76. Returns 1 if the single-precision floating-point value `a' is a signaling
  77. NaN; otherwise returns 0.
  78. -------------------------------------------------------------------------------
  79. */
  80. flag float32_is_signaling_nan( float32 a )
  81. {
  82. return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
  83. }
  84. /*
  85. -------------------------------------------------------------------------------
  86. Returns the result of converting the single-precision floating-point NaN
  87. `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  88. exception is raised.
  89. -------------------------------------------------------------------------------
  90. */
  91. static commonNaNT float32ToCommonNaN( float32 a )
  92. {
  93. commonNaNT z;
  94. if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
  95. z.sign = a>>31;
  96. z.low = 0;
  97. z.high = ( (bits64) a )<<41;
  98. return z;
  99. }
  100. /*
  101. -------------------------------------------------------------------------------
  102. Returns the result of converting the canonical NaN `a' to the single-
  103. precision floating-point format.
  104. -------------------------------------------------------------------------------
  105. */
  106. static float32 commonNaNToFloat32( commonNaNT a )
  107. {
  108. return ( ( (bits32) a.sign )<<31 ) | 0x7FC00000 | ( a.high>>41 );
  109. }
  110. /*
  111. -------------------------------------------------------------------------------
  112. Takes two single-precision floating-point values `a' and `b', one of which
  113. is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  114. signaling NaN, the invalid exception is raised.
  115. -------------------------------------------------------------------------------
  116. */
  117. static float32 propagateFloat32NaN( float32 a, float32 b )
  118. {
  119. flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
  120. aIsNaN = float32_is_nan( a );
  121. aIsSignalingNaN = float32_is_signaling_nan( a );
  122. bIsNaN = float32_is_nan( b );
  123. bIsSignalingNaN = float32_is_signaling_nan( b );
  124. a |= 0x00400000;
  125. b |= 0x00400000;
  126. if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
  127. if ( aIsNaN ) {
  128. return ( aIsSignalingNaN & bIsNaN ) ? b : a;
  129. }
  130. else {
  131. return b;
  132. }
  133. }
  134. /*
  135. -------------------------------------------------------------------------------
  136. The pattern for a default generated double-precision NaN.
  137. -------------------------------------------------------------------------------
  138. */
  139. #define float64_default_nan LIT64( 0xFFFFFFFFFFFFFFFF )
  140. /*
  141. -------------------------------------------------------------------------------
  142. Returns 1 if the double-precision floating-point value `a' is a NaN;
  143. otherwise returns 0.
  144. -------------------------------------------------------------------------------
  145. */
  146. flag float64_is_nan( float64 a )
  147. {
  148. return ( LIT64( 0xFFE0000000000000 ) < (bits64) ( a<<1 ) );
  149. }
  150. /*
  151. -------------------------------------------------------------------------------
  152. Returns 1 if the double-precision floating-point value `a' is a signaling
  153. NaN; otherwise returns 0.
  154. -------------------------------------------------------------------------------
  155. */
  156. flag float64_is_signaling_nan( float64 a )
  157. {
  158. return
  159. ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
  160. && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
  161. }
  162. /*
  163. -------------------------------------------------------------------------------
  164. Returns the result of converting the double-precision floating-point NaN
  165. `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  166. exception is raised.
  167. -------------------------------------------------------------------------------
  168. */
  169. static commonNaNT float64ToCommonNaN( float64 a )
  170. {
  171. commonNaNT z;
  172. if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
  173. z.sign = a>>63;
  174. z.low = 0;
  175. z.high = a<<12;
  176. return z;
  177. }
  178. /*
  179. -------------------------------------------------------------------------------
  180. Returns the result of converting the canonical NaN `a' to the double-
  181. precision floating-point format.
  182. -------------------------------------------------------------------------------
  183. */
  184. static float64 commonNaNToFloat64( commonNaNT a )
  185. {
  186. return
  187. ( ( (bits64) a.sign )<<63 )
  188. | LIT64( 0x7FF8000000000000 )
  189. | ( a.high>>12 );
  190. }
  191. /*
  192. -------------------------------------------------------------------------------
  193. Takes two double-precision floating-point values `a' and `b', one of which
  194. is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  195. signaling NaN, the invalid exception is raised.
  196. -------------------------------------------------------------------------------
  197. */
  198. static float64 propagateFloat64NaN( float64 a, float64 b )
  199. {
  200. flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
  201. aIsNaN = float64_is_nan( a );
  202. aIsSignalingNaN = float64_is_signaling_nan( a );
  203. bIsNaN = float64_is_nan( b );
  204. bIsSignalingNaN = float64_is_signaling_nan( b );
  205. a |= LIT64( 0x0008000000000000 );
  206. b |= LIT64( 0x0008000000000000 );
  207. if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
  208. if ( aIsNaN ) {
  209. return ( aIsSignalingNaN & bIsNaN ) ? b : a;
  210. }
  211. else {
  212. return b;
  213. }
  214. }
  215. #ifdef FLOATX80
  216. /*
  217. -------------------------------------------------------------------------------
  218. The pattern for a default generated extended double-precision NaN. The
  219. `high' and `low' values hold the most- and least-significant bits,
  220. respectively.
  221. -------------------------------------------------------------------------------
  222. */
  223. #define floatx80_default_nan_high 0xFFFF
  224. #define floatx80_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
  225. /*
  226. -------------------------------------------------------------------------------
  227. Returns 1 if the extended double-precision floating-point value `a' is a
  228. NaN; otherwise returns 0.
  229. -------------------------------------------------------------------------------
  230. */
  231. flag floatx80_is_nan( floatx80 a )
  232. {
  233. return ( ( a.high & 0x7FFF ) == 0x7FFF ) && (bits64) ( a.low<<1 );
  234. }
  235. /*
  236. -------------------------------------------------------------------------------
  237. Returns 1 if the extended double-precision floating-point value `a' is a
  238. signaling NaN; otherwise returns 0.
  239. -------------------------------------------------------------------------------
  240. */
  241. flag floatx80_is_signaling_nan( floatx80 a )
  242. {
  243. //register int lr;
  244. bits64 aLow;
  245. //__asm__("mov %0, lr" : : "g" (lr));
  246. //fp_printk("floatx80_is_signalling_nan() called from 0x%08x\n",lr);
  247. aLow = a.low & ~ LIT64( 0x4000000000000000 );
  248. return
  249. ( ( a.high & 0x7FFF ) == 0x7FFF )
  250. && (bits64) ( aLow<<1 )
  251. && ( a.low == aLow );
  252. }
  253. /*
  254. -------------------------------------------------------------------------------
  255. Returns the result of converting the extended double-precision floating-
  256. point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
  257. invalid exception is raised.
  258. -------------------------------------------------------------------------------
  259. */
  260. static commonNaNT floatx80ToCommonNaN( floatx80 a )
  261. {
  262. commonNaNT z;
  263. if ( floatx80_is_signaling_nan( a ) ) float_raise( float_flag_invalid );
  264. z.sign = a.high>>15;
  265. z.low = 0;
  266. z.high = a.low<<1;
  267. return z;
  268. }
  269. /*
  270. -------------------------------------------------------------------------------
  271. Returns the result of converting the canonical NaN `a' to the extended
  272. double-precision floating-point format.
  273. -------------------------------------------------------------------------------
  274. */
  275. static floatx80 commonNaNToFloatx80( commonNaNT a )
  276. {
  277. floatx80 z;
  278. z.low = LIT64( 0xC000000000000000 ) | ( a.high>>1 );
  279. z.high = ( ( (bits16) a.sign )<<15 ) | 0x7FFF;
  280. z.__padding = 0;
  281. return z;
  282. }
  283. /*
  284. -------------------------------------------------------------------------------
  285. Takes two extended double-precision floating-point values `a' and `b', one
  286. of which is a NaN, and returns the appropriate NaN result. If either `a' or
  287. `b' is a signaling NaN, the invalid exception is raised.
  288. -------------------------------------------------------------------------------
  289. */
  290. static floatx80 propagateFloatx80NaN( floatx80 a, floatx80 b )
  291. {
  292. flag aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN;
  293. aIsNaN = floatx80_is_nan( a );
  294. aIsSignalingNaN = floatx80_is_signaling_nan( a );
  295. bIsNaN = floatx80_is_nan( b );
  296. bIsSignalingNaN = floatx80_is_signaling_nan( b );
  297. a.low |= LIT64( 0xC000000000000000 );
  298. b.low |= LIT64( 0xC000000000000000 );
  299. if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid );
  300. if ( aIsNaN ) {
  301. return ( aIsSignalingNaN & bIsNaN ) ? b : a;
  302. }
  303. else {
  304. return b;
  305. }
  306. }
  307. #endif