Skip to content

Commit fe31098

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
Added the remaining long double <math.h> routines
1 parent 8bf4aff commit fe31098

File tree

27 files changed

+522
-138
lines changed

27 files changed

+522
-138
lines changed

src/libc/acosl.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
#include <errno.h>
4+
#include <math.h>
5+
#include <stdbool.h>
6+
#include "__float64_constants.h"
7+
8+
/**
9+
* @remarks Minimum ulp:
10+
* ulp of -13337 at +0x1.ffffffe84b398p-1 assuming ideal atanl
11+
*/
12+
long double acosl(long double x) {
13+
if (x < 0.5L) {
14+
return F64_PI2 - asinl(x);
15+
}
16+
if (x <= 1.0L) {
17+
return atanl((sqrtl(1.0L - x) * sqrtl(1.0L + x)) / x);
18+
}
19+
errno = EDOM;
20+
return __builtin_nanl("");
21+
}

src/libc/asinl.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright (c) 2000-2008 Zilog, Inc. */
2+
3+
/**
4+
* asin(arg) return the arcsin of arg.
5+
* arctan is called after appropriate range reduction.
6+
*/
7+
8+
#include <errno.h>
9+
#include <math.h>
10+
#include <stdbool.h>
11+
#include "__float64_constants.h"
12+
13+
/**
14+
* @remarks Minimum ulp:
15+
* ulp of -805 at -0x1.ffffffc97db1bp-1 assuming ideal atanl
16+
*/
17+
long double asinl(long double arg) {
18+
bool arg_sign;
19+
long double temp;
20+
arg_sign = signbit(arg);
21+
arg = fabsl(arg);
22+
23+
if(arg > 1.0L) {
24+
errno = EDOM;
25+
return 0.0L;
26+
}
27+
28+
temp = sqrtl(1.0L - arg * arg);
29+
if(arg > 0.7L) {
30+
temp = F64_PI2 - atanl(temp / arg);
31+
} else {
32+
temp = atanl(arg / temp);
33+
}
34+
if (arg_sign) {
35+
temp = -temp;
36+
}
37+
return temp;
38+
}

src/libc/cbrtf.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
#include <math.h>
22

3+
/**
4+
* @remarks Minimum ulp:
5+
* ulp of -1 at +0x1.000006p-4 with ideal expf logf (1/16 < |x| < 16)
6+
* ulp of -5 at +0x1.cb149cp-4 with current expf logf (1/16 < |x| < 16)
7+
* ulp of -70 at +0x1.e9b19ep-100 with current expf logf (1.0e-30 < |x| < 1.0e+30)
8+
* ulp of +81 at +0x1.e40000p-142 with current expf logf (entire range)
9+
*/
310
float cbrtf(float x)
411
{
5-
return copysignf(powf(fabsf(x), 1.f/3.f), x);
12+
if (x == 0.0f) {
13+
return x;
14+
}
15+
return copysignf(expf(logf(fabsf(x)) / 3.0f), x);
616
}
717

818
double cbrt(double) __attribute__((alias("cbrtf")));

src/libc/cbrtl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <math.h>
2+
3+
/**
4+
* @remarks Minimum ulp:
5+
* ulp of -257 at +0x1.ffe3f201d9f88p-808 assuming ideal logl expl
6+
*/
7+
long double cbrtl(long double x)
8+
{
9+
if (x == 0.0L) {
10+
return x;
11+
}
12+
return copysignl(expl(logl(fabsl(x)) / 3.0L), x);
13+
}

src/libc/coshf.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,16 @@
1-
/************************************************************************/
2-
/* */
3-
/* Copyright (C)1987-2008 by */
4-
/* Zilog, Inc. */
5-
/* */
6-
/* San Jose, California */
7-
/* */
8-
/************************************************************************/
9-
/*
10-
sinh(arg) returns the hyperbolic sine of its floating-
11-
point argument.
12-
13-
The exponential function is called for arguments
14-
greater in magnitude than 0.5.
15-
16-
A series is used for arguments smaller in magnitude than 0.5.
17-
The coefficients are #2029 from Hart & Cheney. (20.36D)
18-
19-
cosh(arg) is computed from the exponential function for
20-
all arguments.
21-
*/
22-
231
#include <math.h>
242

