Coverage Report

Created: 2025-11-15 06:49

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
11.3M
{
29
11.3M
  int32_t hx, hy, hz, ix, iy, n, sx;
30
11.3M
  u_int32_t lx, ly, lz;
31
32
11.3M
  EXTRACT_WORDS(hx,lx,x);
33
11.3M
  EXTRACT_WORDS(hy,ly,y);
34
11.3M
  sx = hx&0x80000000;   /* sign of x */
35
11.3M
  hx ^= sx;     /* |x| */
36
11.3M
  hy &= 0x7fffffff;   /* |y| */
37
38
    /* purge off exception values */
39
11.3M
  if((hy|ly)==0||(hx>=0x7ff00000)||  /* y=0,or x not finite */
40
10.5M
    ((hy|((ly|-ly)>>31))>0x7ff00000))  /* or y is NaN */
41
796k
      return nan_mix_op(x, y, *)/nan_mix_op(x, y, *);
42
10.5M
  if(hx<=hy) {
43
3.48M
      if((hx<hy)||(lx<ly)) return x;  /* |x|<|y| return x */
44
3.14k
      if(lx==ly) 
45
3.01k
    return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
46
3.14k
  }
47
48
    /* determine ix = ilogb(x) */
49
7.03M
  if(hx<0x00100000)
50
0
      ix = subnormal_ilogb(hx, lx);
51
7.03M
  else
52
7.03M
      ix = (hx>>20)-1023;
53
54
    /* determine iy = ilogb(y) */
55
7.03M
  if(hy<0x00100000)
56
20
      iy = subnormal_ilogb(hy, ly);
57
7.03M
  else
58
7.03M
      iy = (hy>>20)-1023;
59
60
    /* set up {hx,lx}, {hy,ly} and align y to x */
61
7.03M
  if(ix >= -1022) 
62
7.03M
      hx = 0x00100000|(0x000fffff&hx);
63
0
  else {   /* subnormal x, shift x to normal */
64
0
      n = -1022-ix;
65
0
      if(n<=31) {
66
0
          hx = (hx<<n)|(lx>>(32-n));
67
0
          lx <<= n;
68
0
      } else {
69
0
    hx = lx<<(n-32);
70
0
    lx = 0;
71
0
      }
72
0
  }
73
7.03M
  if(iy >= -1022) 
74
7.03M
      hy = 0x00100000|(0x000fffff&hy);
75
20
  else {   /* subnormal y, shift y to normal */
76
20
      n = -1022-iy;
77
20
      if(n<=31) {
78
15
          hy = (hy<<n)|(ly>>(32-n));
79
15
          ly <<= n;
80
15
      } else {
81
5
    hy = ly<<(n-32);
82
5
    ly = 0;
83
5
      }
84
20
  }
85
86
    /* fix point fmod */
87
7.03M
  n = ix - iy;
88
183M
  while(n--) {
89
178M
      hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
90
178M
      if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
91
90.2M
      else {
92
90.2M
        if((hz|lz)==0)    /* return sign(x)*0 */
93
1.56M
        return Zero[(u_int32_t)sx>>31];
94
88.7M
        hx = hz+hz+(lz>>31); lx = lz+lz;
95
88.7M
      }
96
178M
  }
97
5.46M
  hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
98
5.46M
  if(hz>=0) {hx=hz;lx=lz;}
99
100
    /* convert back to floating value and restore the sign */
101
5.46M
  if((hx|lx)==0)      /* return sign(x)*0 */
102
638k
      return Zero[(u_int32_t)sx>>31];
103
8.90M
  while(hx<0x00100000) {   /* normalize x */
104
4.07M
      hx = hx+hx+(lx>>31); lx = lx+lx;
105
4.07M
      iy -= 1;
106
4.07M
  }
107
4.82M
  if(iy>= -1022) { /* normalize output */
108
4.82M
      hx = ((hx-0x00100000)|((iy+1023)<<20));
109
4.82M
      INSERT_WORDS(x,hx|sx,lx);
110
4.82M
  } else {   /* subnormal output */
111
20
      n = -1022 - iy;
112
20
      if(n<=20) {
113
13
    lx = (lx>>n)|((u_int32_t)hx<<(32-n));
114
13
    hx >>= n;
115
13
      } else if (n<=31) {
116
3
    lx = (hx<<(32-n))|(lx>>n); hx = sx;
117
4
      } else {
118
4
    lx = hx>>(n-32); hx = sx;
119
4
      }
120
20
      INSERT_WORDS(x,hx|sx,lx);
121
20
      x *= one;   /* create necessary signal */
122
20
  }
123
4.82M
  return x;   /* exact output */
124
5.46M
}