Changes in uspace/lib/math/generic/trunc.c [9adb61d:b336613f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/math/generic/trunc.c
r9adb61d rb336613f 1 1 /* 2 * Copyright (c) 2015 Jiri Svoboda3 2 * Copyright (c) 2014 Martin Decky 4 3 * All rights reserved. … … 39 38 /** Truncate fractional part (round towards zero) 40 39 * 41 * Truncate the fractional part of IEEE 754 single42 * precision floating point number by zeroing fraction43 * bits, effectively rounding the number towards zero44 * to the nearest whole number.45 *46 * If the argument is infinity or NaN, an exception47 * 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 TODO70 }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 *83 40 * Truncate the fractional part of IEEE 754 double 84 41 * precision floating point number by zeroing fraction … … 94 51 * 95 52 */ 96 float64 _t float64_trunc(float64_tval)53 float64 trunc_float64(float64 val) 97 54 { 98 float64_u v; 99 int32_t exp; 100 101 v.val = val; 102 exp = v.data.parts.exp - FLOAT64_BIAS; 55 int32_t exp = val.parts.exp - FLOAT64_BIAS; 103 56 104 57 if (exp < 0) { 105 58 /* -1 < val < 1 => result is +0 or -0 */ 106 v .data.parts.exp = 0;107 v .data.parts.fraction = 0;59 val.parts.exp = 0; 60 val.parts.fraction = 0; 108 61 } else if (exp >= FLOAT64_FRACTION_SIZE) { 109 62 if (exp == 1024) { … … 115 68 } else { 116 69 /* Truncate irrelevant fraction bits */ 117 v .data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);70 val.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp); 118 71 } 119 72 120 return v .val;73 return val; 121 74 } 122 75
Note:
See TracChangeset
for help on using the changeset viewer.