e_fmodl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* @(#)e_fmod.c 1.3 95/01/18 */
  2. /*-
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/e_fmodl.c,v 1.2 2008/07/31 20:09:47 das Exp $");
  14. #include <float.h>
  15. #include <stdint.h>
  16. #include "fpmath.h"
  17. #include "openlibm.h"
  18. #include "math_private.h"
  19. #define BIAS (LDBL_MAX_EXP - 1)
  20. #if LDBL_MANL_SIZE > 32
  21. typedef u_int64_t manl_t;
  22. #else
  23. typedef u_int32_t manl_t;
  24. #endif
  25. #if LDBL_MANH_SIZE > 32
  26. typedef u_int64_t manh_t;
  27. #else
  28. typedef u_int32_t manh_t;
  29. #endif
  30. /*
  31. * These macros add and remove an explicit integer bit in front of the
  32. * fractional mantissa, if the architecture doesn't have such a bit by
  33. * default already.
  34. */
  35. #ifdef LDBL_IMPLICIT_NBIT
  36. #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
  37. #define HFRAC_BITS LDBL_MANH_SIZE
  38. #else
  39. #define SET_NBIT(hx) (hx)
  40. #define HFRAC_BITS (LDBL_MANH_SIZE - 1)
  41. #endif
  42. #define MANL_SHIFT (LDBL_MANL_SIZE - 1)
  43. static const long double one = 1.0, Zero[] = {0.0, -0.0,};
  44. /*
  45. * fmodl(x,y)
  46. * Return x mod y in exact arithmetic
  47. * Method: shift and subtract
  48. *
  49. * Assumptions:
  50. * - The low part of the mantissa fits in a manl_t exactly.
  51. * - The high part of the mantissa fits in an int64_t with enough room
  52. * for an explicit integer bit in front of the fractional bits.
  53. */
  54. DLLEXPORT long double
  55. fmodl(long double x, long double y)
  56. {
  57. union IEEEl2bits ux, uy;
  58. int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
  59. manh_t hy;
  60. manl_t lx,ly,lz;
  61. int ix,iy,n,sx;
  62. ux.e = x;
  63. uy.e = y;
  64. sx = ux.bits.sign;
  65. /* purge off exception values */
  66. if((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
  67. (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
  68. (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
  69. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
  70. return (x*y)/(x*y);
  71. if(ux.bits.exp<=uy.bits.exp) {
  72. if((ux.bits.exp<uy.bits.exp) ||
  73. (ux.bits.manh<=uy.bits.manh &&
  74. (ux.bits.manh<uy.bits.manh ||
  75. ux.bits.manl<uy.bits.manl))) {
  76. return x; /* |x|<|y| return x or x-y */
  77. }
  78. if(ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl) {
  79. return Zero[sx]; /* |x|=|y| return x*0*/
  80. }
  81. }
  82. /* determine ix = ilogb(x) */
  83. if(ux.bits.exp == 0) { /* subnormal x */
  84. ux.e *= 0x1.0p512;
  85. ix = ux.bits.exp - (BIAS + 512);
  86. } else {
  87. ix = ux.bits.exp - BIAS;
  88. }
  89. /* determine iy = ilogb(y) */
  90. if(uy.bits.exp == 0) { /* subnormal y */
  91. uy.e *= 0x1.0p512;
  92. iy = uy.bits.exp - (BIAS + 512);
  93. } else {
  94. iy = uy.bits.exp - BIAS;
  95. }
  96. /* set up {hx,lx}, {hy,ly} and align y to x */
  97. hx = SET_NBIT(ux.bits.manh);
  98. hy = SET_NBIT(uy.bits.manh);
  99. lx = ux.bits.manl;
  100. ly = uy.bits.manl;
  101. /* fix point fmod */
  102. n = ix - iy;
  103. while(n--) {
  104. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  105. if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
  106. else {
  107. if ((hz|lz)==0) /* return sign(x)*0 */
  108. return Zero[sx];
  109. hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
  110. }
  111. }
  112. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  113. if(hz>=0) {hx=hz;lx=lz;}
  114. /* convert back to floating value and restore the sign */
  115. if((hx|lx)==0) /* return sign(x)*0 */
  116. return Zero[sx];
  117. while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */
  118. hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
  119. iy -= 1;
  120. }
  121. ux.bits.manh = hx; /* The mantissa is truncated here if needed. */
  122. ux.bits.manl = lx;
  123. if (iy < LDBL_MIN_EXP) {
  124. ux.bits.exp = iy + (BIAS + 512);
  125. ux.e *= 0x1p-512;
  126. } else {
  127. ux.bits.exp = iy + BIAS;
  128. }
  129. x = ux.e * one; /* create necessary signal */
  130. return x; /* exact output */
  131. }