Coverage Report

Created: 2026-05-16 07:11

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