[9854a8f] | 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 | */
|
---|
| 50 | int ipow10_u64(unsigned exp, uint64_t *res)
|
---|
| 51 | {
|
---|
| 52 | unsigned 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 | * @return Logarithm value
|
---|
| 84 | */
|
---|
| 85 | unsigned ilog10_u64(uint64_t v)
|
---|
| 86 | {
|
---|
| 87 | unsigned b;
|
---|
| 88 | unsigned e;
|
---|
| 89 | uint64_t p10p2[6];
|
---|
| 90 | uint64_t a;
|
---|
| 91 | unsigned r;
|
---|
| 92 |
|
---|
| 93 | /* Determine largest b such that 10^2^b <= v */
|
---|
| 94 | b = 0;
|
---|
| 95 | e = 1;
|
---|
| 96 | a = 10;
|
---|
| 97 | p10p2[0] = a;
|
---|
| 98 |
|
---|
| 99 | while (v / a >= a) {
|
---|
| 100 | ++b;
|
---|
| 101 | a = a * a;
|
---|
| 102 | e = e + e;
|
---|
| 103 | p10p2[b] = a;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /* Determine the binary digits of largest e such that 10^e <= v */
|
---|
| 107 | r = 0;
|
---|
| 108 | while (true) {
|
---|
| 109 | if (v >= p10p2[b]) {
|
---|
| 110 | v = v / p10p2[b];
|
---|
| 111 | r = r ^ (1 << b);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (b == 0)
|
---|
| 115 | break;
|
---|
| 116 | --b;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return r;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** @}
|
---|
| 123 | */
|
---|