source: mainline/uspace/lib/math/generic/round.c@ 10d65d70

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 10d65d70 was 516e780, checked in by GitHub <noreply@…>, 7 years ago

Strip down libmath. (#45)

libmath is mostly unused (except for trunc(), sin() and cos()), and most functions in it are either very imprecise or downright broken. Additionally, it is implemented in manner that conflicts with C standard. Instead of trying to fix all the shortcomings while maintaining unused functionality, I'm opting to simply remove most of it and only keep the parts that are currently necessary.

Later readdition of the removed functions is possible, but there needs to be a reliable way to evaluate their quality first.

  • Property mode set to 100644
File size: 3.0 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 <math.h>
37#include <float.h>
38#include <stdint.h>
39
40/**
41 * Rounds its argument to the nearest integer value in floating-point format,
42 * rounding halfway cases away from zero, regardless of the current rounding
43 * direction.
44 */
45float roundf(float val)
46{
47 const int exp_bias = FLT_MAX_EXP - 1;
48 const int mant_bits = FLT_MANT_DIG - 1;
49
50 union {
51 float f;
52 uint32_t i;
53 } u = { .f = fabsf(val) };
54
55 int exp = (u.i >> mant_bits) - exp_bias;
56
57 /* If value is less than 0.5, return zero with appropriate sign. */
58 if (exp < -1)
59 return copysignf(0.0f, val);
60
61 /* If exponent is exactly mant_bits, adding 0.5 could change the result. */
62 if (exp >= mant_bits)
63 return val;
64
65 /* Use trunc with adjusted value to do the rounding. */
66 return copysignf(truncf(u.f + 0.5), val);
67}
68
69/**
70 * Rounds its argument to the nearest integer value in floating-point format,
71 * rounding halfway cases away from zero, regardless of the current rounding
72 * direction.
73 */
74double round(double val)
75{
76 const int exp_bias = DBL_MAX_EXP - 1;
77 const int mant_bits = DBL_MANT_DIG - 1;
78
79 union {
80 double f;
81 uint64_t i;
82 } u = { .f = fabs(val) };
83
84 int exp = ((int)(u.i >> mant_bits)) - exp_bias;
85
86 /* If value is less than 0.5, return zero with appropriate sign. */
87 if (exp < -1)
88 return copysign(0.0, val);
89
90 /* If exponent is exactly mant_bits, adding 0.5 could change the result. */
91 if (exp >= mant_bits)
92 return val;
93
94 /* Use trunc with adjusted value to do the rounding. */
95 return copysign(trunc(u.f + 0.5), val);
96}
97
98/** @}
99 */
Note: See TracBrowser for help on using the repository browser.