Skip to content

Commit 3763ac8

Browse files
ossy-szegedrerobika
authored andcommitted
Get rid of strict aliasing rule violations from libm (#3069)
JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác [email protected]
1 parent f7391a9 commit 3763ac8

File tree

16 files changed

+246
-197
lines changed

16 files changed

+246
-197
lines changed

jerry-libm/acos.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
double
7171
acos (double x)
7272
{
73-
double z, p, q, r, w, s, c, df;
73+
double z, p, q, r, w, s, c;
7474
int hx, ix;
7575

7676
hx = __HI (x);
@@ -114,16 +114,17 @@ acos (double x)
114114
}
115115
else /* x > 0.5 */
116116
{
117+
double_accessor df;
117118
z = (one - x) * 0.5;
118119
s = sqrt (z);
119-
df = s;
120-
__LO (df) = 0;
121-
c = (z - df * df) / (s + df);
120+
df.dbl = s;
121+
df.as_int.lo = 0;
122+
c = (z - df.dbl * df.dbl) / (s + df.dbl);
122123
p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
123124
q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
124125
r = p / q;
125126
w = r * s + c;
126-
return 2.0 * (df + w);
127+
return 2.0 * (df.dbl + w);
127128
}
128129
} /* acos */
129130

jerry-libm/asin.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
double
7878
asin (double x)
7979
{
80-
double t, w, p, q, c, r, s;
80+
double t, p, q, c, r, s;
81+
double_accessor w;
8182
int hx, ix;
8283

8384
hx = __HI (x);
@@ -102,28 +103,28 @@ asin (double x)
102103
t = x * x;
103104
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
104105
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
105-
w = p / q;
106-
return x + x * w;
106+
w.dbl = p / q;
107+
return x + x * w.dbl;
107108
}
108109
/* 1 > |x| >= 0.5 */
109-
w = one - fabs (x);
110-
t = w * 0.5;
110+
w.dbl = one - fabs (x);
111+
t = w.dbl * 0.5;
111112
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
112113
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
113114
s = sqrt (t);
114115
if (ix >= 0x3FEF3333) /* if |x| > 0.975 */
115116
{
116-
w = p / q;
117-
t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
117+
w.dbl = p / q;
118+
t = pio2_hi - (2.0 * (s + s * w.dbl) - pio2_lo);
118119
}
119120
else
120121
{
121-
w = s;
122-
__LO (w) = 0;
123-
c = (t - w * w) / (s + w);
122+
w.dbl = s;
123+
w.as_int.lo = 0;
124+
c = (t - w.dbl * w.dbl) / (s + w.dbl);
124125
r = p / q;
125126
p = 2.0 * s * r - (pio2_lo - 2.0 * c);
126-
q = pio4_hi - 2.0 * w;
127+
q = pio4_hi - 2.0 * w.dbl;
127128
t = pio4_hi - (p - q);
128129
}
129130
if (hx > 0)

jerry-libm/atan2.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
double
6565
atan2 (double y, double x)
6666
{
67-
double z;
67+
double_accessor z;
6868
int k, m, hx, hy, ix, iy;
6969
unsigned lx, ly;
7070

@@ -168,35 +168,35 @@ atan2 (double y, double x)
168168
k = (iy - ix) >> 20;
169169
if (k > 60) /* |y / x| > 2**60 */
170170
{
171-
z = pi_o_2 + 0.5 * pi_lo;
171+
z.dbl = pi_o_2 + 0.5 * pi_lo;
172172
}
173173
else if (hx < 0 && k < -60) /* |y| / x < -2**60 */
174174
{
175-
z = 0.0;
175+
z.dbl = 0.0;
176176
}
177177
else /* safe to do y / x */
178178
{
179-
z = atan (fabs (y / x));
179+
z.dbl = atan (fabs (y / x));
180180
}
181181
switch (m)
182182
{
183183
case 0: /* atan(+,+) */
184184
{
185-
return z;
185+
return z.dbl;
186186
}
187187
case 1: /* atan(-,+) */
188188
{
189-
__HI (z) ^= 0x80000000;
190-
return z;
189+
z.as_int.hi ^= 0x80000000;
190+
return z.dbl;
191191
}
192192
case 2: /* atan(+,-) */
193193
{
194-
return pi - (z - pi_lo);
194+
return pi - (z.dbl - pi_lo);
195195
}
196196
/* case 3: */
197197
default: /* atan(-,-) */
198198
{
199-
return (z - pi_lo) - pi;
199+
return (z.dbl - pi_lo) - pi;
200200
}
201201
}
202202
} /* atan2 */

jerry-libm/ceil.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ ceil (double x)
123123
i1 &= (~i);
124124
}
125125
}
126-
__HI (x) = i0;
127-
__LO (x) = i1;
128-
return x;
126+
127+
double_accessor ret;
128+
ret.as_int.hi = i0;
129+
ret.as_int.lo = i1;
130+
return ret.dbl;
129131
} /* ceil */
130132

131133
#undef huge

