Changeset 516e780 in mainline for uspace/lib/math/generic/round.c
- 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)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.