diff options
Diffstat (limited to 'Libraries/LibM')
-rw-r--r-- | Libraries/LibM/math.cpp | 124 | ||||
-rw-r--r-- | Libraries/LibM/math.h | 136 |
2 files changed, 129 insertions, 131 deletions
diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index fc1e1ec38a..d83dbecd64 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -59,17 +59,17 @@ constexpr size_t product_odd() { return value * product_odd<value - 2>(); } extern "C" { -double trunc(double x) +double trunc(double x) NOEXCEPT { return (int64_t)x; } -double cos(double angle) +double cos(double angle) NOEXCEPT { return sin(angle + M_PI_2); } -float cosf(float angle) +float cosf(float angle) NOEXCEPT { return sinf(angle + M_PI_2); } @@ -78,7 +78,7 @@ float cosf(float angle) // now this works pretty well (and doesn't mess anything up // in quake in particular, which is very Floating-Point precision // heavy) -double sin(double angle) +double sin(double angle) NOEXCEPT { double ret = 0.0; __asm__( @@ -89,7 +89,7 @@ double sin(double angle) return ret; } -float sinf(float angle) +float sinf(float angle) NOEXCEPT { float ret = 0.0f; __asm__( @@ -99,7 +99,7 @@ float sinf(float angle) return ret; } -double pow(double x, double y) +double pow(double x, double y) NOEXCEPT { // FIXME: Please fix me. I am naive. if (y == 0) @@ -109,7 +109,7 @@ double pow(double x, double y) int y_as_int = (int)y; if (y == (double)y_as_int) { double result = x; - for (int i = 0; i < abs(y) - 1; ++i) + for (int i = 0; i < fabs(y) - 1; ++i) result *= x; if (y < 0) result = 1.0 / result; @@ -118,7 +118,7 @@ double pow(double x, double y) return exp(y * log(x)); } -float powf(float x, float y) +float powf(float x, float y) NOEXCEPT { // FIXME: Please fix me. I am naive. if (y == 0) @@ -128,7 +128,7 @@ float powf(float x, float y) int y_as_int = (int)y; if (y == (float)y_as_int) { float result = x; - for (int i = 0; i < abs(y) - 1; ++i) + for (int i = 0; i < fabs(y) - 1; ++i) result *= x; if (y < 0) result = 1.0 / result; @@ -137,14 +137,14 @@ float powf(float x, float y) return (float)exp((double)y * log((double)x)); } -double ldexp(double x, int exp) +double ldexp(double x, int exp) NOEXCEPT { // FIXME: Please fix me. I am naive. double val = pow(2, exp); return x * val; } -double tanh(double x) +double tanh(double x) NOEXCEPT { if (x > 0) { double exponentiated = exp(2 * x); @@ -155,7 +155,7 @@ double tanh(double x) return (plusX - minusX) / (plusX + minusX); } -double ampsin(double angle) +double ampsin(double angle) NOEXCEPT { double looped_angle = fmod(M_PI + angle, M_TAU) - M_PI; double looped_angle_squared = looped_angle * looped_angle; @@ -172,12 +172,12 @@ double ampsin(double angle) return quadratic_term + linear_term; } -double tan(double angle) +double tan(double angle) NOEXCEPT { return ampsin(angle) / ampsin(M_PI_2 + angle); } -double sqrt(double x) +double sqrt(double x) NOEXCEPT { double res; __asm__("fsqrt" @@ -186,7 +186,7 @@ double sqrt(double x) return res; } -float sqrtf(float x) +float sqrtf(float x) NOEXCEPT { float res; __asm__("fsqrt" @@ -195,7 +195,7 @@ float sqrtf(float x) return res; } -double sinh(double x) +double sinh(double x) NOEXCEPT { double exponentiated = exp(x); if (x > 0) @@ -203,12 +203,12 @@ double sinh(double x) return (exponentiated - 1 / exponentiated) / 2; } -double log10(double x) +double log10(double x) NOEXCEPT { return log(x) / M_LN10; } -double log(double x) +double log(double x) NOEXCEPT { if (x < 0) return NAN; @@ -223,22 +223,22 @@ double log(double x) return y + 2 * (x - exponentiated) / (x + exponentiated); } -float logf(float x) +float logf(float x) NOEXCEPT { return (float)log(x); } -double fmod(double index, double period) +double fmod(double index, double period) NOEXCEPT { return index - trunc(index / period) * period; } -float fmodf(float index, float period) +float fmodf(float index, float period) NOEXCEPT { return index - trunc(index / period) * period; } -double exp(double exponent) +double exp(double exponent) NOEXCEPT { double result = 1; if (exponent >= 1) { @@ -274,22 +274,22 @@ double exp(double exponent) return result * taylor_series_result; } -float expf(float exponent) +float expf(float exponent) NOEXCEPT { return (float)exp(exponent); } -double exp2(double exponent) +double exp2(double exponent) NOEXCEPT { return pow(2.0, exponent); } -float exp2f(float exponent) +float exp2f(float exponent) NOEXCEPT { return pow(2.0f, exponent); } -double cosh(double x) +double cosh(double x) NOEXCEPT { double exponentiated = exp(-x); if (x < 0) @@ -297,7 +297,7 @@ double cosh(double x) return (1 / exponentiated + exponentiated) / 2; } -double atan2(double y, double x) +double atan2(double y, double x) NOEXCEPT { if (x > 0) return atan(y / x); @@ -313,12 +313,12 @@ double atan2(double y, double x) return atan(y / x) - M_PI; } -float atan2f(float y, float x) +float atan2f(float y, float x) NOEXCEPT { return (float)atan2(y, x); } -double atan(double x) +double atan(double x) NOEXCEPT { if (x < 0) return -atan(-x); @@ -328,7 +328,7 @@ double atan(double x) return x / (1 + 1 * 1 * squared / (3 + 2 * 2 * squared / (5 + 3 * 3 * squared / (7 + 4 * 4 * squared / (9 + 5 * 5 * squared / (11 + 6 * 6 * squared / (13 + 7 * 7 * squared))))))); } -double asin(double x) +double asin(double x) NOEXCEPT { if (x > 1 || x < -1) return NAN; @@ -351,60 +351,60 @@ double asin(double x) return value; } -float asinf(float x) +float asinf(float x) NOEXCEPT { return (float)asin(x); } -double acos(double x) +double acos(double x) NOEXCEPT { return M_PI_2 - asin(x); } -float acosf(float x) +float acosf(float x) NOEXCEPT { return M_PI_2 - asinf(x); } -double fabs(double value) +double fabs(double value) NOEXCEPT { return value < 0 ? -value : value; } -double log2(double x) +double log2(double x) NOEXCEPT { return log(x) / M_LN2; } -float log2f(float x) +float log2f(float x) NOEXCEPT { return log2(x); } -long double log2l(long double x) +long double log2l(long double x) NOEXCEPT { return log2(x); } -double frexp(double, int*) +double frexp(double, int*) NOEXCEPT { ASSERT_NOT_REACHED(); return 0; } -float frexpf(float, int*) +float frexpf(float, int*) NOEXCEPT { ASSERT_NOT_REACHED(); return 0; } -long double frexpl(long double, int*) +long double frexpl(long double, int*) NOEXCEPT { ASSERT_NOT_REACHED(); return 0; } -double round(double value) +double round(double value) NOEXCEPT { // FIXME: Please fix me. I am naive. if (value >= 0.0) @@ -412,7 +412,7 @@ double round(double value) return (double)(int)(value - 0.5); } -float roundf(float value) +float roundf(float value) NOEXCEPT { // FIXME: Please fix me. I am naive. if (value >= 0.0f) @@ -420,7 +420,7 @@ float roundf(float value) return (float)(int)(value - 0.5f); } -float floorf(float value) +float floorf(float value) NOEXCEPT { if (value >= 0) return (int)value; @@ -428,7 +428,7 @@ float floorf(float value) return ((float)intvalue == value) ? intvalue : intvalue - 1; } -double floor(double value) +double floor(double value) NOEXCEPT { if (value >= 0) return (int)value; @@ -436,12 +436,12 @@ double floor(double value) return ((double)intvalue == value) ? intvalue : intvalue - 1; } -double rint(double value) +double rint(double value) NOEXCEPT { return (int)roundf(value); } -float ceilf(float value) +float ceilf(float value) NOEXCEPT { // FIXME: Please fix me. I am naive. int as_int = (int)value; @@ -455,7 +455,7 @@ float ceilf(float value) return as_int + 1; } -double ceil(double value) +double ceil(double value) NOEXCEPT { // FIXME: Please fix me. I am naive. int as_int = (int)value; @@ -469,24 +469,24 @@ double ceil(double value) return as_int + 1; } -double modf(double x, double* intpart) +double modf(double x, double* intpart) NOEXCEPT { *intpart = (double)((int)(x)); return x - (int)x; } -double gamma(double x) +double gamma(double x) NOEXCEPT { // Stirling approximation return sqrt(2.0 * M_PI / x) * pow(x / M_E, x); } -double expm1(double x) +double expm1(double x) NOEXCEPT { return pow(M_E, x) - 1; } -double cbrt(double x) +double cbrt(double x) NOEXCEPT { if (x > 0) { return pow(x, 1.0 / 3.0); @@ -495,35 +495,35 @@ double cbrt(double x) return -pow(-x, 1.0 / 3.0); } -double log1p(double x) +double log1p(double x) NOEXCEPT { return log(1 + x); } -double acosh(double x) +double acosh(double x) NOEXCEPT { return log(x + sqrt(x * x - 1)); } -double asinh(double x) +double asinh(double x) NOEXCEPT { return log(x + sqrt(x * x + 1)); } -double atanh(double x) +double atanh(double x) NOEXCEPT { return log((1 + x) / (1 - x)) / 2.0; } -double hypot(double x, double y) +double hypot(double x, double y) NOEXCEPT { return sqrt(x * x + y * y); } -double erf(double x) +double erf(double x) NOEXCEPT { // algorithm taken from Abramowitz and Stegun (no. 26.2.17) - double t = 1 / (1 + 0.47047 * abs(x)); + double t = 1 / (1 + 0.47047 * fabs(x)); double poly = t * (0.3480242 + t * (-0.958798 + t * 0.7478556)); double answer = 1 - poly * exp(-x * x); if (x < 0) @@ -532,15 +532,9 @@ double erf(double x) return answer; } -double erfc(double x) +double erfc(double x) NOEXCEPT { return 1 - erf(x); } -int isnormal(double x) -{ - if (x < 0) - x = -x; - return x >= DOUBLE_MIN && x <= DOUBLE_MAX; -} } diff --git a/Libraries/LibM/math.h b/Libraries/LibM/math.h index 571f11f01f..0efe69e9cd 100644 --- a/Libraries/LibM/math.h +++ b/Libraries/LibM/math.h @@ -28,6 +28,12 @@ #include <sys/cdefs.h> +#if __cplusplus >= 201103L +#define NOEXCEPT noexcept +#else +#define NOEXCEPT +#endif + __BEGIN_DECLS #define HUGE_VAL 1e10000 @@ -47,74 +53,72 @@ __BEGIN_DECLS #define DOUBLE_MAX ((double)0b0111111111101111111111111111111111111111111111111111111111111111) #define DOUBLE_MIN ((double)0b0000000000010000000000000000000000000000000000000000000000000000) -double acos(double); -float acosf(float); -double asin(double); -float asinf(float); -double atan(double); -float atanf(float); -double atan2(double, double); -float atan2f(float, float); -double cos(double); -float cosf(float); -double cosh(double); -float coshf(float); -double sin(double); -float sinf(float); -double sinh(double); -float sinhf(float); -double tan(double); -float tanf(float); -double tanh(double); -float tanhf(float); -double ceil(double); -float ceilf(float); -double floor(double); -float floorf(float); -double round(double); -float roundf(float); -double fabs(double); -float fabsf(float); -double fmod(double, double); -float fmodf(float, float); -double exp(double); -float expf(float); -double exp2(double); -float exp2f(float); -double frexp(double, int* exp); -float frexpf(float, int* exp); -double log(double); -float logf(float); -double log10(double); -float log10f(float); -double sqrt(double); -float sqrtf(float); -double modf(double, double*); -float modff(float, float*); -double ldexp(double, int exp); -float ldexpf(float, int exp); - -double pow(double x, double y); -float powf(float x, float y); +double acos(double) NOEXCEPT; +float acosf(float) NOEXCEPT; +double asin(double) NOEXCEPT; +float asinf(float) NOEXCEPT; +double atan(double) NOEXCEPT; +float atanf(float) NOEXCEPT; +double atan2(double, double) NOEXCEPT; +float atan2f(float, float) NOEXCEPT; +double cos(double) NOEXCEPT; +float cosf(float) NOEXCEPT; +double cosh(double) NOEXCEPT; +float coshf(float) NOEXCEPT; +double sin(double) NOEXCEPT; +float sinf(float) NOEXCEPT; +double sinh(double) NOEXCEPT; +float sinhf(float) NOEXCEPT; +double tan(double) NOEXCEPT; +float tanf(float) NOEXCEPT; +double tanh(double) NOEXCEPT; +float tanhf(float) NOEXCEPT; +double ceil(double) NOEXCEPT; +float ceilf(float) NOEXCEPT; +double floor(double) NOEXCEPT; +float floorf(float) NOEXCEPT; +double round(double) NOEXCEPT; +float roundf(float) NOEXCEPT; +double fabs(double) NOEXCEPT; +float fabsf(float) NOEXCEPT; +double fmod(double, double) NOEXCEPT; +float fmodf(float, float) NOEXCEPT; +double exp(double) NOEXCEPT; +float expf(float) NOEXCEPT; +double exp2(double) NOEXCEPT; +float exp2f(float) NOEXCEPT; +double frexp(double, int* exp) NOEXCEPT; +float frexpf(float, int* exp) NOEXCEPT; +double log(double) NOEXCEPT; +float logf(float) NOEXCEPT; +double log10(double) NOEXCEPT; +float log10f(float) NOEXCEPT; +double sqrt(double) NOEXCEPT; +float sqrtf(float) NOEXCEPT; +double modf(double, double*) NOEXCEPT; +float modff(float, float*) NOEXCEPT; +double ldexp(double, int exp) NOEXCEPT; +float ldexpf(float, int exp) NOEXCEPT; -double log2(double); -float log2f(float); -long double log2l(long double); -double frexp(double, int*); -float frexpf(float, int*); -long double frexpl(long double, int*); +double pow(double x, double y) NOEXCEPT; +float powf(float x, float y) NOEXCEPT; -double gamma(double); -double expm1(double); -double cbrt(double); -double log1p(double); -double acosh(double); -double asinh(double); -double atanh(double); -double hypot(double, double); -double erf(double); -double erfc(double); +double log2(double) NOEXCEPT; +float log2f(float) NOEXCEPT; +long double log2l(long double) NOEXCEPT; +double frexp(double, int*) NOEXCEPT; +float frexpf(float, int*) NOEXCEPT; +long double frexpl(long double, int*) NOEXCEPT; -int isnormal(double); +double gamma(double) NOEXCEPT; +double expm1(double) NOEXCEPT; +double cbrt(double) NOEXCEPT; +double log1p(double) NOEXCEPT; +double acosh(double) NOEXCEPT; +double asinh(double) NOEXCEPT; +double atanh(double) NOEXCEPT; +double hypot(double, double) NOEXCEPT; +double erf(double) NOEXCEPT; +double erfc(double) NOEXCEPT; __END_DECLS |