Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: fusl/src/math/fmod.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/math/fminl.c ('k') | fusl/src/math/fmodf.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <math.h>
2 #include <stdint.h>
3
4 double fmod(double x, double y)
5 {
6 union {double f; uint64_t i;} ux = {x}, uy = {y};
7 int ex = ux.i>>52 & 0x7ff;
8 int ey = uy.i>>52 & 0x7ff;
9 int sx = ux.i>>63;
10 uint64_t i;
11
12 /* in the followings uxi should be ux.i, but then gcc wrongly adds */
13 /* float load/store to inner loops ruining performance and code size */
14 uint64_t uxi = ux.i;
15
16 if (uy.i<<1 == 0 || isnan(y) || ex == 0x7ff)
17 return (x*y)/(x*y);
18 if (uxi<<1 <= uy.i<<1) {
19 if (uxi<<1 == uy.i<<1)
20 return 0*x;
21 return x;
22 }
23
24 /* normalize x and y */
25 if (!ex) {
26 for (i = uxi<<12; i>>63 == 0; ex--, i <<= 1);
27 uxi <<= -ex + 1;
28 } else {
29 uxi &= -1ULL >> 12;
30 uxi |= 1ULL << 52;
31 }
32 if (!ey) {
33 for (i = uy.i<<12; i>>63 == 0; ey--, i <<= 1);
34 uy.i <<= -ey + 1;
35 } else {
36 uy.i &= -1ULL >> 12;
37 uy.i |= 1ULL << 52;
38 }
39
40 /* x mod y */
41 for (; ex > ey; ex--) {
42 i = uxi - uy.i;
43 if (i >> 63 == 0) {
44 if (i == 0)
45 return 0*x;
46 uxi = i;
47 }
48 uxi <<= 1;
49 }
50 i = uxi - uy.i;
51 if (i >> 63 == 0) {
52 if (i == 0)
53 return 0*x;
54 uxi = i;
55 }
56 for (; uxi>>52 == 0; uxi <<= 1, ex--);
57
58 /* scale result */
59 if (ex > 0) {
60 uxi -= 1ULL << 52;
61 uxi |= (uint64_t)ex << 52;
62 } else {
63 uxi >>= -ex + 1;
64 }
65 uxi |= (uint64_t)sx << 63;
66 ux.i = uxi;
67 return ux.f;
68 }
OLDNEW
« no previous file with comments | « fusl/src/math/fminl.c ('k') | fusl/src/math/fmodf.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698