Changeset 992ffa6 in mainline
- Timestamp:
- 2015-09-04T06:40:20Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 01cdd5a
- Parents:
- bae1e1f
- Location:
- uspace
- Files:
-
- 6 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/tester/float/float2.c
rbae1e1f r992ffa6 1 1 /* 2 2 * Copyright (c) 2014 Martin Decky 3 * Copyright (c) 2015 Jiri Svoboda 3 4 * All rights reserved. 4 5 * … … 27 28 */ 28 29 30 #include <stdbool.h> 29 31 #include <stdio.h> 30 32 #include <stdlib.h> … … 32 34 #include "../tester.h" 33 35 34 #define OPERANDS 10 35 #define PRECISION 100000000 36 #define OPERANDS 10 37 #define PRECISIONF 10000 38 #define PRECISION 100000000 36 39 37 40 static double arguments[OPERANDS] = { 38 41 3.5, -2.1, 100.0, 50.0, -1024.0, 0.0, 768.3156, 1080.499999, -600.0, 1.0 39 42 }; 43 44 static double arguments_exp[OPERANDS] = { 45 3.5, -2.1, 50.0, 0.0, 1.0, 13.2, -1.1, -5.5, 0.1, -66.0 46 }; 47 48 static double arguments_log[OPERANDS] = { 49 3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0, 50 2.718281828459045, 9.9, 0.001 51 }; 52 40 53 41 54 static double results_ceil[OPERANDS] = { … … 63 76 }; 64 77 78 static double results_log[OPERANDS] = { 79 1.252762968495, 4.605170185988, 3.912023005428, 6.644200586236, 80 6.985179175021, 0.000000000000, 4.189654742026, 1.000000000000, 81 2.292534757141, -6.907755278982 82 }; 83 84 static double results_exp[OPERANDS] = { 85 33.115451958692, 0.122456428253, 5184705528587072045056.0, 86 1.000000000000, 2.718281828459, 540364.937246691552, 0.332871083698, 87 0.004086771438, 1.105170918076, 0.000000000000 88 }; 89 90 static bool cmp_float(float a, float b) 91 { 92 float r; 93 94 /* XXX Need fabsf() */ 95 if (b < 1.0 / PRECISIONF && b > -1.0 / PRECISIONF) 96 r = a; 97 else 98 r = a / b - 1.0; 99 100 /* XXX Need fabsf() */ 101 if (r < 0.0) 102 r = -r; 103 104 return r < 1.0 / PRECISIONF; 105 } 106 107 static bool cmp_double(double a, double b) 108 { 109 double r; 110 111 /* XXX Need fabs() */ 112 if (b < 1.0 / PRECISION && b > -1.0 / PRECISION) 113 r = a; 114 else 115 r = a / b - 1.0; 116 117 /* XXX Need fabs() */ 118 if (r < 0.0) 119 r = -r; 120 121 return r < 1.0 / PRECISION; 122 } 123 65 124 const char *test_float2(void) 66 125 { 67 126 bool fail = false; 68 127 69 128 for (unsigned int i = 0; i < OPERANDS; i++) { 70 129 double res = floor(arguments[i]); … … 73 132 74 133 if (res_int != corr_int) { 75 TPRINTF("Double floor failed (%" PRId64 " !=%" PRId6476 " , arg %u)\n", res_int, corr_int, i);134 TPRINTF("Double precision floor failed (%" PRId64 135 " != %" PRId64 ", arg %u)\n", res_int, corr_int, i); 77 136 fail = true; 78 137 } … … 85 144 86 145 if (res_int != corr_int) { 87 TPRINTF("Double ceil failed (%" PRId64 " !=%" PRId6488 " , arg %u)\n", res_int, corr_int, i);146 TPRINTF("Double precision ceil failed (%" PRId64 147 " != %" PRId64 ", arg %u)\n", res_int, corr_int, i); 89 148 fail = true; 90 149 } … … 97 156 98 157 if (res_int != corr_int) { 99 TPRINTF("Double truncation failed (%" PRId64 " !=%" PRId64100 " , arg %u)\n", res_int, corr_int, i);158 TPRINTF("Double precisiontruncation failed (%" PRId64 159 " != %" PRId64 ", arg %u)\n", res_int, corr_int, i); 101 160 fail = true; 102 161 } … … 109 168 110 169 if (res_int != corr_int) { 111 TPRINTF("Double sine failed (%" PRId64 " !=%" PRId64112 " , arg %u)\n", res_int, corr_int, i);170 TPRINTF("Double precision sine failed (%" PRId64 171 " != %" PRId64 ", arg %u)\n", res_int, corr_int, i); 113 172 fail = true; 114 173 } … … 121 180 122 181 if (res_int != corr_int) { 123 TPRINTF("Double cosine failed (%" PRId64 " != %" PRId64 124 ", arg %u)\n", res_int, corr_int, i); 182 TPRINTF("Double precision cosine failed (%" PRId64 183 " != %" PRId64 ", arg %u)\n", res_int, corr_int, i); 184 fail = true; 185 } 186 } 187 188 for (unsigned int i = 0; i < OPERANDS; i++) { 189 float res = logf(arguments_log[i]); 190 191 if (!cmp_float(res, results_log[i])) { 192 TPRINTF("Single precision logarithm failed " 193 "(%lf != %lf, arg %u)\n", res, results_log[i], i); 194 fail = true; 195 } 196 } 197 198 for (unsigned int i = 0; i < OPERANDS; i++) { 199 double res = log(arguments_log[i]); 200 201 if (!cmp_double(res, results_log[i])) { 202 TPRINTF("Double precision logarithm failed " 203 "(%lf != %lf, arg %u)\n", res, results_log[i], i); 204 fail = true; 205 } 206 } 207 208 for (unsigned int i = 0; i < OPERANDS; i++) { 209 float res = exp(arguments_exp[i]); 210 211 if (!cmp_float(res, results_exp[i])) { 212 TPRINTF("Single precision exponential failed " 213 "(%lf != %lf, arg %u)\n", res, results_exp[i], i); 214 fail = true; 215 } 216 } 217 218 for (unsigned int i = 0; i < OPERANDS; i++) { 219 double res = exp(arguments_exp[i]); 220 221 if (!cmp_double(res, results_exp[i])) { 222 TPRINTF("Double precision exponential failed " 223 "(%lf != %lf, arg %u)\n", res, results_exp[i], i); 125 224 fail = true; 126 225 } -
uspace/lib/math/Makefile
rbae1e1f r992ffa6 42 42 GENERIC_SOURCES = \ 43 43 generic/ceil.c \ 44 generic/exp.c \ 44 45 generic/floor.c \ 46 generic/log.c \ 45 47 generic/trig.c \ 48 generic/pow.c \ 46 49 generic/mod.c \ 47 50 generic/trunc.c -
uspace/lib/math/arch/amd64/include/libarch/math.h
rbae1e1f r992ffa6 37 37 38 38 #include <ceil.h> 39 #include <exp.h> 39 40 #include <floor.h> 41 #include <log.h> 40 42 #include <mathtypes.h> 41 43 #include <mod.h> 44 #include <pow.h> 42 45 43 46 static inline float64_t fmod(float64_t dividend, float64_t divisor) … … 60 63 } 61 64 65 static inline float32_t expf(float32_t val) 66 { 67 return float32_exp(val); 68 } 69 70 static inline float64_t exp(float64_t val) 71 { 72 return float64_exp(val); 73 } 74 62 75 static inline float64_t floor(float64_t val) 63 76 { … … 71 84 } 72 85 86 static inline float32_t logf(float32_t val) 87 { 88 return float32_log(val); 89 } 90 91 static inline float64_t log(float64_t val) 92 { 93 return float64_log(val); 94 } 95 96 static inline float32_t powf(float32_t x, float32_t y) 97 { 98 return float32_pow(x, y); 99 } 100 101 static inline float64_t pow(float64_t x, float64_t y) 102 { 103 return float64_pow(x, y); 104 } 105 73 106 extern float64_t trunc(float64_t); 74 107 -
uspace/lib/math/generic/ceil.c
rbae1e1f r992ffa6 48 48 float64_u v; 49 49 float64_u r; 50 50 51 51 v.data = val; 52 52 t.data = trunc_float64(val); 53 53 54 54 if (val.parts.sign == 1 || v.val == t.val) { 55 55 r = t; … … 57 57 r.val = t.val + 1.0; 58 58 } 59 59 60 60 return r.data; 61 61 } -
uspace/lib/math/generic/floor.c
rbae1e1f r992ffa6 49 49 float64_u v; 50 50 float64_u r; 51 51 52 52 v.data = val; 53 53 t.data = trunc_float64(val); 54 54 55 55 if (val.parts.sign == 0 || v.val == t.val) { 56 56 r = t; … … 58 58 r.val = t.val - 1.0; 59 59 } 60 60 61 61 return r.data; 62 62 } -
uspace/lib/math/generic/trig.c
rbae1e1f r992ffa6 36 36 #include <trig.h> 37 37 38 #define TAYLOR_DEGREE 13 38 #define TAYLOR_DEGREE_32 13 39 #define TAYLOR_DEGREE_64 21 39 40 40 41 /** Precomputed values for factorial (starting from 1!) */ 41 static float64_t factorials[TAYLOR_DEGREE ] = {42 static float64_t factorials[TAYLOR_DEGREE_64] = { 42 43 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 43 479001600, 6227020800 44 479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L, 45 20922789888000.0L, 355687428096000.0L, 6402373705728000.0L, 46 121645100408832000.0L, 2432902008176640000.0L, 51090942171709440000.0L 44 47 }; 45 48 … … 61 64 float64_t nom = 1; 62 65 63 for (unsigned int i = 0; i < TAYLOR_DEGREE ; i++) {66 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { 64 67 nom *= arg; 65 68 … … 90 93 float64_t nom = 1; 91 94 92 for (unsigned int i = 0; i < TAYLOR_DEGREE ; i++) {95 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { 93 96 nom *= arg; 94 97 -
uspace/lib/math/generic/trunc.c
rbae1e1f r992ffa6 35 35 #include <mathtypes.h> 36 36 #include <trunc.h> 37 38 /** Truncate fractional part (round towards zero) 39 * 40 * Truncate the fractional part of IEEE 754 single 41 * precision floating point number by zeroing fraction 42 * bits, effectively rounding the number towards zero 43 * to the nearest whole number. 44 * 45 * If the argument is infinity or NaN, an exception 46 * should be indicated. This is not implemented yet. 47 * 48 * @param val Floating point number. 49 * 50 * @return Number rounded towards zero. 51 * 52 */ 53 float32 trunc_float32(float32 val) 54 { 55 int32_t exp = val.parts.exp - FLOAT32_BIAS; 56 57 if (exp < 0) { 58 /* -1 < val < 1 => result is +0 or -0 */ 59 val.parts.exp = 0; 60 val.parts.fraction = 0; 61 } else if (exp >= FLOAT32_FRACTION_SIZE) { 62 if (exp == 1024) { 63 /* val is +inf, -inf or NaN => trigger an exception */ 64 // FIXME TODO 65 } 66 67 /* All bits in val are relevant for the result */ 68 } else { 69 /* Truncate irrelevant fraction bits */ 70 val.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp); 71 } 72 73 return val; 74 } 37 75 38 76 /** Truncate fractional part (round towards zero) -
uspace/lib/math/include/math.h
rbae1e1f r992ffa6 38 38 #include <libarch/math.h> 39 39 40 #define M_PI 3.14159265358979323846 40 #define M_LN2 0.69314718055994530942 41 #define M_LOG2E 1.4426950408889634074 42 #define M_PI 3.14159265358979323846 41 43 42 44 #endif -
uspace/lib/math/include/trunc.h
rbae1e1f r992ffa6 38 38 #include <mathtypes.h> 39 39 40 extern float32 trunc_float32(float32); 40 41 extern float64 trunc_float64(float64); 41 42
Note:
See TracChangeset
for help on using the changeset viewer.