Skip to content

Commit 6d4d2a4

Browse files
committed
Added tests for math functions and fixed bugs
1 parent 7bd7231 commit 6d4d2a4

File tree

16 files changed

+353
-32
lines changed

16 files changed

+353
-32
lines changed

src/libc/erfcf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @remarks Minimum ulp:
88
* ulp of +16 at +0x1.a13834p-1 with ideal expf (x < 1.0f)
9-
* ulp of +594482 at +0x1.251634p+3 with ideal expf (x >= 1.0f)
9+
* ulp of +594482 at +0x1.251634p+3 with current expf (x >= 1.0f)
1010
*/
1111
float erfcf(float x)
1212
{

src/libc/erfcl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @remarks Minimum ulp:
88
* ulp of -516 at +2.373300261e+01 with ideal expl
99
*/
10-
long double _erfcl_c(long double arg) {
10+
long double erfcl(long double arg) {
1111
long double x, x_sqr;
1212
long double t0, t1, t2, t3, t4, t5;
1313
long double ret;

src/libc/expf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ float _expf_c(float arg) {
4949
if ( arg == 0.0f){
5050
return 1.0f;
5151
}
52-
if ( arg < -exp_min_arg ){
52+
if ( arg < exp_min_arg ){
5353
return 0.0f;
5454
}
5555
if ( arg > exp_max_arg ){

src/libc/include/math.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern "C" {
2727
#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
2828
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
2929
#define M_SQRT1_2 0.707106781186547524401 /* 1/sqrt(2) */
30-
// /* deprecated */ #define M_LOG_2M_PI 1.83787706640934548 /* log(2*M_PI) */
3130

3231
#define FP_ILOGB0 (~__INT_MAX__)
3332
#define FP_ILOGBNAN __INT_MAX__

src/libc/include/tgmath.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <math.h>
1111

12-
1312
#define __tgmath_promote(x) _Generic((x), \
1413
float: ((float)0.f), \
1514
default: ((double)0.), \
@@ -378,8 +377,6 @@
378377
float: __hypot3f \
379378
)((x), (y), (z))
380379

381-
#define __tgmath_hypot2(x,y)2
382-
#define __tgmath_hypot3(x,y,z)3
383380
#define __tgmath_hypot_count(_1,_2,_3,count,...)count
384381
#define __tgmath_JOIN2(X,Y)X##Y
385382
#define __tgmath_CONCAT(X,Y)__tgmath_JOIN2(X,Y)

src/libc/isnormall.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ __isnormall:
2020
ld d, $40
2121
; test for zero/subnormal
2222
sbc hl, de
23-
jr c, .exp_all_zero
23+
; jr c, .exp_all_zero
2424

2525
.exp_all_ones:
2626
.exp_all_zero:

src/libc/lgammaf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ float lgammaf(float x) { /* the natural logarithm of the Gamma function. */
4242
*/
4343
const int maximum_iter = 36 + N;
4444
for (int iter = 0; iter < maximum_iter; iter++) {
45-
if (x < (float)N) {
45+
if (!(x < (float)N)) {
4646
break;
4747
}
4848
v *= x;
@@ -53,7 +53,7 @@ float lgammaf(float x) { /* the natural logarithm of the Gamma function. */
5353
+ (B12 / (12.0f * 11.0f))) * w + (B10 / (10.0f * 9.0f))) * w
5454
+ (B8 / ( 8.0f * 7.0f))) * w + (B6 / ( 6.0f * 5.0f))) * w
5555
+ (B4 / ( 4.0f * 3.0f))) * w + (B2 / ( 2.0f * 1.0f))) / x
56-
+ ln_pi_div_2 - logf(v) - x + (x - 0.5f) * logf(x);
56+
+ ln_pi_div_2 - logf(fabsf(v)) - x + (x - 0.5f) * logf(fabsf(x));
5757
}
5858

5959
double lgamma(double) __attribute__((alias("lgammaf")));

src/libc/sinf.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
*/
1212
#include <math.h>
1313

14-
#define two_over_pi 0.636619772367581
15-
#define p0 0.135788409787738e8
16-
#define p1 -0.494290810090284e7
17-
#define p2 0.440103053537527e6
18-
#define p3 -0.138472724998245e5
19-
#define p4 0.145968840666577e3
20-
#define q0 0.864455865292253e7
21-
#define q1 0.408179225234330e6
22-
#define q2 0.946309610153821e4
23-
#define q3 0.132653490878614e3
14+
#define two_over_pi 0.63661977236758134f
15+
#define p0 0.135788409787738e8f
16+
#define p1 -0.494290810090284e7f
17+
#define p2 0.440103053537527e6f
18+
#define p3 -0.138472724998245e5f
19+
#define p4 0.145968840666577e3f
20+
#define q0 0.864455865292253e7f
21+
#define q1 0.408179225234330e6f
22+
#define q2 0.946309610153821e4f
23+
#define q3 0.132653490878614e3f
2424

2525
float sinus(float arg, int quad)
2626
{
@@ -31,34 +31,44 @@ float sinus(float arg, int quad)
3131
float temp1, temp2;
3232

3333
x = arg;
34-
if(x<0) {
34+
if (x<0.0f) {
3535
x = -x;
3636
quad = quad + 2;
3737
}
38-
x = x*two_over_pi; /*underflow?*/
39-
if(x>32764){
38+
x = x * two_over_pi; /* underflow? */
39+
if (x > 32764.0f) {
4040
y = modff(x,&e);
4141
e = e + quad;
42-
modff(0.25*e,&f);
43-
quad = e - 4*f;
44-
}else{
42+
modff(0.25f * e,&f);
43+
quad = e - 4.0f * f;
44+
} else {
4545
k = x;
4646
y = x - k;
47-
quad = (quad + k) & 03;
47+
quad = (quad + k) & 0x3;
4848
}
49-
if (quad & 01)
50-
y = 1-y;
51-
if(quad > 1)
49+
if (quad & 0x1) {
50+
y = 1.0f - y;
51+
}
52+
if (quad > 1) {
5253
y = -y;
54+
}
5355

5456
ysq = y*y;
5557
temp1 = ((((p4*ysq+p3)*ysq+p2)*ysq+p1)*ysq+p0)*y;
5658
temp2 = ((((ysq+q3)*ysq+q2)*ysq+q1)*ysq+q0);
5759
return(temp1/temp2);
5860
}
5961

62+
/**
63+
* @remarks Minimum ulp:
64+
* ulp of -5 at +0x1.fe2dd0p-9 (|x| < pi/2)
65+
*/
6066
float _sinf_c(float arg) {
67+
if (fabsf(arg) < 0x1.0p-11f) {
68+
return arg;
69+
}
6170
return sinus(arg, 0);
6271
}
6372

73+
6474
double _sin_c(double) __attribute__((alias("_sinf_c")));

src/libc/tgammaf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ float tgammaf(float x) { /* Gamma function */
2929
return zero/zero; /* probably better to return NAN here */
3030
}
3131
ret = (float)M_PI / (sinf((float)M_PI * f) * expf(lgammaf(1.0f - x)));
32-
if (((unsigned int)i & 0x1) == 0) {
32+
if (((unsigned char)i & 0x1) == 0) {
3333
ret = -ret;
3434
}
3535
return ret;

src/libcxx/include/cmath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ template<typename _Tp, typename _Up> inline constexpr enable_if_t<is_integral_v<
239239
hypot(_Tp __x, _Up __y) { return hypot(__x, __y); }
240240

241241
inline float hypot(float __x, float __y, float __z) { return __hypot3f(__x, __y, __z); }
242-
inline long double hypot(long double __x, long double __y, long double __z) { __hypot3l(__x, __y, __z); }
242+
inline long double hypot(long double __x, long double __y, long double __z) { return __hypot3l(__x, __y, __z); }
243243
template<typename _Tp, typename _Up, typename _Vp>
244244
inline enable_if_t<is_integral_v<_Tp> && is_integral_v<_Up> && is_integral_v<_Vp>, double>
245245
hypot(_Tp __x, _Up __y, _Vp __z) { return __hypot3(__x, __y, __z); }

0 commit comments

Comments
 (0)