[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 |
|
---|
[516e780] | 36 | #include <math.h>
|
---|
| 37 | #include <float.h>
|
---|
| 38 | #include <stdint.h>
|
---|
[b5440cf] | 39 |
|
---|
[992ffa6] | 40 | /** Truncate fractional part (round towards zero)
|
---|
| 41 | *
|
---|
| 42 | * Truncate the fractional part of IEEE 754 single
|
---|
| 43 | * precision floating point number by zeroing fraction
|
---|
| 44 | * bits, effectively rounding the number towards zero
|
---|
| 45 | * to the nearest whole number.
|
---|
| 46 | *
|
---|
| 47 | * If the argument is infinity or NaN, an exception
|
---|
| 48 | * should be indicated. This is not implemented yet.
|
---|
| 49 | *
|
---|
| 50 | * @param val Floating point number.
|
---|
| 51 | *
|
---|
| 52 | * @return Number rounded towards zero.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
[516e780] | 55 | float truncf(float val)
|
---|
[992ffa6] | 56 | {
|
---|
[516e780] | 57 | const int exp_bias = FLT_MAX_EXP - 1;
|
---|
| 58 | const int mant_bits = FLT_MANT_DIG - 1;
|
---|
| 59 | const uint32_t mant_mask = (UINT32_C(1) << mant_bits) - 1;
|
---|
[a35b458] | 60 |
|
---|
[516e780] | 61 | union {
|
---|
| 62 | float f;
|
---|
| 63 | uint32_t i;
|
---|
| 64 | } u = { .f = fabsf(val) };
|
---|
[a35b458] | 65 |
|
---|
[516e780] | 66 | int exp = (u.i >> mant_bits) - exp_bias;
|
---|
[a35b458] | 67 |
|
---|
[516e780] | 68 | /* If value is less than one, return zero with appropriate sign. */
|
---|
| 69 | if (exp < 0)
|
---|
| 70 | return copysignf(0.0f, val);
|
---|
[a35b458] | 71 |
|
---|
[516e780] | 72 | if (exp >= mant_bits)
|
---|
| 73 | return val;
|
---|
| 74 |
|
---|
| 75 | /* Truncate irrelevant fraction bits */
|
---|
| 76 | u.i &= ~(mant_mask >> exp);
|
---|
| 77 | return copysignf(u.f, val);
|
---|
[992ffa6] | 78 | }
|
---|
| 79 |
|
---|
[47f7390f] | 80 | /** Truncate fractional part (round towards zero)
|
---|
| 81 | *
|
---|
| 82 | * Truncate the fractional part of IEEE 754 double
|
---|
| 83 | * precision floating point number by zeroing fraction
|
---|
| 84 | * bits, effectively rounding the number towards zero
|
---|
| 85 | * to the nearest whole number.
|
---|
| 86 | *
|
---|
| 87 | * If the argument is infinity or NaN, an exception
|
---|
| 88 | * should be indicated. This is not implemented yet.
|
---|
| 89 | *
|
---|
| 90 | * @param val Floating point number.
|
---|
| 91 | *
|
---|
| 92 | * @return Number rounded towards zero.
|
---|
| 93 | *
|
---|
| 94 | */
|
---|
[516e780] | 95 | double trunc(double val)
|
---|
[d9be488] | 96 | {
|
---|
[516e780] | 97 | const int exp_bias = DBL_MAX_EXP - 1;
|
---|
| 98 | const int mant_bits = DBL_MANT_DIG - 1;
|
---|
| 99 | const uint64_t mant_mask = (UINT64_C(1) << mant_bits) - 1;
|
---|
| 100 |
|
---|
| 101 | union {
|
---|
| 102 | double f;
|
---|
| 103 | uint64_t i;
|
---|
| 104 | } u = { .f = fabs(val) };
|
---|
[a35b458] | 105 |
|
---|
[516e780] | 106 | int exp = ((int)(u.i >> mant_bits)) - exp_bias;
|
---|
[a35b458] | 107 |
|
---|
[516e780] | 108 | /* If value is less than one, return zero with appropriate sign. */
|
---|
| 109 | if (exp < 0)
|
---|
| 110 | return copysign(0.0, val);
|
---|
[a35b458] | 111 |
|
---|
[516e780] | 112 | if (exp >= mant_bits)
|
---|
| 113 | return val;
|
---|
[a35b458] | 114 |
|
---|
[516e780] | 115 | /* Truncate irrelevant fraction bits */
|
---|
| 116 | u.i &= ~(mant_mask >> exp);
|
---|
| 117 | return copysign(u.f, val);
|
---|
[d9be488] | 118 | }
|
---|
[b5440cf] | 119 |
|
---|
[750636a] | 120 | /** @}
|
---|
[846848a6] | 121 | */
|
---|