home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / qfloat / qfloor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-17  |  1.2 KB  |  116 lines

  1.  
  2. /* qfloor - largest integer not greater than x
  3.  * qround - nearest integer to x
  4.  */
  5.  
  6. #include "mconf.h"
  7. #include "qhead.h"
  8. #ifdef DEC
  9. #define void int
  10. #endif
  11.  
  12. extern short qone[], qhalf[];
  13.  
  14. static short bmask[] = {
  15. 0xffff,
  16. 0xfffe,
  17. 0xfffc,
  18. 0xfff8,
  19. 0xfff0,
  20. 0xffe0,
  21. 0xffc0,
  22. 0xff80,
  23. 0xff00,
  24. 0xfe00,
  25. 0xfc00,
  26. 0xf800,
  27. 0xf000,
  28. 0xe000,
  29. 0xc000,
  30. 0x8000,
  31. 0x0000,
  32. };
  33.  
  34.  
  35. void qfloor( x, y )
  36. short x[], y[];
  37. {
  38. short t[NQ];
  39. register unsigned short *p;
  40. short e, i;
  41.  
  42. qmov( x, t );
  43. if( t[1] == 0 )
  44.     {
  45.     qclear( y );
  46.     return;
  47.     }
  48. e = t[1] - 0x4000;
  49.  
  50. if( e <= 0 )
  51.     {
  52.     if( t[0] < 0 )
  53.         {
  54.         qmov( qone, y );
  55.         qneg( y );
  56.         }
  57.     else
  58.         {
  59.         qclear( y );
  60.         }
  61.     return;
  62.     }
  63.  
  64. /* number of bits to clear out */
  65. e = NBITS - e;
  66.  
  67. qmov( t, y );
  68. p = (unsigned short *)&y[NQ-1];
  69.  
  70. while( e >= 16 )
  71.     {
  72.     *p-- = 0;
  73.     e -= 16;
  74.     }
  75.  
  76. /* clear the remaining bits */
  77. *p &= bmask[e];
  78.  
  79. /* truncate negatives toward minus infinity */
  80. if( t[0] < 0 )
  81.     {
  82.     if( qcmp( t, y ) != 0 )
  83.         qsub( qone, y, y );
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. double qround(x, y)
  92. short *x, *y;
  93. {
  94. short z[NQ], f[NQ];
  95. int r;
  96.  
  97. qfloor( x, z );
  98. qsub( z, x, f );
  99. r = qcmp( f, qhalf );
  100. if( r > 0 )
  101.     goto rndup;
  102. if( r == 0 )
  103.     {
  104. /* round to even */
  105.     z[1] -= 1;
  106.     qfloor( z, f );
  107.     z[1] += 1;
  108.     f[1] += 1;
  109.     if( qcmp(z,f) != 0 )
  110.         {
  111. rndup:        qadd( qone, z, z );
  112.         }
  113.     }
  114. qmov( z, y );
  115. }
  116.