[b5440cf] | 1 | /*
|
---|
[9adb61d] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[d9be488] | 3 | * Copyright (c) 2014 Martin Decky
|
---|
[b5440cf] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[47f7390f] | 30 | /** @addtogroup libmath
|
---|
[846848a6] | 31 | * @{
|
---|
| 32 | */
|
---|
[d9be488] | 33 | /** @file
|
---|
[846848a6] | 34 | */
|
---|
| 35 |
|
---|
[d9be488] | 36 | #include <mathtypes.h>
|
---|
| 37 | #include <trunc.h>
|
---|
[b5440cf] | 38 |
|
---|
[992ffa6] | 39 | /** Truncate fractional part (round towards zero)
|
---|
| 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 | */
|
---|
[e6f5766] | 54 | float32_t float32_trunc(float32_t val)
|
---|
[992ffa6] | 55 | {
|
---|
[e6f5766] | 56 | float32_u v;
|
---|
| 57 | int32_t exp;
|
---|
| 58 |
|
---|
| 59 | v.val = val;
|
---|
| 60 | exp = v.data.parts.exp - FLOAT32_BIAS;
|
---|
[992ffa6] | 61 |
|
---|
| 62 | if (exp < 0) {
|
---|
| 63 | /* -1 < val < 1 => result is +0 or -0 */
|
---|
[e6f5766] | 64 | v.data.parts.exp = 0;
|
---|
| 65 | v.data.parts.fraction = 0;
|
---|
[992ffa6] | 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 */
|
---|
[e6f5766] | 75 | v.data.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp);
|
---|
[992ffa6] | 76 | }
|
---|
| 77 |
|
---|
[e6f5766] | 78 | return v.val;
|
---|
[992ffa6] | 79 | }
|
---|
| 80 |
|
---|
[47f7390f] | 81 | /** Truncate fractional part (round towards zero)
|
---|
| 82 | *
|
---|
| 83 | * Truncate the fractional part of IEEE 754 double
|
---|
| 84 | * precision floating point number by zeroing fraction
|
---|
| 85 | * bits, effectively rounding the number towards zero
|
---|
| 86 | * to the nearest whole number.
|
---|
| 87 | *
|
---|
| 88 | * If the argument is infinity or NaN, an exception
|
---|
| 89 | * should be indicated. This is not implemented yet.
|
---|
| 90 | *
|
---|
| 91 | * @param val Floating point number.
|
---|
| 92 | *
|
---|
| 93 | * @return Number rounded towards zero.
|
---|
| 94 | *
|
---|
| 95 | */
|
---|
[e6f5766] | 96 | float64_t float64_trunc(float64_t val)
|
---|
[d9be488] | 97 | {
|
---|
[e6f5766] | 98 | float64_u v;
|
---|
| 99 | int32_t exp;
|
---|
| 100 |
|
---|
| 101 | v.val = val;
|
---|
| 102 | exp = v.data.parts.exp - FLOAT64_BIAS;
|
---|
[d9be488] | 103 |
|
---|
| 104 | if (exp < 0) {
|
---|
| 105 | /* -1 < val < 1 => result is +0 or -0 */
|
---|
[e6f5766] | 106 | v.data.parts.exp = 0;
|
---|
| 107 | v.data.parts.fraction = 0;
|
---|
[d9be488] | 108 | } else if (exp >= FLOAT64_FRACTION_SIZE) {
|
---|
| 109 | if (exp == 1024) {
|
---|
| 110 | /* val is +inf, -inf or NaN => trigger an exception */
|
---|
| 111 | // FIXME TODO
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /* All bits in val are relevant for the result */
|
---|
| 115 | } else {
|
---|
| 116 | /* Truncate irrelevant fraction bits */
|
---|
[e6f5766] | 117 | v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
|
---|
[d9be488] | 118 | }
|
---|
| 119 |
|
---|
[e6f5766] | 120 | return v.val;
|
---|
[d9be488] | 121 | }
|
---|
[b5440cf] | 122 |
|
---|
[750636a] | 123 | /** @}
|
---|
[846848a6] | 124 | */
|
---|