rndint.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 rint, nearbyint,
  8. ** rinttol, round, roundtol, trunc, modf and modfl. This file
  9. ** targets PowerPC or Power platforms.
  10. **
  11. ** Written by: A. Sazegari, Apple AltiVec Group
  12. ** Created originally by Jon Okada, Apple Numerics Group
  13. **
  14. ** Copyright: © 1992-2001 by Apple Computer, Inc., all rights reserved
  15. **
  16. ** Change History (most recent first):
  17. **
  18. ** 13 Jul 01 ram replaced --setflm calls with inline assembly
  19. ** 03 Mar 01 ali first port to os x using gcc, added the crucial __setflm
  20. ** definition.
  21. ** 1. removed double_t, put in double for now.
  22. ** 2. removed iclass from nearbyint.
  23. ** 3. removed wrong comments intrunc.
  24. ** 4.
  25. ** 13 May 97 ali made performance improvements in rint, rinttol, roundtol
  26. ** and trunc by folding some of the taligent ideas into this
  27. ** implementation. nearbyint is faster than the one in taligent,
  28. ** rint is more elegant, but slower by %30 than the taligent one.
  29. ** 09 Apr 97 ali deleted modfl and deferred to AuxiliaryDD.c
  30. ** 15 Sep 94 ali Major overhaul and performance improvements of all functions.
  31. ** 20 Jul 94 PAF New faster version
  32. ** 16 Jul 93 ali Added the modfl function.
  33. ** 18 Feb 93 ali Changed the return value of fenv functions
  34. ** feclearexcept and feraiseexcept to their new
  35. ** NCEG X3J11.1/93-001 definitions.
  36. ** 16 Dec 92 JPO Removed __itrunc implementation to a
  37. ** separate file.
  38. ** 15 Dec 92 JPO Added __itrunc implementation and modified
  39. ** rinttol to include conversion from double
  40. ** to long int format. Modified roundtol to
  41. ** call __itrunc.
  42. ** 10 Dec 92 JPO Added modf (double) implementation.
  43. ** 04 Dec 92 JPO First created.
  44. **
  45. *******************************************************************************/
  46. #include <limits.h>
  47. #include <math.h>
  48. #if !defined(__ppc__)
  49. #define asm(x)
  50. #endif
  51. #define SET_INVALID 0x01000000UL
  52. typedef union
  53. {
  54. struct {
  55. #if defined(__BIG_ENDIAN__)
  56. unsigned long int hi;
  57. unsigned long int lo;
  58. #else
  59. unsigned long int lo;
  60. unsigned long int hi;
  61. #endif
  62. } words;
  63. double dbl;
  64. } DblInHex;
  65. static const unsigned long int signMask = 0x80000000ul;
  66. static const double twoTo52 = 4503599627370496.0;
  67. static const double doubleToLong = 4503603922337792.0; // 2^52
  68. static const DblInHex Huge = {{ 0x7FF00000, 0x00000000 }};
  69. static const DblInHex TOWARDZERO = {{ 0x00000000, 0x00000001 }};
  70. /*******************************************************************************
  71. * *
  72. * The function rint rounds its double argument to integral value *
  73. * according to the current rounding direction and returns the result in *
  74. * double format. This function signals inexact if an ordered return *
  75. * value is not equal to the operand. *
  76. * *
  77. ********************************************************************************
  78. * *
  79. * This function calls: fabs. *
  80. * *
  81. *******************************************************************************/
  82. /*******************************************************************************
  83. * First, an elegant implementation. *
  84. ********************************************************************************
  85. *
  86. *double rint ( double x )
  87. * {
  88. * double y;
  89. *
  90. * y = twoTo52.fval;
  91. *
  92. * if ( fabs ( x ) >= y ) // huge case is exact
  93. * return x;
  94. * if ( x < 0 ) y = -y; // negative case
  95. * y = ( x + y ) - y; // force rounding
  96. * if ( y == 0.0 ) // zero results mirror sign of x
  97. * y = copysign ( y, x );
  98. * return ( y );
  99. * }
  100. ********************************************************************************
  101. * Now a bit twidling version that is about %30 faster. *
  102. *******************************************************************************/
  103. #if defined(__ppc__)
  104. double rint ( double x )
  105. {
  106. DblInHex argument;
  107. register double y;
  108. unsigned long int xHead;
  109. register long int target;
  110. argument.dbl = x;
  111. xHead = argument.words.hi & 0x7fffffffUL; // xHead <- high half of |x|
  112. target = ( argument.words.hi < signMask ); // flags positive sign
  113. if ( xHead < 0x43300000ul )
  114. /*******************************************************************************
  115. * Is |x| < 2.0^52? *
  116. *******************************************************************************/
  117. {
  118. if ( xHead < 0x3ff00000ul )
  119. /*******************************************************************************
  120. * Is |x| < 1.0? *
  121. *******************************************************************************/
  122. {
  123. if ( target )
  124. y = ( x + twoTo52 ) - twoTo52; // round at binary point
  125. else
  126. y = ( x - twoTo52 ) + twoTo52; // round at binary point
  127. if ( y == 0.0 )
  128. { // fix sign of zero result
  129. if ( target )
  130. return ( 0.0 );
  131. else
  132. return ( -0.0 );
  133. }
  134. return y;
  135. }
  136. /*******************************************************************************
  137. * Is 1.0 < |x| < 2.0^52? *
  138. *******************************************************************************/
  139. if ( target )
  140. return ( ( x + twoTo52 ) - twoTo52 ); // round at binary pt.
  141. else
  142. return ( ( x - twoTo52 ) + twoTo52 );
  143. }
  144. /*******************************************************************************
  145. * |x| >= 2.0^52 or x is a NaN. *
  146. *******************************************************************************/
  147. return ( x );
  148. }
  149. #endif /* __ppc__ */
  150. /*******************************************************************************
  151. * *
  152. * The function nearbyint rounds its double argument to integral value *
  153. * according to the current rounding direction and returns the result in *
  154. * double format. This function does not signal inexact. *
  155. * *
  156. ********************************************************************************
  157. * *
  158. * This function calls fabs and copysign. *
  159. * *
  160. *******************************************************************************/
  161. double nearbyint ( double x )
  162. {
  163. double y, OldEnvironment;
  164. y = twoTo52;
  165. asm ("mffs %0" : "=f" (OldEnvironment)); /* get the environement */
  166. if ( fabs ( x ) >= y ) /* huge case is exact */
  167. return x;
  168. if ( x < 0 ) y = -y; /* negative case */
  169. y = ( x + y ) - y; /* force rounding */
  170. if ( y == 0.0 ) /* zero results mirror sign of x */
  171. y = copysign ( y, x );
  172. // restore old flags
  173. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment ));
  174. return ( y );
  175. }
  176. /*******************************************************************************
  177. * *
  178. * The function rinttol converts its double argument to integral value *
  179. * according to the current rounding direction and returns the result in *
  180. * long int format. This conversion signals invalid if the argument is a *
  181. * NaN or the rounded intermediate result is out of range of the *
  182. * destination long int format, and it delivers an unspecified result in *
  183. * this case. This function signals inexact if the rounded result is *
  184. * within range of the long int format but unequal to the operand. *
  185. * *
  186. *******************************************************************************/
  187. long int rinttol ( double x )
  188. {
  189. register double y;
  190. DblInHex argument, OldEnvironment;
  191. unsigned long int xHead;
  192. register long int target;
  193. argument.dbl = x;
  194. target = ( argument.words.hi < signMask ); // flag positive sign
  195. xHead = argument.words.hi & 0x7ffffffful; // high 32 bits of x
  196. if ( target )
  197. /*******************************************************************************
  198. * Sign of x is positive. *
  199. *******************************************************************************/
  200. {
  201. if ( xHead < 0x41dffffful )
  202. { // x is safely in long range
  203. y = ( x + twoTo52 ) - twoTo52; // round at binary point
  204. argument.dbl = y + doubleToLong; // force result into argument.words.lo
  205. return ( ( long ) argument.words.lo );
  206. }
  207. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  208. if ( xHead > 0x41dffffful )
  209. { // x is safely out of long range
  210. OldEnvironment.words.lo |= SET_INVALID;
  211. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  212. return ( LONG_MAX );
  213. }
  214. /*******************************************************************************
  215. * x > 0.0 and may or may not be out of range of long. *
  216. *******************************************************************************/
  217. y = ( x + twoTo52 ) - twoTo52; // do rounding
  218. if ( y > ( double ) LONG_MAX )
  219. { // out of range of long
  220. OldEnvironment.words.lo |= SET_INVALID;
  221. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  222. return ( LONG_MAX );
  223. }
  224. argument.dbl = y + doubleToLong; // in range
  225. return ( ( long ) argument.words.lo ); // return result & flags
  226. }
  227. /*******************************************************************************
  228. * Sign of x is negative. *
  229. *******************************************************************************/
  230. if ( xHead < 0x41e00000ul )
  231. { // x is safely in long range
  232. y = ( x - twoTo52 ) + twoTo52;
  233. argument.dbl = y + doubleToLong;
  234. return ( ( long ) argument.words.lo );
  235. }
  236. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  237. if ( xHead > 0x41e00000ul )
  238. { // x is safely out of long range
  239. OldEnvironment.words.lo |= SET_INVALID;
  240. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  241. return ( LONG_MIN );
  242. }
  243. /*******************************************************************************
  244. * x < 0.0 and may or may not be out of range of long. *
  245. *******************************************************************************/
  246. y = ( x - twoTo52 ) + twoTo52; // do rounding
  247. if ( y < ( double ) LONG_MIN )
  248. { // out of range of long
  249. OldEnvironment.words.lo |= SET_INVALID;
  250. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  251. return ( LONG_MIN );
  252. }
  253. argument.dbl = y + doubleToLong; // in range
  254. return ( ( long ) argument.words.lo ); // return result & flags
  255. }
  256. /*******************************************************************************
  257. * *
  258. * The function round rounds its double argument to integral value *
  259. * according to the "add half to the magnitude and truncate" rounding of *
  260. * Pascal's Round function and FORTRAN's ANINT function and returns the *
  261. * result in double format. This function signals inexact if an ordered *
  262. * return value is not equal to the operand. *
  263. * *
  264. *******************************************************************************/
  265. double round ( double x )
  266. {
  267. DblInHex argument, OldEnvironment;
  268. register double y, z;
  269. register unsigned long int xHead;
  270. register long int target;
  271. argument.dbl = x;
  272. xHead = argument.words.hi & 0x7fffffffUL; // xHead <- high half of |x|
  273. target = ( argument.words.hi < signMask ); // flag positive sign
  274. if ( xHead < 0x43300000ul )
  275. /*******************************************************************************
  276. * Is |x| < 2.0^52? *
  277. *******************************************************************************/
  278. {
  279. if ( xHead < 0x3ff00000ul )
  280. /*******************************************************************************
  281. * Is |x| < 1.0? *
  282. *******************************************************************************/
  283. {
  284. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  285. if ( xHead < 0x3fe00000ul )
  286. /*******************************************************************************
  287. * Is |x| < 0.5? *
  288. *******************************************************************************/
  289. {
  290. if ( ( xHead | argument.words.lo ) != 0ul )
  291. OldEnvironment.words.lo |= 0x02000000ul;
  292. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  293. if ( target )
  294. return ( 0.0 );
  295. else
  296. return ( -0.0 );
  297. }
  298. /*******************************************************************************
  299. * Is 0.5 ² |x| < 1.0? *
  300. *******************************************************************************/
  301. OldEnvironment.words.lo |= 0x02000000ul;
  302. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  303. if ( target )
  304. return ( 1.0 );
  305. else
  306. return ( -1.0 );
  307. }
  308. /*******************************************************************************
  309. * Is 1.0 < |x| < 2.0^52? *
  310. *******************************************************************************/
  311. if ( target )
  312. { // positive x
  313. y = ( x + twoTo52 ) - twoTo52; // round at binary point
  314. if ( y == x ) // exact case
  315. return ( x );
  316. z = x + 0.5; // inexact case
  317. y = ( z + twoTo52 ) - twoTo52; // round at binary point
  318. if ( y > z )
  319. return ( y - 1.0 );
  320. else
  321. return ( y );
  322. }
  323. /*******************************************************************************
  324. * Is x < 0? *
  325. *******************************************************************************/
  326. else
  327. {
  328. y = ( x - twoTo52 ) + twoTo52; // round at binary point
  329. if ( y == x )
  330. return ( x );
  331. z = x - 0.5;
  332. y = ( z - twoTo52 ) + twoTo52; // round at binary point
  333. if ( y < z )
  334. return ( y + 1.0 );
  335. else
  336. return ( y );
  337. }
  338. }
  339. /*******************************************************************************
  340. * |x| >= 2.0^52 or x is a NaN. *
  341. *******************************************************************************/
  342. return ( x );
  343. }
  344. /*******************************************************************************
  345. * *
  346. * The function roundtol converts its double argument to integral format *
  347. * according to the "add half to the magnitude and chop" rounding mode of *
  348. * Pascal's Round function and FORTRAN's NINT function. This conversion *
  349. * signals invalid if the argument is a NaN or the rounded intermediate *
  350. * result is out of range of the destination long int format, and it *
  351. * delivers an unspecified result in this case. This function signals *
  352. * inexact if the rounded result is within range of the long int format but *
  353. * unequal to the operand. *
  354. * *
  355. *******************************************************************************/
  356. long int roundtol ( double x )
  357. {
  358. register double y, z;
  359. DblInHex argument, OldEnvironment;
  360. register unsigned long int xhi;
  361. register long int target;
  362. const DblInHex kTZ = {{ 0x0, 0x1 }};
  363. const DblInHex kUP = {{ 0x0, 0x2 }};
  364. argument.dbl = x;
  365. xhi = argument.words.hi & 0x7ffffffful; // high 32 bits of x
  366. target = ( argument.words.hi < signMask ); // flag positive sign
  367. if ( xhi > 0x41e00000ul )
  368. /*******************************************************************************
  369. * Is x is out of long range or NaN? *
  370. *******************************************************************************/
  371. {
  372. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // get environment
  373. OldEnvironment.words.lo |= SET_INVALID;
  374. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  375. if ( target ) // pin result
  376. return ( LONG_MAX );
  377. else
  378. return ( LONG_MIN );
  379. }
  380. if ( target )
  381. /*******************************************************************************
  382. * Is sign of x is "+"? *
  383. *******************************************************************************/
  384. {
  385. if ( x < 2147483647.5 )
  386. /*******************************************************************************
  387. * x is in the range of a long. *
  388. *******************************************************************************/
  389. {
  390. y = ( x + doubleToLong ) - doubleToLong; // round at binary point
  391. if ( y != x )
  392. { // inexact case
  393. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // save environment
  394. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( kTZ.dbl )); // truncate rounding
  395. z = x + 0.5; // truncate x + 0.5
  396. argument.dbl = z + doubleToLong;
  397. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  398. return ( ( long ) argument.words.lo );
  399. }
  400. argument.dbl = y + doubleToLong; // force result into argument.words.lo
  401. return ( ( long ) argument.words.lo ); // return long result
  402. }
  403. /*******************************************************************************
  404. * Rounded positive x is out of the range of a long. *
  405. *******************************************************************************/
  406. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  407. OldEnvironment.words.lo |= SET_INVALID;
  408. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  409. return ( LONG_MAX ); // return pinned result
  410. }
  411. /*******************************************************************************
  412. * x < 0.0 and may or may not be out of the range of a long. *
  413. *******************************************************************************/
  414. if ( x > -2147483648.5 )
  415. /*******************************************************************************
  416. * x is in the range of a long. *
  417. *******************************************************************************/
  418. {
  419. y = ( x + doubleToLong ) - doubleToLong; // round at binary point
  420. if ( y != x )
  421. { // inexact case
  422. asm ("mffs %0" : "=f" (OldEnvironment.dbl)); // save environment
  423. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( kUP.dbl )); // round up
  424. z = x - 0.5; // truncate x - 0.5
  425. argument.dbl = z + doubleToLong;
  426. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  427. return ( ( long ) argument.words.lo );
  428. }
  429. argument.dbl = y + doubleToLong;
  430. return ( ( long ) argument.words.lo ); // return long result
  431. }
  432. /*******************************************************************************
  433. * Rounded negative x is out of the range of a long. *
  434. *******************************************************************************/
  435. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  436. OldEnvironment.words.lo |= SET_INVALID;
  437. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  438. return ( LONG_MIN ); // return pinned result
  439. }
  440. /*******************************************************************************
  441. * *
  442. * The function trunc truncates its double argument to integral value *
  443. * and returns the result in double format. This function signals *
  444. * inexact if an ordered return value is not equal to the operand. *
  445. * *
  446. *******************************************************************************/
  447. double trunc ( double x )
  448. {
  449. DblInHex argument,OldEnvironment;
  450. register double y;
  451. register unsigned long int xhi;
  452. register long int target;
  453. argument.dbl = x;
  454. xhi = argument.words.hi & 0x7fffffffUL; // xhi <- high half of |x|
  455. target = ( argument.words.hi < signMask ); // flag positive sign
  456. if ( xhi < 0x43300000ul )
  457. /*******************************************************************************
  458. * Is |x| < 2.0^53? *
  459. *******************************************************************************/
  460. {
  461. if ( xhi < 0x3ff00000ul )
  462. /*******************************************************************************
  463. * Is |x| < 1.0? *
  464. *******************************************************************************/
  465. {
  466. if ( ( xhi | argument.words.lo ) != 0ul )
  467. { // raise deserved INEXACT
  468. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  469. OldEnvironment.words.lo |= 0x02000000ul;
  470. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  471. }
  472. if ( target ) // return properly signed zero
  473. return ( 0.0 );
  474. else
  475. return ( -0.0 );
  476. }
  477. /*******************************************************************************
  478. * Is 1.0 < |x| < 2.0^52? *
  479. *******************************************************************************/
  480. if ( target )
  481. {
  482. y = ( x + twoTo52 ) - twoTo52; // round at binary point
  483. if ( y > x )
  484. return ( y - 1.0 );
  485. else
  486. return ( y );
  487. }
  488. else
  489. {
  490. y = ( x - twoTo52 ) + twoTo52; // round at binary point.
  491. if ( y < x )
  492. return ( y + 1.0 );
  493. else
  494. return ( y );
  495. }
  496. }
  497. /*******************************************************************************
  498. * Is |x| >= 2.0^52 or x is a NaN. *
  499. *******************************************************************************/
  500. return ( x );
  501. }
  502. /*******************************************************************************
  503. * The modf family of functions separate a floating-point number into its *
  504. * fractional and integral parts, returning the fractional part and writing *
  505. * the integral part in floating-point format to the object pointed to by a *
  506. * pointer argument. If the input argument is integral or infinite in *
  507. * value, the return value is a zero with the sign of the input argument. *
  508. * The modf family of functions raises no floating-point exceptions. older *
  509. * implemenation set the INVALID flag due to signaling NaN input. *
  510. * *
  511. *******************************************************************************/
  512. /*******************************************************************************
  513. * modf is the double implementation. *
  514. *******************************************************************************/
  515. #if defined(__ppc__)
  516. double modf ( double x, double *iptr )
  517. {
  518. register double OldEnvironment, xtrunc;
  519. register unsigned long int xHead, signBit;
  520. DblInHex argument;
  521. argument.dbl = x;
  522. xHead = argument.words.hi & 0x7ffffffful; // |x| high bit pattern
  523. signBit = ( argument.words.hi & 0x80000000ul ); // isolate sign bit
  524. if (xHead == 0x7ff81fe0)
  525. signBit = signBit | 0;
  526. if ( xHead < 0x43300000ul )
  527. /*******************************************************************************
  528. * Is |x| < 2.0^53? *
  529. *******************************************************************************/
  530. {
  531. if ( xHead < 0x3ff00000ul )
  532. /*******************************************************************************
  533. * Is |x| < 1.0? *
  534. *******************************************************************************/
  535. {
  536. argument.words.hi = signBit; // truncate to zero
  537. argument.words.lo = 0ul;
  538. *iptr = argument.dbl;
  539. return ( x );
  540. }
  541. /*******************************************************************************
  542. * Is 1.0 < |x| < 2.0^52? *
  543. *******************************************************************************/
  544. asm ("mffs %0" : "=f" (OldEnvironment)); // save environment
  545. // round toward zero
  546. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( TOWARDZERO.dbl ));
  547. if ( signBit == 0ul ) // truncate to integer
  548. xtrunc = ( x + twoTo52 ) - twoTo52;
  549. else
  550. xtrunc = ( x - twoTo52 ) + twoTo52;
  551. // restore caller's env
  552. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment ));
  553. *iptr = xtrunc; // store integral part
  554. if ( x != xtrunc ) // nonzero fraction
  555. return ( x - xtrunc );
  556. else
  557. { // zero with x's sign
  558. argument.words.hi = signBit;
  559. argument.words.lo = 0ul;
  560. return ( argument.dbl );
  561. }
  562. }
  563. *iptr = x; // x is integral or NaN
  564. if ( x != x ) // NaN is returned
  565. return x;
  566. else
  567. { // zero with x's sign
  568. argument.words.hi = signBit;
  569. argument.words.lo = 0ul;
  570. return ( argument.dbl );
  571. }
  572. }
  573. #endif /* __ppc__ */