| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to deal |
| 6 |
* in the Software without restriction, including without limitation the rights |
| 7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
* copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef PVR_UTIL_H |
| 25 |
#define PVR_UTIL_H |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <stdint.h> |
| 29 |
|
| 30 |
#include "pvr_types.h" |
| 31 |
|
| 32 |
#include "util/bitscan.h" |
| 33 |
#include "util/u_math.h" |
| 34 |
|
| 35 |
static inline bool pvr_dev_addr_is_aligned(pvr_dev_addr_t addr, |
| 36 |
const uint32_t alignment) |
| 37 |
{ |
| 38 |
return util_is_aligned(addr.addr, alignment); |
| 39 |
} |
| 40 |
|
| 41 |
/***************************************************************************** |
| 42 |
Math functions |
| 43 |
*****************************************************************************/ |
| 44 |
|
| 45 |
static inline uint32_t u32_bin_digits(uint32_t x) |
| 46 |
{ |
| 47 |
return x == 0 ? 1 : util_last_bit(x); |
| 48 |
} |
| 49 |
|
| 50 |
static inline uint32_t u64_bin_digits(uint64_t x) |
| 51 |
{ |
| 52 |
return x == 0 ? 1 : util_last_bit64(x); |
| 53 |
} |
| 54 |
|
| 55 |
extern const uint8_t est_log10_from_log2[64 + 1]; |
| 56 |
extern const uint32_t u32_powers_of_ten[10]; |
| 57 |
extern const uint64_t u64_powers_of_ten[20]; |
| 58 |
|
| 59 |
/* |
| 60 |
* This function includes parts of a public-domain log10 algorithm from |
| 61 |
* "Bit Twiddling Hacks", an aggregate collection which is: |
| 62 |
* |
| 63 |
* © 1997-2005 Sean Eron Anderson. |
| 64 |
* |
| 65 |
* https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 66 |
*/ |
| 67 |
static inline uint32_t u32_dec_digits(uint32_t x) |
| 68 |
{ |
| 69 |
/* The value of nr_digits is an estimation of ceil(log10(x)) with variance |
| 70 |
* +0/-1 (as per the comment on the estimation lut). |
| 71 |
*/ |
| 72 |
const uint32_t nr_digits = est_log10_from_log2[util_last_bit(x)]; |
| 73 |
|
| 74 |
/* We then convert this approximation to a real value by adding 1 iff |
| 75 |
* x >= 10**nr_digits using another lut to quickly compute the |
| 76 |
* exponentiation. See the comment on u32_powers_of_ten for details of this. |
| 77 |
*/ |
| 78 |
return nr_digits + (x >= u32_powers_of_ten[nr_digits]); |
| 79 |
} |
| 80 |
|
| 81 |
/* This function is an extended form of u32_dec_digits(); see the comments on |
| 82 |
* that function for details. |
| 83 |
*/ |
| 84 |
static inline uint32_t u64_dec_digits(uint64_t x) |
| 85 |
{ |
| 86 |
const uint32_t nr_digits = est_log10_from_log2[util_last_bit64(x)]; |
| 87 |
return nr_digits + (x >= u64_powers_of_ten[nr_digits]); |
| 88 |
} |
| 89 |
|
| 90 |
static inline uint32_t u32_hex_digits(uint32_t x) |
| 91 |
{ |
| 92 |
const uint32_t binary_digits = u32_bin_digits(x); |
| 93 |
return (binary_digits >> 2) + !!(binary_digits & 3); |
| 94 |
} |
| 95 |
|
| 96 |
static inline uint32_t u64_hex_digits(uint64_t x) |
| 97 |
{ |
| 98 |
const uint32_t binary_digits = u64_bin_digits(x); |
| 99 |
return (binary_digits >> 2) + !!(binary_digits & 3); |
| 100 |
} |
| 101 |
|
| 102 |
#endif /* PVR_UTIL_H */ |