Changes in uspace/lib/math/generic/trunc.c [b336613f:9adb61d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/math/generic/trunc.c
rb336613f r9adb61d 1 1 /* 2 * Copyright (c) 2015 Jiri Svoboda 2 3 * Copyright (c) 2014 Martin Decky 3 4 * All rights reserved. … … 38 39 /** Truncate fractional part (round towards zero) 39 40 * 41 * Truncate the fractional part of IEEE 754 single 42 * precision floating point number by zeroing fraction 43 * bits, effectively rounding the number towards zero 44 * to the nearest whole number. 45 * 46 * If the argument is infinity or NaN, an exception 47 * should be indicated. This is not implemented yet. 48 * 49 * @param val Floating point number. 50 * 51 * @return Number rounded towards zero. 52 * 53 */ 54 float32_t float32_trunc(float32_t val) 55 { 56 float32_u v; 57 int32_t exp; 58 59 v.val = val; 60 exp = v.data.parts.exp - FLOAT32_BIAS; 61 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 } 71 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 } 77 78 return v.val; 79 } 80 81 /** Truncate fractional part (round towards zero) 82 * 40 83 * Truncate the fractional part of IEEE 754 double 41 84 * precision floating point number by zeroing fraction … … 51 94 * 52 95 */ 53 float64 trunc_float64(float64val)96 float64_t float64_trunc(float64_t val) 54 97 { 55 int32_t exp = val.parts.exp - FLOAT64_BIAS; 98 float64_u v; 99 int32_t exp; 100 101 v.val = val; 102 exp = v.data.parts.exp - FLOAT64_BIAS; 56 103 57 104 if (exp < 0) { 58 105 /* -1 < val < 1 => result is +0 or -0 */ 59 v al.parts.exp = 0;60 v al.parts.fraction = 0;106 v.data.parts.exp = 0; 107 v.data.parts.fraction = 0; 61 108 } else if (exp >= FLOAT64_FRACTION_SIZE) { 62 109 if (exp == 1024) { … … 68 115 } else { 69 116 /* Truncate irrelevant fraction bits */ 70 v al.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);117 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp); 71 118 } 72 119 73 return v al;120 return v.val; 74 121 } 75 122
Note:
See TracChangeset
for help on using the changeset viewer.