Changeset 84929b0 in mainline
- Timestamp:
- 2018-08-29T20:06:00Z (6 years ago)
- Children:
- ed9043f7
- Parents:
- 184ff675
- git-author:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-29 19:44:33)
- git-committer:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-29 20:06:00)
- Location:
- uspace
- Files:
-
- 2 added
- 1 deleted
- 6 edited
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/Makefile.common
r184ff675 r84929b0 109 109 LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint 110 110 111 LIBMATH_PREFIX = $(LIB_PREFIX)/math112 LIBMATH_INCLUDES_FLAGS = \113 -I$(LIBMATH_PREFIX)/include \114 -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include115 116 LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix117 111 LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest 118 112 -
uspace/lib/c/include/float.h
r184ff675 r84929b0 1 1 /* 2 * Copyright (c) 201 4 Martin Decky2 * Copyright (c) 2018 CZ.NIC, z.s.p.o. 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libmath 30 * @{ 31 */ 32 /** @file 33 */ 29 #ifndef _FLOAT_H 30 #define _FLOAT_H 34 31 35 #ifndef LIBMATH_FMOD_H_ 36 #define LIBMATH_FMOD_H_ 32 // FIXME: <float.h> is freestanding. Just include the compiler-provided file. 37 33 38 # include <mathtypes.h>39 40 extern float32_t float32_fmod(float32_t, float32_t); 41 extern float64_t float64_fmod(float64_t, float64_t); 34 #define FLT_MANT_DIG __FLT_MANT_DIG__ 35 #define DBL_MANT_DIG __DBL_MANT_DIG__ 36 #define FLT_MAX_EXP __FLT_MAX_EXP__ 37 #define DBL_MAX_EXP __DBL_MAX_EXP__ 42 38 43 39 #endif 44 40 45 /** @}46 */ -
uspace/lib/math/Makefile
r184ff675 r84929b0 34 34 35 35 SOURCES = \ 36 generic/__fcompare.c \ 37 generic/__fpclassify.c \ 38 generic/__signbit.c \ 39 generic/fabs.c \ 36 40 generic/fmod.c \ 37 41 generic/trig.c \ -
uspace/lib/math/generic/__fcompare.c
r184ff675 r84929b0 1 1 /* 2 * Copyright (c) 201 1 Petr Koupy2 * Copyright (c) 2018 CZ.NIC, z.s.p.o. 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libposix 30 * @{ 29 #include <math.h> 30 #include <stdarg.h> 31 32 /** 33 * Fallback symbol used when code including <math.h> is compiled with something 34 * other than GCC or Clang. The function itself must be built with GCC or Clang. 31 35 */ 32 /** @file Mathematical operations. 33 * 34 * The purpose of this file is only to provide prototypes of mathematical 35 * functions defined by C standard and by POSIX. 36 * 37 * It is up to the application to correctly link with either libmath 38 * (provided by HelenOS) or by some other math library (such as fdlibm). 39 */ 36 int __fcompare(size_t sz1, size_t sz2, ...) 37 { 38 va_list ap; 39 va_start(ap, sz2); 40 40 41 #ifndef POSIX_MATH_H_ 42 #define POSIX_MATH_H_ 41 long double val1; 42 long double val2; 43 43 44 #ifdef __GNUC__ 45 #define HUGE_VAL (__builtin_huge_val()) 46 #endif 44 switch (sz1) { 45 case 4: 46 val1 = (long double) va_arg(ap, double); 47 break; 48 case 8: 49 val1 = (long double) va_arg(ap, double); 50 break; 51 default: 52 val1 = va_arg(ap, long double); 53 break; 54 } 47 55 48 extern double ldexp(double, int); 49 extern double frexp(double, int *); 56 switch (sz2) { 57 case 4: 58 val2 = (long double) va_arg(ap, double); 59 break; 60 case 8: 61 val2 = (long double) va_arg(ap, double); 62 break; 63 default: 64 val2 = va_arg(ap, long double); 65 break; 66 } 50 67 51 extern double fabs(double); 52 extern double floor(double); 53 extern double ceil(double); 54 extern double modf(double, double *); 55 extern double fmod(double, double); 56 extern double pow(double, double); 57 extern double exp(double); 58 extern double frexp(double, int *); 59 extern double expm1(double); 60 extern double sqrt(double); 61 extern double log(double); 62 extern double log10(double); 63 extern double sin(double); 64 extern double sinh(double); 65 extern double asin(double); 66 extern double asinh(double); 67 extern double cos(double); 68 extern double cosh(double); 69 extern double acos(double); 70 extern double acosh(double); 71 extern double tan(double); 72 extern double tanh(double); 73 extern double atan(double); 74 extern double atanh(double); 75 extern double atan2(double, double); 76 extern double copysign(double, double); 68 va_end(ap); 77 69 78 #endif /* POSIX_MATH_H_ */ 70 if (isgreaterequal(val1, val2)) { 71 if (isgreater(val1, val2)) 72 return __FCOMPARE_GREATER; 73 else 74 return __FCOMPARE_EQUAL; 75 } else { 76 if (isless(val1, val2)) 77 return __FCOMPARE_LESS; 78 else 79 return 0; 80 } 81 } 79 82 80 /** @}81 */ -
uspace/lib/math/generic/__signbit.c
r184ff675 r84929b0 1 1 /* 2 * Copyright (c) 201 4 Martin Decky2 * Copyright (c) 2018 CZ.NIC, z.s.p.o. 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libmath 30 * @{ 29 #include <math.h> 30 #include <stdarg.h> 31 32 /** 33 * Fallback symbol used when code including <math.h> is compiled with something 34 * other than GCC or Clang. The function itself must be built with GCC or Clang. 31 35 */ 32 /** @file 33 */ 36 int __signbit(size_t sz, ...) 37 { 38 va_list ap; 39 va_start(ap, sz); 34 40 35 #ifndef LIBMATH_TRIG_H_ 36 #define LIBMATH_TRIG_H_ 41 int result; 37 42 38 #include <mathtypes.h> 43 switch (sz) { 44 case 4: 45 result = signbit(va_arg(ap, double)); 46 break; 47 case 8: 48 result = signbit(va_arg(ap, double)); 49 break; 50 default: 51 result = signbit(va_arg(ap, long double)); 52 break; 53 } 39 54 40 extern float32_t float32_sin(float32_t); 41 extern float64_t float64_sin(float64_t); 42 extern float32_t float32_cos(float32_t); 43 extern float64_t float64_cos(float64_t); 55 va_end(ap); 56 return result; 57 } 44 58 45 #endif46 47 /** @}48 */ -
uspace/lib/math/generic/fabs.c
r184ff675 r84929b0 33 33 */ 34 34 35 #ifndef LIBMATH_TRUNC_H_ 36 #define LIBMATH_TRUNC_H_ 35 #include <math.h> 37 36 38 #include <mathtypes.h> 37 float fabsf(float val) 38 { 39 return copysignf(val, 1.0f); 40 } 39 41 40 extern float32_t float32_trunc(float32_t); 41 extern float64_t float64_trunc(float64_t); 42 43 #endif 42 double fabs(double val) 43 { 44 return copysign(val, 1.0); 45 } 44 46 45 47 /** @} -
uspace/lib/math/generic/fmod.c
r184ff675 r84929b0 33 33 */ 34 34 35 #include <fmod.h>36 35 #include <math.h> 37 36 … … 52 51 * 53 52 */ 54 float 32_t float32_fmod(float32_t dividend, float32_t divisor)53 float fmodf(float dividend, float divisor) 55 54 { 56 55 // FIXME: replace with exact arithmetics 57 56 58 float 32_t quotient = trunc_f32(dividend / divisor);57 float quotient = truncf(dividend / divisor); 59 58 60 59 return (dividend - quotient * divisor); … … 77 76 * 78 77 */ 79 float64_t float64_fmod(float64_t dividend, float64_tdivisor)78 double fmod(double dividend, double divisor) 80 79 { 81 80 // FIXME: replace with exact arithmetics 82 81 83 float64_t quotient = trunc_f64(dividend / divisor);82 double quotient = trunc(dividend / divisor); 84 83 85 84 return (dividend - quotient * divisor); -
uspace/lib/math/generic/trig.c
r184ff675 r84929b0 35 35 36 36 #include <math.h> 37 #include <trig.h>38 37 39 38 #define TAYLOR_DEGREE_32 13 … … 41 40 42 41 /** Precomputed values for factorial (starting from 1!) */ 43 static float64_tfactorials[TAYLOR_DEGREE_64] = {42 static double factorials[TAYLOR_DEGREE_64] = { 44 43 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 45 44 479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L, … … 60 59 * 61 60 */ 62 static float 32_t taylor_sin_32(float32_t arg)63 { 64 float 32_tret = 0;65 float 32_tnom = 1;61 static float taylor_sin_32(float arg) 62 { 63 float ret = 0; 64 float nom = 1; 66 65 67 66 for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) { … … 89 88 * 90 89 */ 91 static float64_t taylor_sin_64(float64_targ)92 { 93 float64_tret = 0;94 float64_tnom = 1;90 static double taylor_sin_64(double arg) 91 { 92 double ret = 0; 93 double nom = 1; 95 94 96 95 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { … … 118 117 * 119 118 */ 120 static float 32_t taylor_cos_32(float32_t arg)121 { 122 float 32_tret = 1;123 float 32_tnom = 1;119 static float taylor_cos_32(float arg) 120 { 121 float ret = 1; 122 float nom = 1; 124 123 125 124 for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) { … … 147 146 * 148 147 */ 149 static float64_t taylor_cos_64(float64_targ)150 { 151 float64_tret = 1;152 float64_tnom = 1;148 static double taylor_cos_64(double arg) 149 { 150 double ret = 1; 151 double nom = 1; 153 152 154 153 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { … … 176 175 * 177 176 */ 178 static float 32_t base_sin_32(float32_t arg)177 static float base_sin_32(float arg) 179 178 { 180 179 unsigned int period = arg / (M_PI / 4); … … 209 208 * 210 209 */ 211 static float64_t base_sin_64(float64_targ)210 static double base_sin_64(double arg) 212 211 { 213 212 unsigned int period = arg / (M_PI / 4); … … 242 241 * 243 242 */ 244 static float 32_t base_cos_32(float32_t arg)243 static float base_cos_32(float arg) 245 244 { 246 245 unsigned int period = arg / (M_PI / 4); … … 275 274 * 276 275 */ 277 static float64_t base_cos_64(float64_targ)276 static double base_cos_64(double arg) 278 277 { 279 278 unsigned int period = arg / (M_PI / 4); … … 305 304 * 306 305 */ 307 float 32_t float32_sin(float32_t arg)308 { 309 float 32_t base_arg = fmod_f32(arg, 2 * M_PI);306 float sinf(float arg) 307 { 308 float base_arg = fmodf(arg, 2 * M_PI); 310 309 311 310 if (base_arg < 0) … … 324 323 * 325 324 */ 326 float64_t float64_sin(float64_targ)327 { 328 float64_t base_arg = fmod_f64(arg, 2 * M_PI);325 double sin(double arg) 326 { 327 double base_arg = fmod(arg, 2 * M_PI); 329 328 330 329 if (base_arg < 0) … … 343 342 * 344 343 */ 345 float 32_t float32_cos(float32_t arg)346 { 347 float 32_t base_arg = fmod_f32(arg, 2 * M_PI);344 float cosf(float arg) 345 { 346 float base_arg = fmodf(arg, 2 * M_PI); 348 347 349 348 if (base_arg < 0) … … 362 361 * 363 362 */ 364 float64_t float64_cos(float64_targ)365 { 366 float64_t base_arg = fmod_f64(arg, 2 * M_PI);363 double cos(double arg) 364 { 365 double base_arg = fmod(arg, 2 * M_PI); 367 366 368 367 if (base_arg < 0) -
uspace/lib/math/generic/trunc.c
r184ff675 r84929b0 34 34 */ 35 35 36 #include <mathtypes.h> 37 #include <trunc.h> 36 #include <math.h> 37 #include <float.h> 38 #include <stdint.h> 38 39 39 40 /** Truncate fractional part (round towards zero) … … 52 53 * 53 54 */ 54 float 32_t float32_trunc(float32_t val)55 float truncf(float val) 55 56 { 56 float32_u v; 57 int32_t exp; 57 /* If the input is a nan, return a canonical nan. */ 58 if (isnan(val)) 59 return __builtin_nanf(""); 58 60 59 v.val = val; 60 exp = v.data.parts.exp - FLOAT32_BIAS; 61 const int exp_bias = FLT_MAX_EXP - 1; 62 const int mant_bits = FLT_MANT_DIG - 1; 63 const uint32_t mant_mask = (UINT32_C(1) << mant_bits) - 1; 61 64 62 if (exp < 0) { 63 /* -1 < val < 1 => result is +0 or -0 */ 64 v.data.parts.exp = 0; 65 v.data.parts.fraction = 0; 66 } else if (exp >= FLOAT32_FRACTION_SIZE) { 67 if (exp == 1024) { 68 /* val is +inf, -inf or NaN => trigger an exception */ 69 // FIXME TODO 70 } 65 union { 66 float f; 67 uint32_t i; 68 } u = { .f = fabsf(val) }; 71 69 72 /* All bits in val are relevant for the result */ 73 } else { 74 /* Truncate irrelevant fraction bits */ 75 v.data.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp); 76 } 70 int exp = (u.i >> mant_bits) - exp_bias; 77 71 78 return v.val; 72 /* If value is less than one, return zero with appropriate sign. */ 73 if (exp < 0) 74 return copysignf(0.0f, val); 75 76 if (exp >= mant_bits) 77 return val; 78 79 /* Truncate irrelevant fraction bits */ 80 u.i &= ~(mant_mask >> exp); 81 return copysignf(u.f, val); 79 82 } 80 83 … … 94 97 * 95 98 */ 96 float64_t float64_trunc(float64_tval)99 double trunc(double val) 97 100 { 98 float64_u v; 99 int32_t exp; 101 /* If the input is a nan, return a canonical nan. */ 102 if (isnan(val)) 103 return __builtin_nan(""); 100 104 101 v.val = val; 102 exp = v.data.parts.exp - FLOAT64_BIAS; 105 const int exp_bias = DBL_MAX_EXP - 1; 106 const int mant_bits = DBL_MANT_DIG - 1; 107 const uint64_t mant_mask = (UINT64_C(1) << mant_bits) - 1; 103 108 104 if (exp < 0) { 105 /* -1 < val < 1 => result is +0 or -0 */ 106 v.data.parts.exp = 0; 107 v.data.parts.fraction = 0; 108 } else if (exp >= FLOAT64_FRACTION_SIZE) { 109 if (exp == 1024) { 110 /* val is +inf, -inf or NaN => trigger an exception */ 111 // FIXME TODO 112 } 109 union { 110 double f; 111 uint64_t i; 112 } u = { .f = fabs(val) }; 113 113 114 /* All bits in val are relevant for the result */ 115 } else { 116 /* Truncate irrelevant fraction bits */ 117 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp); 118 } 114 int exp = ((int)(u.i >> mant_bits)) - exp_bias; 119 115 120 return v.val; 116 /* If value is less than one, return zero with appropriate sign. */ 117 if (exp < 0) 118 return copysign(0.0, val); 119 120 if (exp >= mant_bits) 121 return val; 122 123 /* Truncate irrelevant fraction bits */ 124 u.i &= ~(mant_mask >> exp); 125 return copysign(u.f, val); 121 126 } 122 127 -
uspace/lib/softfloat/Makefile
r184ff675 r84929b0 30 30 USPACE_PREFIX = ../.. 31 31 LIBRARY = libsoftfloat 32 EXTRA_CFLAGS += $(LIBMATH_INCLUDES_FLAGS)33 32 34 33 SOURCES = \
Note:
See TracChangeset
for help on using the changeset viewer.