Changeset a35b458 in mainline for uspace/lib/math
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- uspace/lib/math
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/math/arch/amd64/src/cos.S
r3061bc1 ra35b458 35 35 pushq %rbp 36 36 movq %rsp, %rbp 37 37 38 38 # compute cosine (using red zone) 39 39 40 40 movsd %xmm0, -8(%rbp) 41 41 fldl -8(%rbp) 42 42 43 43 fcos 44 44 45 45 # detect if source operand is out of range 46 46 47 47 fnstsw %ax 48 48 andw $X87_STATUS_WORD_C2_MASK, %ax 49 49 jnz fix_range 50 50 51 51 fstpl -8(%rbp) 52 52 movsd -8(%rbp), %xmm0 53 53 54 54 leave 55 55 retq 56 56 57 57 # argument reduction 58 58 59 59 fix_range: 60 60 fldpi 61 61 fadd %st(0) 62 62 fxch %st(1) 63 63 64 64 reduce: 65 65 fprem1 … … 67 67 andw $X87_STATUS_WORD_C2_MASK, %ax 68 68 jnz reduce 69 69 70 70 fstp %st(1) 71 71 fcos 72 72 73 73 fstpl -8(%rbp) 74 74 movsd -8(%rbp), %xmm0 75 75 76 76 leave 77 77 retq -
uspace/lib/math/arch/amd64/src/sin.S
r3061bc1 ra35b458 35 35 pushq %rbp 36 36 movq %rsp, %rbp 37 37 38 38 # compute sine (using red zone) 39 39 40 40 movsd %xmm0, -8(%rbp) 41 41 fldl -8(%rbp) 42 42 43 43 fsin 44 44 45 45 # detect if source operand is out of range 46 46 47 47 fnstsw %ax 48 48 andw $X87_STATUS_WORD_C2_MASK, %ax 49 49 jnz fix_range 50 50 51 51 fstpl -8(%rbp) 52 52 movsd -8(%rbp), %xmm0 53 53 54 54 leave 55 55 retq 56 56 57 57 # argument reduction 58 58 59 59 fix_range: 60 60 fldpi 61 61 fadd %st(0) 62 62 fxch %st(1) 63 63 64 64 reduce: 65 65 fprem1 … … 67 67 andw $X87_STATUS_WORD_C2_MASK, %ax 68 68 jnz reduce 69 69 70 70 fstp %st(1) 71 71 fsin 72 72 73 73 fstpl -8(%rbp) 74 74 movsd -8(%rbp), %xmm0 75 75 76 76 leave 77 77 retq -
uspace/lib/math/arch/amd64/src/trunc.S
r3061bc1 ra35b458 35 35 pushq %rbp 36 36 movq %rsp, %rbp 37 37 38 38 # store x87 control word in the red zone 39 39 40 40 fnstcw -8(%rbp) 41 41 movw -8(%rbp), %ax 42 42 43 43 # set rounding control to truncate 44 44 # (no masking necessary for this flag) 45 45 46 46 orw $X87_CONTROL_WORD_RC_TRUNCATE, %ax 47 47 movw %ax, -16(%rbp) 48 48 fldcw -16(%rbp) 49 49 50 50 # truncate 51 51 52 52 movsd %xmm0, -16(%rbp) 53 53 fldl -16(%rbp) … … 55 55 fstpl -16(%rbp) 56 56 movsd -16(%rbp), %xmm0 57 57 58 58 # restore original control word 59 59 60 60 fldcw -8(%rbp) 61 61 62 62 leave 63 63 retq -
uspace/lib/math/arch/ia32/src/cos.S
r3061bc1 ra35b458 34 34 FUNCTION_BEGIN(cos_f64) 35 35 # compute cosine (no stack frame) 36 36 37 37 fldl 4(%esp) 38 38 fcos 39 39 40 40 # detect if source operand is out of range 41 41 42 42 fnstsw %ax 43 43 andw $X87_STATUS_WORD_C2_MASK, %ax 44 44 jnz fix_range 45 45 46 46 ret 47 47 48 48 # argument reduction 49 49 50 50 fix_range: 51 51 fldpi 52 52 fadd %st(0) 53 53 fxch %st(1) 54 54 55 55 reduce: 56 56 fprem1 … … 58 58 andw $X87_STATUS_WORD_C2_MASK, %ax 59 59 jnz reduce 60 60 61 61 fstp %st(1) 62 62 fcos 63 63 64 64 ret 65 65 FUNCTION_END(cos_f64) -
uspace/lib/math/arch/ia32/src/sin.S
r3061bc1 ra35b458 34 34 FUNCTION_BEGIN(sin_f64) 35 35 # compute sine (no stack frame) 36 36 37 37 fldl 4(%esp) 38 38 fsin 39 39 40 40 # detect if source operand is out of range 41 41 42 42 fnstsw %ax 43 43 andw $X87_STATUS_WORD_C2_MASK, %ax 44 44 jnz fix_range 45 45 46 46 ret 47 47 48 48 # argument reduction 49 49 50 50 fix_range: 51 51 fldpi 52 52 fadd %st(0) 53 53 fxch %st(1) 54 54 55 55 reduce: 56 56 fprem1 … … 58 58 andw $X87_STATUS_WORD_C2_MASK, %ax 59 59 jnz reduce 60 60 61 61 fstp %st(1) 62 62 fsin 63 63 64 64 ret 65 65 FUNCTION_END(sin_f64) -
uspace/lib/math/arch/ia32/src/trunc.S
r3061bc1 ra35b458 36 36 movl %esp, %ebp 37 37 subl $8, %esp 38 38 39 39 # store x87 control word 40 40 41 41 fnstcw -4(%ebp) 42 42 movw -4(%ebp), %ax 43 43 44 44 # set rounding control to truncate 45 45 # (no masking necessary for this flag) 46 46 47 47 orw $X87_CONTROL_WORD_RC_TRUNCATE, %ax 48 48 movw %ax, -8(%ebp) 49 49 fldcw -8(%ebp) 50 50 51 51 # truncate 52 52 53 53 fldl 8(%ebp) 54 54 frndint 55 55 56 56 # restore original control word 57 57 58 58 fldcw -4(%ebp) 59 59 60 60 leave 61 61 ret -
uspace/lib/math/generic/asin.c
r3061bc1 ra35b458 49 49 { 50 50 float32_t aval; 51 51 52 52 if (arg < -1.0 || arg > 1.0) { 53 53 errno = EDOM; 54 54 return FLOAT32_NAN; 55 55 } 56 56 57 57 aval = 2.0 * atan_f32(arg / (1.0 + sqrt_f32(1.0 - arg*arg))); 58 58 if (arg > 0.0) … … 74 74 { 75 75 float64_t aval; 76 76 77 77 if (arg < -1.0 || arg > 1.0) { 78 78 errno = EDOM; 79 79 return FLOAT64_NAN; 80 80 } 81 81 82 82 aval = 2.0 * atan_f64(arg / (1.0 + sqrt_f64(1.0 - arg*arg))); 83 83 if (arg > 0.0) -
uspace/lib/math/generic/atan.c
r3061bc1 ra35b458 54 54 float32_t sum = 0; 55 55 float32_t a = arg / (1.0 + arg * arg); 56 56 57 57 /* 58 58 * atan(z) = sum(n=0, +inf) [ (2^2n) * (n!)^2 / (2n + 1)! * 59 59 * z^(2n+1) / (1 + z^2)^(n+1) ] 60 60 */ 61 61 62 62 for (unsigned int n = 0; n < SERIES_DEGREE_32; n++) { 63 63 if (n > 0) { … … 68 68 a = a * 4.0 * arg * arg / (1.0 + arg * arg); 69 69 } 70 70 71 71 return sum; 72 72 } … … 86 86 float64_t sum = 0; 87 87 float64_t a = arg / (1.0 + arg * arg); 88 88 89 89 /* 90 90 * atan(z) = sum(n=0, +inf) [ (2^2n) * (n!)^2 / (2n + 1)! * 91 91 * z^(2n+1) / (1 + z^2)^(n+1) ] 92 92 */ 93 93 94 94 for (unsigned int n = 0; n < SERIES_DEGREE_64; n++) { 95 95 if (n > 0) { … … 100 100 a = a * 4.0 * arg * arg / (1.0 + arg * arg); 101 101 } 102 102 103 103 return sum; 104 104 } -
uspace/lib/math/generic/ceil.c
r3061bc1 ra35b458 48 48 float32_u v; 49 49 float32_u r; 50 50 51 51 v.val = val; 52 52 t.val = trunc_f32(val); 53 53 54 54 if (v.data.parts.sign == 1 || val == t.val) { 55 55 r = t; … … 57 57 r.val = t.val + 1.0; 58 58 } 59 59 60 60 return r.val; 61 61 } … … 72 72 float64_u v; 73 73 float64_u r; 74 74 75 75 v.val = val; 76 76 t.val = trunc_f64(val); 77 77 78 78 if (v.data.parts.sign == 1 || val == t.val) { 79 79 r = t; … … 81 81 r.val = t.val + 1.0; 82 82 } 83 83 84 84 return r.val; 85 85 } -
uspace/lib/math/generic/exp.c
r3061bc1 ra35b458 64 64 float32_t ret = 1; 65 65 float32_t nom = 1; 66 66 67 67 for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) { 68 68 nom *= arg; 69 69 ret += nom / factorials[i]; 70 70 } 71 71 72 72 return ret; 73 73 } … … 89 89 float64_t ret = 1; 90 90 float64_t nom = 1; 91 91 92 92 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { 93 93 nom *= arg; 94 94 ret += nom / factorials[i]; 95 95 } 96 96 97 97 return ret; 98 98 } -
uspace/lib/math/generic/floor.c
r3061bc1 ra35b458 48 48 float32_t t; 49 49 float32_u v; 50 50 51 51 v.val = val; 52 52 t = trunc_f32(val); 53 53 54 54 if (v.data.parts.sign == 0 || val == t) 55 55 return t; … … 69 69 float64_t t; 70 70 float64_u v; 71 71 72 72 v.val = val; 73 73 t = trunc_f64(val); 74 74 75 75 if (v.data.parts.sign == 0 || val == t) 76 76 return t; -
uspace/lib/math/generic/fmod.c
r3061bc1 ra35b458 55 55 { 56 56 // FIXME: replace with exact arithmetics 57 57 58 58 float32_t quotient = trunc_f32(dividend / divisor); 59 59 60 60 return (dividend - quotient * divisor); 61 61 } … … 80 80 { 81 81 // FIXME: replace with exact arithmetics 82 82 83 83 float64_t quotient = trunc_f64(dividend / divisor); 84 84 85 85 return (dividend - quotient * divisor); 86 86 } -
uspace/lib/math/generic/log.c
r3061bc1 ra35b458 55 55 float32_t ret = 0; 56 56 float32_t num = 1; 57 57 58 58 for (unsigned int i = 1; i <= TAYLOR_DEGREE_32; i++) { 59 59 num *= arg; 60 60 61 61 if ((i % 2) == 0) 62 62 ret += num / i; … … 64 64 ret -= num / i; 65 65 } 66 66 67 67 return ret; 68 68 } … … 83 83 float64_t ret = 0; 84 84 float64_t num = 1; 85 85 86 86 for (unsigned int i = 1; i <= TAYLOR_DEGREE_64; i++) { 87 87 num *= arg; 88 88 89 89 if ((i % 2) == 0) 90 90 ret += num / i; … … 92 92 ret -= num / i; 93 93 } 94 94 95 95 return ret; 96 96 } … … 107 107 float32_u m; 108 108 int e; 109 109 110 110 m.val = arg; 111 111 /* … … 118 118 e = m.data.parts.exp - (FLOAT32_BIAS - 1); 119 119 m.data.parts.exp = FLOAT32_BIAS - 1; 120 120 121 121 /* 122 122 * arg = m * 2^e ; log(arg) = log(m) + log(2^e) = … … 137 137 float64_u m; 138 138 int e; 139 139 140 140 m.val = arg; 141 141 142 142 /* 143 143 * Factor arg into m * 2^e where m has exponent -1, … … 149 149 e = m.data.parts.exp - (FLOAT64_BIAS - 1); 150 150 m.data.parts.exp = FLOAT64_BIAS - 1; 151 151 152 152 /* 153 153 * arg = m * 2^e ; log(arg) = log(m) + log(2^e) = -
uspace/lib/math/generic/trig.c
r3061bc1 ra35b458 64 64 float32_t ret = 0; 65 65 float32_t nom = 1; 66 66 67 67 for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) { 68 68 nom *= arg; 69 69 70 70 if ((i % 4) == 0) 71 71 ret += nom / factorials[i]; … … 73 73 ret -= nom / factorials[i]; 74 74 } 75 75 76 76 return ret; 77 77 } … … 93 93 float64_t ret = 0; 94 94 float64_t nom = 1; 95 95 96 96 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { 97 97 nom *= arg; 98 98 99 99 if ((i % 4) == 0) 100 100 ret += nom / factorials[i]; … … 102 102 ret -= nom / factorials[i]; 103 103 } 104 104 105 105 return ret; 106 106 } … … 122 122 float32_t ret = 1; 123 123 float32_t nom = 1; 124 124 125 125 for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) { 126 126 nom *= arg; 127 127 128 128 if ((i % 4) == 1) 129 129 ret -= nom / factorials[i]; … … 131 131 ret += nom / factorials[i]; 132 132 } 133 133 134 134 return ret; 135 135 } … … 151 151 float64_t ret = 1; 152 152 float64_t nom = 1; 153 153 154 154 for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) { 155 155 nom *= arg; 156 156 157 157 if ((i % 4) == 1) 158 158 ret -= nom / factorials[i]; … … 160 160 ret += nom / factorials[i]; 161 161 } 162 162 163 163 return ret; 164 164 } … … 179 179 { 180 180 unsigned int period = arg / (M_PI / 4); 181 181 182 182 switch (period) { 183 183 case 0: … … 212 212 { 213 213 unsigned int period = arg / (M_PI / 4); 214 214 215 215 switch (period) { 216 216 case 0: … … 245 245 { 246 246 unsigned int period = arg / (M_PI / 4); 247 247 248 248 switch (period) { 249 249 case 0: … … 278 278 { 279 279 unsigned int period = arg / (M_PI / 4); 280 280 281 281 switch (period) { 282 282 case 0: … … 308 308 { 309 309 float32_t base_arg = fmod_f32(arg, 2 * M_PI); 310 310 311 311 if (base_arg < 0) 312 312 return -base_sin_32(-base_arg); 313 313 314 314 return base_sin_32(base_arg); 315 315 } … … 327 327 { 328 328 float64_t base_arg = fmod_f64(arg, 2 * M_PI); 329 329 330 330 if (base_arg < 0) 331 331 return -base_sin_64(-base_arg); 332 332 333 333 return base_sin_64(base_arg); 334 334 } … … 346 346 { 347 347 float32_t base_arg = fmod_f32(arg, 2 * M_PI); 348 348 349 349 if (base_arg < 0) 350 350 return base_cos_32(-base_arg); 351 351 352 352 return base_cos_32(base_arg); 353 353 } … … 365 365 { 366 366 float64_t base_arg = fmod_f64(arg, 2 * M_PI); 367 367 368 368 if (base_arg < 0) 369 369 return base_cos_64(-base_arg); 370 370 371 371 return base_cos_64(base_arg); 372 372 } -
uspace/lib/math/generic/trunc.c
r3061bc1 ra35b458 56 56 float32_u v; 57 57 int32_t exp; 58 58 59 59 v.val = val; 60 60 exp = v.data.parts.exp - FLOAT32_BIAS; 61 61 62 62 if (exp < 0) { 63 63 /* -1 < val < 1 => result is +0 or -0 */ … … 69 69 // FIXME TODO 70 70 } 71 71 72 72 /* All bits in val are relevant for the result */ 73 73 } else { … … 75 75 v.data.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp); 76 76 } 77 77 78 78 return v.val; 79 79 } … … 98 98 float64_u v; 99 99 int32_t exp; 100 100 101 101 v.val = val; 102 102 exp = v.data.parts.exp - FLOAT64_BIAS; 103 103 104 104 if (exp < 0) { 105 105 /* -1 < val < 1 => result is +0 or -0 */ … … 111 111 // FIXME TODO 112 112 } 113 113 114 114 /* All bits in val are relevant for the result */ 115 115 } else { … … 117 117 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp); 118 118 } 119 119 120 120 return v.val; 121 121 } -
uspace/lib/math/include/mathtypes.h
r3061bc1 ra35b458 91 91 typedef union { 92 92 uint32_t bin; 93 93 94 94 struct { 95 95 uint32_t sign : 1; … … 101 101 typedef union { 102 102 uint64_t bin; 103 103 104 104 struct { 105 105 uint64_t sign : 1; … … 114 114 uint32_t lo; 115 115 } bin __attribute__((packed)); 116 116 117 117 struct { 118 118 uint64_t padding : 16; … … 128 128 uint64_t lo; 129 129 } bin __attribute__((packed)); 130 130 131 131 struct { 132 132 uint64_t sign : 1; … … 141 141 typedef union { 142 142 uint32_t bin; 143 143 144 144 struct { 145 145 uint32_t fraction : 23; … … 151 151 typedef union { 152 152 uint64_t bin; 153 153 154 154 struct { 155 155 uint64_t fraction : 52; … … 164 164 uint64_t hi; 165 165 } bin __attribute__((packed)); 166 166 167 167 struct { 168 168 uint64_t fraction : 64; … … 178 178 uint64_t hi; 179 179 } bin __attribute__((packed)); 180 180 181 181 struct { 182 182 uint64_t frac_lo : 64;
Note:
See TracChangeset
for help on using the changeset viewer.