source: mainline/uspace/lib/math/generic/trunc.c@ 8565a42

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8565a42 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
3 * Copyright (c) 2014 Martin Decky
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
30/** @addtogroup libmath
31 * @{
32 */
33/** @file
34 */
35
36#include <mathtypes.h>
37#include <trunc.h>
38
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 */
54float32_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 *
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 */
96float64_t float64_trunc(float64_t val)
97{
98 float64_u v;
99 int32_t exp;
100
101 v.val = val;
102 exp = v.data.parts.exp - FLOAT64_BIAS;
103
104 if (exp < 0) {
105 /* -1 < val < 1 => result is +0 or -0 */
106 v.data.parts.exp = 0;
107 v.data.parts.fraction = 0;
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 */
117 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
118 }
119
120 return v.val;
121}
122
123/** @}
124 */
Note: See TracBrowser for help on using the repository browser.