s_modf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*******************************************************************************
  2. ** File: rndint.c
  3. **
  4. ** Contains: C source code for implementations of floating-point
  5. ** functions which round to integral value or format, as
  6. ** defined in header <fp.h>. In particular, this file
  7. ** contains implementations of functions rinttol, roundtol,
  8. ** modf and modfl. This file targets PowrPC or Power platforms.
  9. **
  10. ** Written by: A. Sazegari, Apple AltiVec Group
  11. ** Created originally by Jon Okada, Apple Numerics Group
  12. **
  13. ** Copyright: © 1992-2001 by Apple Computer, Inc., all rights reserved
  14. **
  15. ** Change History (most recent first):
  16. **
  17. ** 13 Jul 01 ram replaced --setflm calls with inline assembly
  18. ** 03 Mar 01 ali first port to os x using gcc, added the crucial __setflm
  19. ** definition.
  20. ** 1. removed double_t, put in double for now.
  21. ** 2. removed iclass from nearbyint.
  22. ** 3. removed wrong comments intrunc.
  23. ** 4.
  24. ** 13 May 97 ali made performance improvements in rint, rinttol, roundtol
  25. ** and trunc by folding some of the taligent ideas into this
  26. ** implementation. nearbyint is faster than the one in taligent,
  27. ** rint is more elegant, but slower by %30 than the taligent one.
  28. ** 09 Apr 97 ali deleted modfl and deferred to AuxiliaryDD.c
  29. ** 15 Sep 94 ali Major overhaul and performance improvements of all functions.
  30. ** 20 Jul 94 PAF New faster version
  31. ** 16 Jul 93 ali Added the modfl function.
  32. ** 18 Feb 93 ali Changed the return value of fenv functions
  33. ** feclearexcept and feraiseexcept to their new
  34. ** NCEG X3J11.1/93-001 definitions.
  35. ** 16 Dec 92 JPO Removed __itrunc implementation to a
  36. ** separate file.
  37. ** 15 Dec 92 JPO Added __itrunc implementation and modified
  38. ** rinttol to include conversion from double
  39. ** to long int format. Modified roundtol to
  40. ** call __itrunc.
  41. ** 10 Dec 92 JPO Added modf (double) implementation.
  42. ** 04 Dec 92 JPO First created.
  43. **
  44. *******************************************************************************/
  45. #include <limits.h>
  46. #include <math.h>
  47. #define SET_INVALID 0x01000000UL
  48. typedef union
  49. {
  50. struct {
  51. #if defined(__BIG_ENDIAN__)
  52. unsigned long int hi;
  53. unsigned long int lo;
  54. #else
  55. unsigned long int lo;
  56. unsigned long int hi;
  57. #endif
  58. } words;
  59. double dbl;
  60. } DblInHex;
  61. static const unsigned long int signMask = 0x80000000ul;
  62. static const double twoTo52 = 4503599627370496.0;
  63. static const double doubleToLong = 4503603922337792.0; // 2^52
  64. static const DblInHex TOWARDZERO = {{ 0x00000000, 0x00000001 }};
  65. /*******************************************************************************
  66. * *
  67. * The function rinttol converts its double argument to integral value *
  68. * according to the current rounding direction and returns the result in *
  69. * long int format. This conversion signals invalid if the argument is a *
  70. * NaN or the rounded intermediate result is out of range of the *
  71. * destination long int format, and it delivers an unspecified result in *
  72. * this case. This function signals inexact if the rounded result is *
  73. * within range of the long int format but unequal to the operand. *
  74. * *
  75. *******************************************************************************/
  76. long int rinttol ( double x )
  77. {
  78. register double y;
  79. DblInHex argument, OldEnvironment;
  80. unsigned long int xHead;
  81. register long int target;
  82. argument.dbl = x;
  83. target = ( argument.words.hi < signMask ); // flag positive sign
  84. xHead = argument.words.hi & 0x7ffffffful; // high 32 bits of x
  85. if ( target )
  86. /*******************************************************************************
  87. * Sign of x is positive. *
  88. *******************************************************************************/
  89. {
  90. if ( xHead < 0x41dffffful )
  91. { // x is safely in long range
  92. y = ( x + twoTo52 ) - twoTo52; // round at binary point
  93. argument.dbl = y + doubleToLong; // force result into argument.words.lo
  94. return ( ( long ) argument.words.lo );
  95. }
  96. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  97. if ( xHead > 0x41dffffful )
  98. { // x is safely out of long range
  99. OldEnvironment.words.lo |= SET_INVALID;
  100. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  101. return ( LONG_MAX );
  102. }
  103. /*******************************************************************************
  104. * x > 0.0 and may or may not be out of range of long. *
  105. *******************************************************************************/
  106. y = ( x + twoTo52 ) - twoTo52; // do rounding
  107. if ( y > ( double ) LONG_MAX )
  108. { // out of range of long
  109. OldEnvironment.words.lo |= SET_INVALID;
  110. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  111. return ( LONG_MAX );
  112. }
  113. argument.dbl = y + doubleToLong; // in range
  114. return ( ( long ) argument.words.lo ); // return result & flags
  115. }
  116. /*******************************************************************************
  117. * Sign of x is negative. *
  118. *******************************************************************************/
  119. if ( xHead < 0x41e00000ul )
  120. { // x is safely in long range
  121. y = ( x - twoTo52 ) + twoTo52;
  122. argument.dbl = y + doubleToLong;
  123. return ( ( long ) argument.words.lo );
  124. }
  125. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  126. if ( xHead > 0x41e00000ul )
  127. { // x is safely out of long range
  128. OldEnvironment.words.lo |= SET_INVALID;
  129. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  130. return ( LONG_MIN );
  131. }
  132. /*******************************************************************************
  133. * x < 0.0 and may or may not be out of range of long. *
  134. *******************************************************************************/
  135. y = ( x - twoTo52 ) + twoTo52; // do rounding
  136. if ( y < ( double ) LONG_MIN )
  137. { // out of range of long
  138. OldEnvironment.words.lo |= SET_INVALID;
  139. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  140. return ( LONG_MIN );
  141. }
  142. argument.dbl = y + doubleToLong; // in range
  143. return ( ( long ) argument.words.lo ); // return result & flags
  144. }
  145. /*******************************************************************************
  146. * *
  147. * The function roundtol converts its double argument to integral format *
  148. * according to the "add half to the magnitude and chop" rounding mode of *
  149. * Pascal's Round function and FORTRAN's NINT function. This conversion *
  150. * signals invalid if the argument is a NaN or the rounded intermediate *
  151. * result is out of range of the destination long int format, and it *
  152. * delivers an unspecified result in this case. This function signals *
  153. * inexact if the rounded result is within range of the long int format but *
  154. * unequal to the operand. *
  155. * *
  156. *******************************************************************************/
  157. long int roundtol ( double x )
  158. {
  159. register double y, z;
  160. DblInHex argument, OldEnvironment;
  161. register unsigned long int xhi;
  162. register long int target;
  163. const DblInHex kTZ = {{ 0x0, 0x1 }};
  164. const DblInHex kUP = {{ 0x0, 0x2 }};
  165. argument.dbl = x;
  166. xhi = argument.words.hi & 0x7ffffffful; // high 32 bits of x
  167. target = ( argument.words.hi < signMask ); // flag positive sign
  168. if ( xhi > 0x41e00000ul )
  169. /*******************************************************************************
  170. * Is x is out of long range or NaN? *
  171. *******************************************************************************/
  172. {
  173. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  174. OldEnvironment.words.lo |= SET_INVALID;
  175. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  176. if ( target ) // pin result
  177. return ( LONG_MAX );
  178. else
  179. return ( LONG_MIN );
  180. }
  181. if ( target )
  182. /*******************************************************************************
  183. * Is sign of x is "+"? *
  184. *******************************************************************************/
  185. {
  186. if ( x < 2147483647.5 )
  187. /*******************************************************************************
  188. * x is in the range of a long. *
  189. *******************************************************************************/
  190. {
  191. y = ( x + doubleToLong ) - doubleToLong; // round at binary point
  192. if ( y != x )
  193. { // inexact case
  194. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // save environment
  195. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( kTZ.dbl )); // truncate rounding
  196. z = x + 0.5; // truncate x + 0.5
  197. argument.dbl = z + doubleToLong;
  198. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  199. return ( ( long ) argument.words.lo );
  200. }
  201. argument.dbl = y + doubleToLong; // force result into argument.words.lo
  202. return ( ( long ) argument.words.lo ); // return long result
  203. }
  204. /*******************************************************************************
  205. * Rounded positive x is out of the range of a long. *
  206. *******************************************************************************/
  207. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  208. OldEnvironment.words.lo |= SET_INVALID;
  209. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  210. return ( LONG_MAX ); // return pinned result
  211. }
  212. /*******************************************************************************
  213. * x < 0.0 and may or may not be out of the range of a long. *
  214. *******************************************************************************/
  215. if ( x > -2147483648.5 )
  216. /*******************************************************************************
  217. * x is in the range of a long. *
  218. *******************************************************************************/
  219. {
  220. y = ( x + doubleToLong ) - doubleToLong; // round at binary point
  221. if ( y != x )
  222. { // inexact case
  223. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // save environment
  224. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( kUP.dbl )); // round up
  225. z = x - 0.5; // truncate x - 0.5
  226. argument.dbl = z + doubleToLong;
  227. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  228. return ( ( long ) argument.words.lo );
  229. }
  230. argument.dbl = y + doubleToLong;
  231. return ( ( long ) argument.words.lo ); // return long result
  232. }
  233. /*******************************************************************************
  234. * Rounded negative x is out of the range of a long. *
  235. *******************************************************************************/
  236. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  237. OldEnvironment.words.lo |= SET_INVALID;
  238. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  239. return ( LONG_MIN ); // return pinned result
  240. }
  241. /*******************************************************************************
  242. * The modf family of functions separate a floating-point number into its *
  243. * fractional and integral parts, returning the fractional part and writing *
  244. * the integral part in floating-point format to the object pointed to by a *
  245. * pointer argument. If the input argument is integral or infinite in *
  246. * value, the return value is a zero with the sign of the input argument. *
  247. * The modf family of functions raises no floating-point exceptions. older *
  248. * implemenation set the INVALID flag due to signaling NaN input. *
  249. * *
  250. *******************************************************************************/
  251. /*******************************************************************************
  252. * modf is the double implementation. *
  253. *******************************************************************************/
  254. double modf ( double x, double *iptr )
  255. {
  256. register double OldEnvironment, xtrunc;
  257. register unsigned long int xHead, signBit;
  258. DblInHex argument;
  259. argument.dbl = x;
  260. xHead = argument.words.hi & 0x7ffffffful; // |x| high bit pattern
  261. signBit = ( argument.words.hi & 0x80000000ul ); // isolate sign bit
  262. if (xHead == 0x7ff81fe0)
  263. signBit = signBit | 0;
  264. if ( xHead < 0x43300000ul )
  265. /*******************************************************************************
  266. * Is |x| < 2.0^53? *
  267. *******************************************************************************/
  268. {
  269. if ( xHead < 0x3ff00000ul )
  270. /*******************************************************************************
  271. * Is |x| < 1.0? *
  272. *******************************************************************************/
  273. {
  274. argument.words.hi = signBit; // truncate to zero
  275. argument.words.lo = 0ul;
  276. *iptr = argument.dbl;
  277. return ( x );
  278. }
  279. /*******************************************************************************
  280. * Is 1.0 < |x| < 2.0^52? *
  281. *******************************************************************************/
  282. asm ("mffs %0" : "=f" (OldEnvironment)); // save environment
  283. // round toward zero
  284. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( TOWARDZERO.dbl ));
  285. if ( signBit == 0ul ) // truncate to integer
  286. xtrunc = ( x + twoTo52 ) - twoTo52;
  287. else
  288. xtrunc = ( x - twoTo52 ) + twoTo52;
  289. // restore caller's env
  290. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment ));
  291. *iptr = xtrunc; // store integral part
  292. if ( x != xtrunc ) // nonzero fraction
  293. return ( x - xtrunc );
  294. else
  295. { // zero with x's sign
  296. argument.words.hi = signBit;
  297. argument.words.lo = 0ul;
  298. return ( argument.dbl );
  299. }
  300. }
  301. *iptr = x; // x is integral or NaN
  302. if ( x != x ) // NaN is returned
  303. return x;
  304. else
  305. { // zero with x's sign
  306. argument.words.hi = signBit;
  307. argument.words.lo = 0ul;
  308. return ( argument.dbl );
  309. }
  310. }