source: mainline/uspace/lib/c/generic/imath.c@ e3272101

Last change on this file since e3272101 was e3272101, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

make ilog10_u64() return an errno_t

The function ilog10_u64() used to return 0
for the value 0. Which is not correct. Either
NaN or -Infinity are correct, but not 0, since
it would be ambiguous with log(1). To ensure this
case the function ilog10_u64() has been changed
to return a errno_t indicating a failure.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/**
33 * @file Integer mathematical functions
34 */
35
36#include <errno.h>
37#include <imath.h>
38#include <stdbool.h>
39#include <stdint.h>
40
41/** Compute integer power of 10, unsigned 64-bit result.
42 *
43 * Fast algorithm using binary digits of exp to compute 10^exp in
44 * time O(log exp).
45 *
46 * @param exp Exponent
47 * @param res Place to store result
48 * @return EOK on success, ERANGE if result does not fit into result type
49 */
50errno_t ipow10_u64(unsigned exp, uint64_t *res)
51{
52 uint64_t a;
53 uint64_t r;
54
55 r = 1;
56 a = 10;
57 while (true) {
58 if ((exp & 1) != 0) {
59 if ((r * a) / a != r)
60 return ERANGE;
61 r = r * a;
62 }
63
64 exp = exp >> 1;
65 if (exp == 0)
66 break;
67
68 if ((a * a) / a != a)
69 return ERANGE;
70 a = a * a;
71 }
72
73 *res = r;
74 return EOK;
75}
76
77/** Compute integer base 10 logarithm, unsigned 64-bit argument.
78 *
79 * For integer v, compute floor(log_10 v). Fast algorithm computing
80 * the binary digits of the result r in time O(log r).
81 *
82 * @param v Value to compute logarithm from
83 * @param res Place to store result
84 * @return EOK on success
85 * @return Logarithm value
86 */
87errno_t ilog10_u64(uint64_t v, unsigned *res)
88{
89 if (v == 0)
90 return ERANGE;
91
92 unsigned b;
93 unsigned e;
94 uint64_t p10p2[6];
95 uint64_t a;
96 unsigned r;
97
98 /* Determine largest b such that 10^2^b <= v */
99 b = 0;
100 e = 1;
101 a = 10;
102 p10p2[0] = a;
103
104 while (v / a >= a) {
105 ++b;
106 a = a * a;
107 e = e + e;
108 p10p2[b] = a;
109 }
110
111 /* Determine the binary digits of largest e such that 10^e <= v */
112 r = 0;
113 while (true) {
114 if (v >= p10p2[b]) {
115 v = v / p10p2[b];
116 r = r ^ (1 << b);
117 }
118
119 if (b == 0)
120 break;
121 --b;
122 }
123
124 *res = r;
125 return EOK;
126}
127
128/** @}
129 */
Note: See TracBrowser for help on using the repository browser.