Changeset 516e780 in mainline
- Timestamp:
- 2018-08-31T11:55:41Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- fa86fff
- Parents:
- 7f7d642
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-08-31 11:55:41)
- git-committer:
- GitHub <noreply@…> (2018-08-31 11:55:41)
- Files:
-
- 3 added
- 64 deleted
- 12 edited
- 8 moved
Legend:
- Unmodified
- Added
- Removed
-
boot/Makefile.common
r7f7d642 r516e780 244 244 $(USPACE_PATH)/lib/sif/test-libsif \ 245 245 $(USPACE_PATH)/lib/uri/test-liburi \ 246 $(USPACE_PATH)/lib/math/test-libmath \ 246 247 $(USPACE_PATH)/drv/bus/usb/xhci/test-xhci \ 247 248 $(USPACE_PATH)/app/bdsh/test-bdsh \ -
uspace/Makefile.common
r7f7d642 r516e780 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/app/tester/float/float2.c
r7f7d642 r516e780 43 43 }; 44 44 45 static double arguments_acos[OPERANDS] = {46 -0.936456687291, -0.504846104600, 0.862318872288, 0.964966028492,47 0.987353618220, 1.0, -0.194939922623, 0.978471923925, -0.999023478833,48 0.54030230586849 };50 51 static double arguments_asin[OPERANDS] = {52 -0.350783227690, -0.863209366649, -0.506365641110, -0.262374853704,53 0.158533380044, 0.0, 0.980815184715, -0.206379975025, -0.044182448332,54 0.84147098480855 };56 57 static double arguments_atan[OPERANDS] = {58 3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0,59 2.718281828459045, 9.9, 0.00160 };61 62 static double arguments_exp[OPERANDS] = {63 3.5, -2.1, 50.0, 0.0, 1.0, 13.2, -1.1, -5.5, 0.1, -66.064 };65 66 static double arguments_log[OPERANDS] = {67 3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0,68 2.718281828459045, 9.9, 0.00169 };70 71 static double arguments_sqrt[OPERANDS] = {72 3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0,73 2.718281828459045, 9.9, 0.00174 };75 76 static double arguments_tanh[OPERANDS] = {77 3.5, -2.1, 50.0, 0.0, 1.0, 13.2, -1.1, -5.5, 0.000001, -66000000.078 };79 80 static double results_acos[OPERANDS] = {81 2.783185307180, 2.100000000000, 0.530964914873, 0.265482457437,82 0.159205070272, 0.000000000000, 1.766992524091, 0.207873834887,83 3.097395817941, 1.00000000000084 };85 86 static double results_asin[OPERANDS] = {87 -0.358407346411, -1.041592653590, -0.530964914874, -0.265482457437,88 0.159205070273, 0.000000000000, 1.374600129498, -0.207873834889,89 -0.044196835651, 1.00000000000090 };91 92 static double results_atan[OPERANDS] = {93 1.292496667790, 1.560796660108, 1.550798992822, 1.569494779052,94 1.569870829603, 0.785398163397, 1.555645970920, 1.218282905017,95 1.470127674637, 0.00099999966796 };97 98 static double results_ceil[OPERANDS] = {99 4.0, -2.0, 100.0, 50.0, -1024.0, 0.0, 769.0, 1081.0, -600.0, 1.0100 };101 102 45 static double results_cos[OPERANDS] = { 103 46 -0.936456687291, -0.504846104600, 0.862318872288, 0.964966028492, … … 106 49 }; 107 50 108 static double results_cosh[OPERANDS] = {109 16.572824671057, 4.144313170410, 2592352764293536022528.000000000000,110 1.000000000000, 1.543080634815, 270182.468624271103, 1.668518553822,111 122.348009517829, 1.005004168056, 23035933171656458903220125696.0112 };113 114 static double results_fabs[OPERANDS] = {115 3.5, 2.1, 100.0, 50.0, 1024.0, 0.0, 768.3156, 1080.499999, 600.0, 1.0116 };117 118 static double results_floor[OPERANDS] = {119 3.0, -3.0, 100.0, 50.0, -1024.0, 0.0, 768.0, 1080.0, -600.0, 1.0120 };121 122 static double results_exp[OPERANDS] = {123 33.115451958692, 0.122456428253, 5184705528587072045056.0,124 1.000000000000, 2.718281828459, 540364.937246691552, 0.332871083698,125 0.004086771438, 1.105170918076, 0.000000000000126 };127 128 static double results_log[OPERANDS] = {129 1.252762968495, 4.605170185988, 3.912023005428, 6.644200586236,130 6.985179175021, 0.000000000000, 4.189654742026, 1.000000000000,131 2.292534757141, -6.907755278982132 };133 134 static double results_log10[OPERANDS] = {135 0.544068044350, 2.000000000000, 1.698970004336, 2.885539651261,136 3.033624770817, 0.000000000000, 1.819543935542, 0.434294481903,137 0.995635194598, -3.000000000000138 };139 140 static double results_log2[OPERANDS] = {141 1.807354922058, 6.643856189775, 5.643856189775, 9.585555236434,142 10.077483355524, 0.000000000000, 6.044394119358, 1.442695040889,143 3.307428525192, -9.965784284662144 };145 146 51 static double results_sin[OPERANDS] = { 147 52 -0.350783227690, -0.863209366649, -0.506365641110, -0.262374853704, 148 53 0.158533380044, 0.0, 0.980815184715, -0.206379975025, -0.044182448332, 149 54 0.841470984808 150 };151 152 static double results_sinh[OPERANDS] = {153 16.542627287635, -4.021856742157, 2592352764293536022528.000000000000,154 0.000000000000, 1.175201193644, 270182.468622420449, -1.335647470124,155 -122.343922746391, 0.100166750020, -23035933171656458903220125696.0156 };157 158 static double results_sqrt[OPERANDS] = {159 1.870828693387, 10.000000000000, 7.071067811865, 27.718506453271,160 32.870959812576, 1.000000000000, 8.124038404636, 1.648721270700,161 3.146426544510, 0.031622776602162 };163 164 static double results_tan[OPERANDS] = {165 0.374585640159, 1.709846542905, -0.587213915157, -0.271900611998,166 0.160563932839, 0.000000000000, -5.031371570891, -0.210920691722,167 0.044225635601, 1.557407724655168 };169 170 static double results_tanh[OPERANDS] = {171 0.998177897611, -0.970451936613, 1.000000000000, 0.000000000000,172 0.761594155956, 0.999999999993, -0.800499021761, -0.999966597156,173 0.000001000000, -1.000000000000174 55 }; 175 56 … … 217 98 218 99 for (unsigned int i = 0; i < OPERANDS; i++) { 219 double res = acos(arguments_acos[i]);220 221 if (!cmp_double(res, results_acos[i])) {222 TPRINTF("Double precision acos failed "223 "(%lf != %lf, arg %u)\n", res, results_acos[i], i);224 fail = true;225 }226 }227 228 for (unsigned int i = 0; i < OPERANDS; i++) {229 float res = acosf(arguments_acos[i]);230 231 if (!cmp_float(res, results_acos[i])) {232 TPRINTF("Single precision acos failed "233 "(%f != %lf, arg %u)\n", res, results_acos[i], i);234 fail = true;235 }236 }237 238 for (unsigned int i = 0; i < OPERANDS; i++) {239 double res = asin(arguments_asin[i]);240 241 if (!cmp_double(res, results_asin[i])) {242 TPRINTF("Double precision asin failed "243 "(%lf != %lf, arg %u)\n", res, results_asin[i], i);244 fail = true;245 }246 }247 248 for (unsigned int i = 0; i < OPERANDS; i++) {249 float res = asinf(arguments_asin[i]);250 251 if (!cmp_float(res, results_asin[i])) {252 TPRINTF("Single precision asin failed "253 "(%f != %lf, arg %u)\n", res, results_asin[i], i);254 fail = true;255 }256 }257 258 for (unsigned int i = 0; i < OPERANDS; i++) {259 double res = atan(arguments_atan[i]);260 261 if (!cmp_double(res, results_atan[i])) {262 TPRINTF("Double precision atan failed "263 "(%.12lf != %.12lf, arg %u)\n", res, results_atan[i], i);264 fail = true;265 }266 }267 268 for (unsigned int i = 0; i < OPERANDS; i++) {269 float res = atanf(arguments_atan[i]);270 271 if (!cmp_float(res, results_atan[i])) {272 TPRINTF("Single precision atan failed "273 "(%f != %lf, arg %u)\n", res, results_atan[i], i);274 fail = true;275 }276 }277 278 for (unsigned int i = 0; i < OPERANDS; i++) {279 double res = ceil(arguments[i]);280 281 if (!cmp_double(res, results_ceil[i])) {282 TPRINTF("Double precision ceil failed "283 "(%lf != %lf, arg %u)\n", res, results_ceil[i], i);284 fail = true;285 }286 }287 288 for (unsigned int i = 0; i < OPERANDS; i++) {289 float res = ceilf(arguments[i]);290 291 if (!cmp_float(res, results_ceil[i])) {292 TPRINTF("Single precision ceil failed "293 "(%f != %lf, arg %u)\n", res, results_ceil[i], i);294 fail = true;295 }296 }297 298 for (unsigned int i = 0; i < OPERANDS; i++) {299 100 double res = cos(arguments[i]); 300 101 … … 317 118 318 119 for (unsigned int i = 0; i < OPERANDS; i++) { 319 double res = cosh(arguments_exp[i]);320 321 if (!cmp_double(res, results_cosh[i])) {322 TPRINTF("Double precision cosh failed "323 "(%lf != %lf, arg %u)\n", res, results_cosh[i], i);324 fail = true;325 }326 }327 328 for (unsigned int i = 0; i < OPERANDS; i++) {329 float res = coshf(arguments_exp[i]);330 331 if (!cmp_float(res, results_cosh[i])) {332 TPRINTF("Single precision cosh failed "333 "(%f != %lf, arg %u)\n", res, results_cosh[i], i);334 fail = true;335 }336 }337 338 for (unsigned int i = 0; i < OPERANDS; i++) {339 double res = exp(arguments_exp[i]);340 341 if (!cmp_double(res, results_exp[i])) {342 TPRINTF("Double precision exp failed "343 "(%lf != %lf, arg %u)\n", res, results_exp[i], i);344 fail = true;345 }346 }347 348 for (unsigned int i = 0; i < OPERANDS; i++) {349 float res = expf(arguments_exp[i]);350 351 if (!cmp_float(res, results_exp[i])) {352 TPRINTF("Single precision exp failed "353 "(%f != %lf, arg %u)\n", res, results_exp[i], i);354 fail = true;355 }356 }357 358 for (unsigned int i = 0; i < OPERANDS; i++) {359 double res = fabs(arguments[i]);360 361 if (!cmp_double(res, results_fabs[i])) {362 TPRINTF("Double precision fabs failed "363 "(%lf != %lf, arg %u)\n", res, results_fabs[i], i);364 fail = true;365 }366 }367 368 for (unsigned int i = 0; i < OPERANDS; i++) {369 float res = fabsf(arguments[i]);370 371 if (!cmp_float(res, results_fabs[i])) {372 TPRINTF("Single precision fabs failed "373 "(%f != %lf, arg %u)\n", res, results_fabs[i], i);374 fail = true;375 }376 }377 378 for (unsigned int i = 0; i < OPERANDS; i++) {379 double res = floor(arguments[i]);380 381 if (!cmp_double(res, results_floor[i])) {382 TPRINTF("Double precision floor failed "383 "(%lf != %lf, arg %u)\n", res, results_floor[i], i);384 fail = true;385 }386 }387 388 for (unsigned int i = 0; i < OPERANDS; i++) {389 float res = floorf(arguments[i]);390 391 if (!cmp_float(res, results_floor[i])) {392 TPRINTF("Single precision floor failed "393 "(%f != %lf, arg %u)\n", res, results_floor[i], i);394 fail = true;395 }396 }397 398 for (unsigned int i = 0; i < OPERANDS; i++) {399 double res = log(arguments_log[i]);400 401 if (!cmp_double(res, results_log[i])) {402 TPRINTF("Double precision log failed "403 "(%lf != %lf, arg %u)\n", res, results_log[i], i);404 fail = true;405 }406 }407 408 for (unsigned int i = 0; i < OPERANDS; i++) {409 float res = logf(arguments_log[i]);410 411 if (!cmp_float(res, results_log[i])) {412 TPRINTF("Single precision log failed "413 "(%f != %lf, arg %u)\n", res, results_log[i], i);414 fail = true;415 }416 }417 418 for (unsigned int i = 0; i < OPERANDS; i++) {419 double res = log10(arguments_log[i]);420 421 if (!cmp_double(res, results_log10[i])) {422 TPRINTF("Double precision log10 failed "423 "(%lf != %lf, arg %u)\n", res, results_log10[i], i);424 fail = true;425 }426 }427 428 for (unsigned int i = 0; i < OPERANDS; i++) {429 float res = log10f(arguments_log[i]);430 431 if (!cmp_float(res, results_log10[i])) {432 TPRINTF("Single precision log10 failed "433 "(%f != %lf, arg %u)\n", res, results_log10[i], i);434 fail = true;435 }436 }437 438 for (unsigned int i = 0; i < OPERANDS; i++) {439 double res = log2(arguments_log[i]);440 441 if (!cmp_double(res, results_log2[i])) {442 TPRINTF("Double precision log2 failed "443 "(%lf != %lf, arg %u)\n", res, results_log2[i], i);444 fail = true;445 }446 }447 448 for (unsigned int i = 0; i < OPERANDS; i++) {449 float res = log2f(arguments_log[i]);450 451 if (!cmp_float(res, results_log2[i])) {452 TPRINTF("Single precision log2 failed "453 "(%f != %lf, arg %u)\n", res, results_log2[i], i);454 fail = true;455 }456 }457 458 for (unsigned int i = 0; i < OPERANDS; i++) {459 120 double res = sin(arguments[i]); 460 121 … … 472 133 TPRINTF("Single precision sin failed " 473 134 "(%f != %lf, arg %u)\n", res, results_sin[i], i); 474 fail = true;475 }476 }477 478 for (unsigned int i = 0; i < OPERANDS; i++) {479 double res = sinh(arguments_exp[i]);480 481 if (!cmp_double(res, results_sinh[i])) {482 TPRINTF("Double precision sinh failed "483 "(%lf != %lf, arg %u)\n", res, results_sinh[i], i);484 fail = true;485 }486 }487 488 for (unsigned int i = 0; i < OPERANDS; i++) {489 float res = sinhf(arguments_exp[i]);490 491 if (!cmp_float(res, results_sinh[i])) {492 TPRINTF("Single precision sinh failed "493 "(%f != %lf, arg %u)\n", res, results_sinh[i], i);494 fail = true;495 }496 }497 498 for (unsigned int i = 0; i < OPERANDS; i++) {499 double res = sqrt(arguments_sqrt[i]);500 501 if (!cmp_double(res, results_sqrt[i])) {502 TPRINTF("Double precision sqrt failed "503 "(%lf != %lf, arg %u)\n", res, results_sqrt[i], i);504 fail = true;505 }506 }507 508 for (unsigned int i = 0; i < OPERANDS; i++) {509 float res = sqrtf(arguments_sqrt[i]);510 511 if (!cmp_float(res, results_sqrt[i])) {512 TPRINTF("Single precision sqrt failed "513 "(%f != %lf, arg %u)\n", res, results_sqrt[i], i);514 fail = true;515 }516 }517 518 for (unsigned int i = 0; i < OPERANDS; i++) {519 double res = tan(arguments[i]);520 521 if (!cmp_double(res, results_tan[i])) {522 TPRINTF("Double precision tan failed "523 "(%lf != %lf, arg %u)\n", res, results_tan[i], i);524 fail = true;525 }526 }527 528 for (unsigned int i = 0; i < OPERANDS; i++) {529 float res = tanf(arguments[i]);530 531 if (!cmp_float(res, results_tan[i])) {532 TPRINTF("Single precision tan failed "533 "(%f != %lf, arg %u)\n", res, results_tan[i], i);534 fail = true;535 }536 }537 538 for (unsigned int i = 0; i < OPERANDS; i++) {539 double res = tanh(arguments_tanh[i]);540 541 if (!cmp_double(res, results_tanh[i])) {542 TPRINTF("Double precision tanh failed "543 "(%lf != %lf, arg %u)\n", res, results_tanh[i], i);544 fail = true;545 }546 }547 548 for (unsigned int i = 0; i < OPERANDS; i++) {549 float res = tanhf(arguments_tanh[i]);550 551 if (!cmp_float(res, results_tanh[i])) {552 TPRINTF("Single precision tanh failed "553 "(%f != %lf, arg %u)\n", res, results_tanh[i], i);554 135 fail = true; 555 136 } -
uspace/lib/c/include/fenv.h
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * 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 _FENV_H 30 #define _FENV_H 34 31 35 #ifndef LIBMATH_EXP_H_ 36 #define LIBMATH_EXP_H_ 32 // TODO 37 33 38 #include <mathtypes.h> 34 #define FE_TOWARDZERO 0 35 #define FE_TONEAREST 1 36 #define FE_UPWARD 2 37 #define FE_DOWNWARD 3 39 38 40 extern float32_t float32_exp(float32_t); 41 extern float64_t float64_exp(float64_t); 39 #define fegetround() FE_TONEAREST 42 40 43 41 #endif 44 42 45 /** @}46 */ -
uspace/lib/c/include/float.h
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * 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_ATAN_H_ 36 #define LIBMATH_ATAN_H_ 32 // FIXME: <float.h> is freestanding. Just include the compiler-provided file. 37 33 38 #include <mathtypes.h> 34 #define FLT_MIN __FLT_MIN__ 35 #define FLT_DENORM_MIN __FLT_DENORM_MIN__ 36 #define FLT_EPSILON __FLT_EPSILON__ 39 37 40 extern float32_t float32_atan(float32_t); 41 extern float64_t float64_atan(float64_t); 38 #define FLT_MANT_DIG __FLT_MANT_DIG__ 39 #define DBL_MANT_DIG __DBL_MANT_DIG__ 40 #define FLT_MAX_EXP __FLT_MAX_EXP__ 41 #define DBL_MAX_EXP __DBL_MAX_EXP__ 42 42 43 43 #endif 44 44 45 /** @}46 */ -
uspace/lib/math/Makefile
r7f7d642 r516e780 30 30 ROOT_PATH = $(USPACE_PREFIX)/.. 31 31 32 CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config33 34 32 LIBRARY = libmath 35 33 SOVERSION = 0.0 36 34 37 EXTRA_CFLAGS += -Iarch/$(UARCH)/include 38 39 -include $(CONFIG_MAKEFILE) 40 -include arch/$(UARCH)/Makefile.inc 41 42 GENERIC_SOURCES = \ 43 generic/acos.c \ 44 generic/asin.c \ 45 generic/atan.c \ 46 generic/atan2.c \ 47 generic/ceil.c \ 48 generic/cosh.c \ 49 generic/exp.c \ 35 SOURCES = \ 36 generic/__fcompare.c \ 37 generic/__fpclassify.c \ 38 generic/__signbit.c \ 50 39 generic/fabs.c \ 51 generic/floor.c \52 40 generic/fmod.c \ 53 generic/frexp.c \ 54 generic/ldexp.c \ 55 generic/log.c \ 56 generic/log10.c \ 57 generic/log2.c \ 58 generic/modf.c \ 59 generic/pow.c \ 60 generic/sinh.c \ 61 generic/sqrt.c \ 62 generic/tan.c \ 63 generic/tanh.c \ 41 generic/nearbyint.c \ 42 generic/round.c \ 64 43 generic/trig.c \ 65 44 generic/trunc.c 66 45 67 SOURCES = \68 $(GENERIC_SOURCES)\69 $(ARCH_SOURCES)46 TEST_SOURCES = \ 47 test/rounding.c \ 48 test/main.c 70 49 71 50 include $(USPACE_PREFIX)/Makefile.common -
uspace/lib/math/generic/__fcompare.c
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * 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 __fcompare(size_t sz1, size_t sz2, ...) 37 { 38 va_list ap; 39 va_start(ap, sz2); 34 40 35 #include <ceil.h> 36 #include <math.h> 37 #include <mathtypes.h> 41 long double val1; 42 long double val2; 38 43 39 /** Ceiling (round towards positive infinity, 32-bit floating point) 40 * 41 * @param val Floating point number. 42 * 43 * @return Number rounded towards positive infinity. 44 */ 45 float32_t float32_ceil(float32_t val) 46 { 47 float32_u t; 48 float32_u v; 49 float32_u r; 50 51 v.val = val; 52 t.val = trunc_f32(val); 53 54 if (v.data.parts.sign == 1 || val == t.val) { 55 r = t; 56 } else { 57 r.val = t.val + 1.0; 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; 58 54 } 59 55 60 return r.val; 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 } 67 68 va_end(ap); 69 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 } 61 81 } 62 82 63 /** Ceiling (round towards positive infinity, 64-bit floating point)64 *65 * @param val Floating point number.66 *67 * @return Number rounded towards positive infinity.68 */69 float64_t float64_ceil(float64_t val)70 {71 float64_u t;72 float64_u v;73 float64_u r;74 75 v.val = val;76 t.val = trunc_f64(val);77 78 if (v.data.parts.sign == 1 || val == t.val) {79 r = t;80 } else {81 r.val = t.val + 1.0;82 }83 84 return r.val;85 }86 87 /** @}88 */ -
uspace/lib/math/generic/__fpclassify.c
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * 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 __fpclassify(size_t sz, ...) 37 { 38 va_list ap; 39 va_start(ap, sz); 34 40 35 #include <math.h> 36 #include <sqrt.h> 41 int result; 37 42 38 /** Single precision square root 39 * 40 * Compute square root. 41 * 42 * @param val Value 43 * 44 * @return Square root. 45 * 46 */ 47 float32_t float32_sqrt(float32_t val) 48 { 49 return pow_f32(val, 0.5); 43 switch (sz) { 44 case 4: 45 result = fpclassify((float) va_arg(ap, double)); 46 break; 47 case 8: 48 result = fpclassify(va_arg(ap, double)); 49 break; 50 default: 51 result = fpclassify(va_arg(ap, long double)); 52 break; 53 } 54 55 va_end(ap); 56 return result; 50 57 } 51 58 52 /** Double precision square root53 *54 * Compute squre root.55 *56 * @param val Value57 *58 * @return Square root.59 *60 */61 float64_t float64_sqrt(float64_t val)62 {63 return pow_f64(val, 0.5);64 }65 66 /** @}67 */ -
uspace/lib/math/generic/__signbit.c
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * 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 #include <math.h> 36 #include <tan.h> 41 int result; 37 42 38 /** Tangent (32-bit floating point) 39 * 40 * Compute tangent value. 41 * 42 * @param arg Tangent argument. 43 * 44 * @return Tangent value. 45 * 46 */ 47 float32_t float32_tan(float32_t arg) 48 { 49 return sin_f32(arg) / cos_f32(arg); 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 } 54 55 va_end(ap); 56 return result; 50 57 } 51 58 52 /** Sine (64-bit floating point)53 *54 * Compute sine value.55 *56 * @param arg Sine argument.57 *58 * @return Sine value.59 *60 */61 float64_t float64_tan(float64_t arg)62 {63 return sin_f64(arg) / cos_f64(arg);64 }65 66 67 /** @}68 */ -
uspace/lib/math/generic/fabs.c
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * Copyright (c) 2014 Martin Decky 3 3 * All rights reserved. 4 4 * … … 34 34 35 35 #include <math.h> 36 #include <fabs.h>37 36 38 /** Absolute value (32-bit floating point) 39 * 40 * Compute absolute value. 41 * 42 * @param arg Argument. 43 * 44 * @return Absolute value. 45 * 46 */ 47 float32_t float32_fabs(float32_t arg) 37 float fabsf(float val) 48 38 { 49 if (arg < 0.0) 50 return -arg; 51 else 52 return arg; 39 return copysignf(val, 1.0f); 53 40 } 54 41 55 /** Absolute value (64-bit floating point) 56 * 57 * Compute absolute value. 58 * 59 * @param arg Argument. 60 * 61 * @return Absolute value. 62 * 63 */ 64 float64_t float64_fabs(float64_t arg) 42 double fabs(double val) 65 43 { 66 if (arg < 0.0) 67 return -arg; 68 else 69 return arg; 44 return copysign(val, 1.0); 70 45 } 71 46 -
uspace/lib/math/generic/fmod.c
r7f7d642 r516e780 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/round.c
r7f7d642 r516e780 35 35 36 36 #include <math.h> 37 #include <pow.h> 37 #include <float.h> 38 #include <stdint.h> 38 39 39 /** Single precision power 40 * 41 * Compute power value. 42 * 43 * @param x Base 44 * @param y Exponent 45 * 46 * @return Cosine value. 47 * 40 /** 41 * Rounds its argument to the nearest integer value in floating-point format, 42 * rounding halfway cases away from zero, regardless of the current rounding 43 * direction. 48 44 */ 49 float 32_t float32_pow(float32_t x, float32_t y)45 float roundf(float val) 50 46 { 51 /* x^y = (e ^ log(x))^y = e ^ (log(x) * y) */ 52 return exp_f32(log_f32(x) * y); 47 const int exp_bias = FLT_MAX_EXP - 1; 48 const int mant_bits = FLT_MANT_DIG - 1; 49 50 union { 51 float f; 52 uint32_t i; 53 } u = { .f = fabsf(val) }; 54 55 int exp = (u.i >> mant_bits) - exp_bias; 56 57 /* If value is less than 0.5, return zero with appropriate sign. */ 58 if (exp < -1) 59 return copysignf(0.0f, val); 60 61 /* If exponent is exactly mant_bits, adding 0.5 could change the result. */ 62 if (exp >= mant_bits) 63 return val; 64 65 /* Use trunc with adjusted value to do the rounding. */ 66 return copysignf(truncf(u.f + 0.5), val); 53 67 } 54 68 55 /** Double precision power 56 * 57 * Compute power value. 58 * 59 * @param x Base 60 * @param y Exponent 61 * 62 * @return Cosine value. 63 * 69 /** 70 * Rounds its argument to the nearest integer value in floating-point format, 71 * rounding halfway cases away from zero, regardless of the current rounding 72 * direction. 64 73 */ 65 float64_t float64_pow(float64_t x, float64_t y)74 double round(double val) 66 75 { 67 /* x^y = (e ^ log(x))^y = e ^ (log(x) * y) */ 68 return exp_f64(log_f64(x) * y); 76 const int exp_bias = DBL_MAX_EXP - 1; 77 const int mant_bits = DBL_MANT_DIG - 1; 78 79 union { 80 double f; 81 uint64_t i; 82 } u = { .f = fabs(val) }; 83 84 int exp = ((int)(u.i >> mant_bits)) - exp_bias; 85 86 /* If value is less than 0.5, return zero with appropriate sign. */ 87 if (exp < -1) 88 return copysign(0.0, val); 89 90 /* If exponent is exactly mant_bits, adding 0.5 could change the result. */ 91 if (exp >= mant_bits) 92 return val; 93 94 /* Use trunc with adjusted value to do the rounding. */ 95 return copysign(trunc(u.f + 0.5), val); 69 96 } 70 97 -
uspace/lib/math/generic/trig.c
r7f7d642 r516e780 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
r7f7d642 r516e780 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 const int exp_bias = FLT_MAX_EXP - 1; 58 const int mant_bits = FLT_MANT_DIG - 1; 59 const uint32_t mant_mask = (UINT32_C(1) << mant_bits) - 1; 58 60 59 v.val = val; 60 exp = v.data.parts.exp - FLOAT32_BIAS; 61 union { 62 float f; 63 uint32_t i; 64 } u = { .f = fabsf(val) }; 61 65 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 } 66 int exp = (u.i >> mant_bits) - exp_bias; 71 67 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 } 68 /* If value is less than one, return zero with appropriate sign. */ 69 if (exp < 0) 70 return copysignf(0.0f, val); 77 71 78 return v.val; 72 if (exp >= mant_bits) 73 return val; 74 75 /* Truncate irrelevant fraction bits */ 76 u.i &= ~(mant_mask >> exp); 77 return copysignf(u.f, val); 79 78 } 80 79 … … 94 93 * 95 94 */ 96 float64_t float64_trunc(float64_tval)95 double trunc(double val) 97 96 { 98 float64_u v; 99 int32_t exp; 97 const int exp_bias = DBL_MAX_EXP - 1; 98 const int mant_bits = DBL_MANT_DIG - 1; 99 const uint64_t mant_mask = (UINT64_C(1) << mant_bits) - 1; 100 100 101 v.val = val; 102 exp = v.data.parts.exp - FLOAT64_BIAS; 101 union { 102 double f; 103 uint64_t i; 104 } u = { .f = fabs(val) }; 103 105 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 } 106 int exp = ((int)(u.i >> mant_bits)) - exp_bias; 113 107 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 } 108 /* If value is less than one, return zero with appropriate sign. */ 109 if (exp < 0) 110 return copysign(0.0, val); 119 111 120 return v.val; 112 if (exp >= mant_bits) 113 return val; 114 115 /* Truncate irrelevant fraction bits */ 116 u.i &= ~(mant_mask >> exp); 117 return copysign(u.f, val); 121 118 } 122 119 -
uspace/lib/math/test/main.c
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 5 Jiri Svoboda2 * Copyright (c) 2014 Vojtech Horky 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libmath 30 * @{ 31 */ 32 /** @file 33 */ 29 #include <stdio.h> 30 #include <pcut/pcut.h> 34 31 35 #ifndef LIBMATH_LOG_H_ 36 #define LIBMATH_LOG_H_ 32 PCUT_INIT; 37 33 38 #include <mathtypes.h> 34 PCUT_IMPORT(rounding); 39 35 40 extern float32_t float32_log(float32_t); 41 extern float64_t float64_log(float64_t); 36 PCUT_MAIN(); 42 37 43 #endif44 45 /** @}46 */ -
uspace/lib/posix/include/posix/math.h
r7f7d642 r516e780 1 1 /* 2 * Copyright (c) 201 1 Petr Koupy2 * Copyright (c) 2018 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 30 30 * @{ 31 31 */ 32 /** @file Mathematical operations.33 *34 * The purpose of this file is only to provide prototypes of mathematical35 * functions defined by C standard and by POSIX.36 *37 * It is up to the application to correctly link with either libmath38 * (provided by HelenOS) or by some other math library (such as fdlibm).39 */40 32 41 33 #ifndef POSIX_MATH_H_ 42 34 #define POSIX_MATH_H_ 43 35 44 #ifdef __GNUC__ 45 #define HUGE_VAL (__builtin_huge_val()) 36 /* 37 * Just a pass-through to libc math.h 38 */ 39 #include "libc/math.h" 40 46 41 #endif 47 48 extern double ldexp(double, int);49 extern double frexp(double, int *);50 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);77 78 #endif /* POSIX_MATH_H_ */79 42 80 43 /** @} -
uspace/lib/softfloat/Makefile
r7f7d642 r516e780 30 30 USPACE_PREFIX = ../.. 31 31 LIBRARY = libsoftfloat 32 EXTRA_CFLAGS += $(LIBMATH_INCLUDES_FLAGS)33 32 34 33 SOURCES = \ -
uspace/lib/softfloat/comparison.c
r7f7d642 r516e780 681 681 } 682 682 683 int __aeabi_fcmpun(float32_t a, float32_t b) 684 { 685 float32_u ua; 686 ua.val = a; 687 688 float32_u ub; 689 ub.val = b; 690 691 // TODO: sigNaNs 692 return is_float32_nan(ua.data) || is_float32_nan(ub.data); 693 } 694 683 695 #endif 684 696 … … 920 932 } 921 933 934 int __aeabi_dcmpun(float64_t a, float64_t b) 935 { 936 float64_u ua; 937 ua.val = a; 938 939 float64_u ub; 940 ub.val = b; 941 942 // TODO: sigNaNs 943 return is_float64_nan(ua.data) || is_float64_nan(ub.data); 944 } 945 922 946 #endif 923 947 -
uspace/lib/softfloat/comparison.h
r7f7d642 r516e780 93 93 extern int __aeabi_fcmple(float32_t, float32_t); 94 94 extern int __aeabi_fcmpeq(float32_t, float32_t); 95 extern int __aeabi_fcmpun(float32_t, float32_t); 95 96 #endif 96 97 … … 109 110 extern int __aeabi_dcmpge(float64_t, float64_t); 110 111 extern int __aeabi_dcmple(float64_t, float64_t); 112 extern int __aeabi_dcmpun(float64_t, float64_t); 111 113 #endif 112 114
Note:
See TracChangeset
for help on using the changeset viewer.