jerry-libm/copysign.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
double
3535
copysign (double x, double y)
3636
{
37-
__HI (x) = (__HI (x) & 0x7fffffff) | (__HI (y) & 0x80000000);
38-
return x;
37+
double_accessor ret;
38+
ret.dbl = x;
39+
ret.as_int.hi = (__HI (x) & 0x7fffffff) | (__HI (y) & 0x80000000);
40+
return ret.dbl;
3941
} /* copysign */

jerry-libm/exp.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static const double ln2LO[2] =
120120
double
121121
exp (double x) /* default IEEE double exp */
122122
{
123-
double y, hi, lo, c, t;
123+
double hi, lo, c, t;
124124
int k = 0, xsb;
125125
unsigned hx;
126126

@@ -182,6 +182,8 @@ exp (double x) /* default IEEE double exp */
182182
k = 0;
183183
}
184184

185+
double_accessor ret;
186+
185187
/* x is now in primary range */
186188
t = x * x;
187189
c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
@@ -191,17 +193,17 @@ exp (double x) /* default IEEE double exp */
191193
}
192194
else
193195
{
194-
y = one - ((lo - (x * c) / (2.0 - c)) - hi);
196+
ret.dbl = one - ((lo - (x * c) / (2.0 - c)) - hi);
195197
}
196198
if (k >= -1021)
197199
{
198-
__HI (y) += (k << 20); /* add k to y's exponent */
199-
return y;
200+
ret.as_int.hi += (k << 20); /* add k to y's exponent */
201+
return ret.dbl;
200202
}
201203
else
202204
{
203-
__HI (y) += ((k + 1000) << 20); /* add k to y's exponent */
204-
return y * twom1000;
205+
ret.as_int.hi += ((k + 1000) << 20); /* add k to y's exponent */
206+
return ret.dbl * twom1000;
205207
}
206208
} /* exp */
207209

jerry-libm/fabs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
double
3434
fabs (double x)
3535
{
36-
__HI (x) &= 0x7fffffff;
37-
return x;
36+
double_accessor ret;
37+
ret.dbl = x;
38+
ret.as_int.hi &= 0x7fffffff;
39+
return ret.dbl;
3840
} /* fabs */

jerry-libm/floor.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ floor (double x)
122122
i1 &= (~i);
123123
}
124124
}
125-
__HI (x) = i0;
126-
__LO (x) = i1;
127-
return x;
125+
126+
double_accessor ret;
127+
ret.as_int.hi = i0;
128+
ret.as_int.lo = i1;
129+
return ret.dbl;
128130
} /* floor */
129131

130132
#undef huge

jerry-libm/fmod.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
static const double Zero[] = { 0.0, -0.0, };
3737

38-
#define one 1.0
39-
4038
double
4139
fmod (double x, double y)
4240
{
@@ -201,11 +199,13 @@ fmod (double x, double y)
201199
lx = lx + lx;
202200
iy -= 1;
203201
}
202+
203+
double_accessor ret;
204204
if (iy >= -1022) /* normalize output */
205205
{
206206
hx = ((hx - 0x00100000) | ((iy + 1023) << 20));
207-
__HI (x) = hx | sx;
208-
__LO (x) = lx;
207+
ret.as_int.hi = hx | sx;
208+
ret.as_int.lo = lx;
209209
}
210210
else /* subnormal output */
211211
{
@@ -225,11 +225,8 @@ fmod (double x, double y)
225225
lx = hx >> (n - 32);
226226
hx = sx;
227227
}
228-
__HI (x) = hx | sx;
229-
__LO (x) = lx;
230-
x *= one; /* create necessary signal */
228+
ret.as_int.hi = hx | sx;
229+
ret.as_int.lo = lx;
231230
}
232-
return x; /* exact output */
231+
return ret.dbl; /* exact output */
233232
} /* fmod */
234-
235-
#undef one

jerry-libm/jerry-libm-internal.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,36 @@
4848
#endif /* !__LITTLE_ENDIAN */
4949

5050
#ifdef __LITTLE_ENDIAN
51-
#define __HI(x) *(1 + (int *) &x)
52-
#define __LO(x) *(int *) &x
51+
#define __HI(x) *(1 + (const int *) &x)
52+
#define __LO(x) *(const int *) &x
53+
typedef union
54+
{
55+
double dbl;
56+
struct
57+
{
58+
int lo;
59+
int hi;
60+
} as_int;
61+
} double_accessor;
5362
#else /* !__LITTLE_ENDIAN */
54-
#define __HI(x) *(int *) &x
55-
#define __LO(x) *(1 + (int *) &x)
63+
#define __HI(x) *(const int *) &x
64+
#define __LO(x) *(1 + (const int *) &x)
65+
66+
typedef union
67+
{
68+
double dbl;
69+
struct
70+
{
71+
int hi;
72+
int lo;
73+
} as_int;
74+
} double_accessor;
5675
#endif /* __LITTLE_ENDIAN */
5776

77+
#ifndef NAN
78+
#define NAN (0.0/0.0)
79+
#endif
80+
5881
/*
5982
* ANSI/POSIX
6083
*/

0 commit comments

Comments
 (0)