Coverage Report

Created: 2026-10-31 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/tools/fdlibm/e_fmod.c
Line
Count
Source
1
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
13
/* 
14
 * __ieee754_fmod(x,y)
15
 * Return x mod y in exact arithmetic
16
 * Method: shift and subtract
17
 */
18
19
#include <float.h>
20
21
#include "math.h"
22
#include "math_private.h"
23
24
static const double one = 1.0, Zero[] = {0.0, -0.0,};
25
26
double
27
__ieee754_fmod(double x, double y)
28
22.0M
{
29
22.0M
  int32_t hx, hy, hz, ix, iy, n, sx;
30
22.0M
  u_int32_t lx, ly, lz;
31
32
22.0M
  EXTRACT_WORDS(hx,lx,x);
33
22.0M
  EXTRACT_WORDS(hy,ly,y);
34
22.0M
  sx = hx&0x80000000;   /* sign of x */
35
22.0M
  hx ^= sx;     /* |x| */
36
22.0M
  hy &= 0x7fffffff;   /* |y| */
37
38
    /* purge off exception values */
39
22.0M
  if((hy|ly)==0||(hx>=0x7ff00000)||  /* y=0,or x not finite */
40
15.4M
    ((hy|((ly|-ly)>>31))>0x7ff00000))  /* or y is NaN */
41
7.08M
      return nan_mix_op(x, y, *)/nan_mix_op(x, y, *);
42
14.9M
  if(hx<=hy) {
43
7.07M
      if((hx<hy)||(lx<ly)) return x;  /* |x|<|y| return x */
44
257k
      if(lx==ly) 
45
6.08k
    return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
46
257k
  }
47
48
    /* determine ix = ilogb(x) */
49
8.11M
  if(hx<0x00100000)
50
1.90k
      ix = subnormal_ilogb(hx, lx);
51
8.11M
  else
52
8.11M
      ix = (hx>>20)-1023;
53
54
    /* determine iy = ilogb(y) */
55
8.11M
  if(hy<0x00100000)
56
138k
      iy = subnormal_ilogb(hy, ly);
57
7.97M
  else
58
7.97M
      iy = (hy>>20)-1023;
59
60
    /* set up {hx,lx}, {hy,ly} and align y to x */
61
8.11M
  if(ix >= -1022) 
62
8.11M
      hx = 0x00100000|(0x000fffff&hx);
63
1.90k
  else {   /* subnormal x, shift x to normal */
64
1.90k
      n = -1022-ix;
65
1.90k
      if(n<=31) {
66
1.62k
          hx = (hx<<n)|(lx>>(32-n));
67
1.62k
          lx <<= n;
68
1.62k
      } else {
69
281
    hx = lx<<(n-32);
70
281
    lx = 0;
71
281
      }
72
1.90k
  }
73
8.11M
  if(iy >= -1022) 
74
7.97M
      hy = 0x00100000|(0x000fffff&hy);
75
138k
  else {   /* subnormal y, shift y to normal */
76
138k
      n = -1022-iy;
77
138k
      if(n<=31) {
78
135k
          hy = (hy<<n)|(ly>>(32-n));
79
135k
          ly <<= n;
80
135k
      } else {
81
3.87k
    hy = ly<<(n-32);
82
3.87k
    ly = 0;
83
3.87k
      }
84
138k
  }
85
86
    /* fix point fmod */
87
8.11M
  n = ix - iy;
88
343M
  while(n--) {
89
336M
      hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
90
336M
      if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
91
174M
      else {
92
174M
        if((hz|lz)==0)    /* return sign(x)*0 */
93
644k
        return Zero[(u_int32_t)sx>>31];
94
173M
        hx = hz+hz+(lz>>31); lx = lz+lz;
95
173M
      }
96
336M
  }
97
7.47M
  hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
98
7.47M
  if(hz>=0) {hx=hz;lx=lz;}
99
100
    /* convert back to floating value and restore the sign */
101
7.47M
  if((hx|lx)==0)      /* return sign(x)*0 */
102
891k
      return Zero[(u_int32_t)sx>>31];
103
29.9M
  while(hx<0x00100000) {   /* normalize x */
104
23.3M
      hx = hx+hx+(lx>>31); lx = lx+lx;
105
23.3M
      iy -= 1;
106
23.3M
  }
107
6.58M
  if(iy>= -1022) { /* normalize output */
108
6.44M
      hx = ((hx-0x00100000)|((iy+1023)<<20));
109
6.44M
      INSERT_WORDS(x,hx|sx,lx);
110
6.44M
  } else {   /* subnormal output */
111
138k
      n = -1022 - iy;
112
138k
      if(n<=20) {
113
133k
    lx = (lx>>n)|((u_int32_t)hx<<(32-n));
114
133k
    hx >>= n;
115
133k
      } else if (n<=31) {
116
1.71k
    lx = (hx<<(32-n))|(lx>>n); hx = sx;
117
3.87k
      } else {
118
3.87k
    lx = hx>>(n-32); hx = sx;
119
3.87k
      }
120
138k
      INSERT_WORDS(x,hx|sx,lx);
121
138k
      x *= one;   /* create necessary signal */
122
138k
  }
123
6.58M
  return x;   /* exact output */
124
7.47M
}