253
/**
264
* @remarks Minimum ulp:
275
* ulp of -1 at +0x1.39a3fep-12 with ideal expf (|x| < 21.0f)
286
* ulp of -18 at +0x1.0a049cp+4 with current expf (|x| < 21.0f)
297
*/
30-
float _coshf_c(float arg) {
31-
float val;
32-
arg = fabsf(arg);
33-
34-
val = expf(arg);
35-
36-
if (arg > 21.0f) {
37-
return val / 2.0f;
8+
float _coshf_c(float x) {
9+
x = fabsf(x);
10+
if (x < 88.5f) {
11+
return (expf(x) + expf(-x)) / 2.0f;
3812
}
39-
40-
val += expf(-arg);
41-
val /= 2.0f;
42-
return val;
13+
return expf(x - (float)M_LN2);
4314
}
4415

4516
double _cosh_c(double) __attribute__((alias("_coshf_c")));

src/libc/coshl.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <math.h>
2+
#include "__float64_constants.h"
3+
4+
/**
5+
* @remarks Minimum ulp:
6+
* ulp of -1 at -0x1.f9134e7749c00p+0 with ideal expl (|x| < 709.0)
7+
* ulp of +496 at +0x1.62e4249341dc8p+9 with ideal expl (|x| >= 709.0)
8+
*/
9+
long double coshl(long double x) {
10+
x = fabsl(x);
11+
if (x < 709.0L) {
12+
return (expl(x) + expl(-x)) / 2.0L;
13+
}
14+
return expl(x - F64_LN2);
15+
}

src/libc/exp2f.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
float exp2f(float x)
44
{
5-
return expf(x * (float)M_LOG2E);
5+
// this can be made much more accurately using frexp and ldexp
6+
return expf(x * (float)M_LN2);
67
}
78

89
double exp2(double) __attribute__((alias("exp2f")));

src/libc/exp2l.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <math.h>
2+
#include "__float64_constants.h"
3+
4+
long double exp2l(long double x)
5+
{
6+
// this can be made much more accurately using frexp and ldexp
7+
return expl(x * F64_LN2);
8+
}

src/libc/include/__math_def.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ double fmod(double, double);
172172
float fmodf(float, float);
173173
long double fmodl(long double, long double);
174174

175-
double frexp(double, int *);
176-
float frexpf(float, int *);
177-
long double frexpl(long double, int *);
175+
double frexp(double, int *) __attribute__((nonnull(2)));
176+
float frexpf(float, int *) __attribute__((nonnull(2)));
177+
long double frexpl(long double, int *) __attribute__((nonnull(2)));
178178

179179
double hypot(double, double);
180180
float hypotf(float, float);
@@ -268,9 +268,9 @@ double remainder(double, double);
268268
float remainderf(float, float);
269269
long double remainderl(long double, long double);
270270

271-
double remquo(double, double, int *);
272-
float remquof(float, float, int *);
273-
long double remquol(long double, long double, int *);
271+
double remquo(double, double, int *) __attribute__((nonnull(3)));
272+
float remquof(float, float, int *) __attribute__((nonnull(3)));
273+
long double remquol(long double, long double, int *) __attribute__((nonnull(3)));
274274

275275
double rint(double);
276276
float rintf(float);

src/libc/include/math.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
float: _isnanf \
3434
)(x))
3535

36-
3736
#define isinf(x) ((int)_Generic(__math_promote(x), \
3837
long double: _isinfl, \
3938
default: _isinff, \

0 commit comments

Comments
 (0)