mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-05 11:48:06 +02:00
pvr: util: Add integer digit counting functions
These are (reasonably) fast helpers for computing the number of binary, decimal or hexadecimal digits required to represent a given non-negative integer. Signed-off-by: Matt Coster <matt.coster@imgtec.com> Reviewed-by: Karmjit Mahil <Karmjit.Mahil@imgtec.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18948>
This commit is contained in:
parent
ccac91db7b
commit
f36c938c94
3 changed files with 167 additions and 0 deletions
|
|
@ -24,6 +24,7 @@ libpowervr_common = static_library(
|
|||
[
|
||||
'pvr_debug.c',
|
||||
'pvr_device_info.c',
|
||||
'pvr_util.c',
|
||||
],
|
||||
include_directories : [
|
||||
inc_include,
|
||||
|
|
|
|||
74
src/imagination/common/pvr_util.c
Normal file
74
src/imagination/common/pvr_util.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright © 2022 Imagination Technologies Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "pvr_util.h"
|
||||
|
||||
/* Generated by floor(n/log2(10)). This is a compact representation of a
|
||||
* log10(2**n) estimation. Estimations of log2(n) +1/-0 are very fast on modern
|
||||
* CPUs with clz (or equivalent) instructions, which makes this log2-indexed
|
||||
* lookup both compact and fast. Assuming an input of log2(x) +1/-0; this lut
|
||||
* should produce an output of ceil(log10(x)) +0/-1.
|
||||
*/
|
||||
const uint8_t est_log10_from_log2[64 + 1] = {
|
||||
0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
|
||||
5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9,
|
||||
10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15,
|
||||
15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19,
|
||||
};
|
||||
|
||||
/* Generated by 10**n with one exception: we make the first power of ten 0
|
||||
* (instead of 1) to ensure a 0-input produces a single digit (instead of no
|
||||
* digits).
|
||||
*/
|
||||
const uint32_t u32_powers_of_ten[10] = {
|
||||
UINT32_C(0), UINT32_C(10), UINT32_C(100),
|
||||
UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
|
||||
UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
|
||||
UINT32_C(1000000000),
|
||||
};
|
||||
|
||||
/* This is an extension of u32_powers_of_ten to include all possible 64-bit
|
||||
* values.
|
||||
*/
|
||||
const uint64_t u64_powers_of_ten[20] = {
|
||||
UINT64_C(0),
|
||||
UINT64_C(10),
|
||||
UINT64_C(100),
|
||||
UINT64_C(1000),
|
||||
UINT64_C(10000),
|
||||
UINT64_C(100000),
|
||||
UINT64_C(1000000),
|
||||
UINT64_C(10000000),
|
||||
UINT64_C(100000000),
|
||||
UINT64_C(1000000000),
|
||||
UINT64_C(10000000000),
|
||||
UINT64_C(100000000000),
|
||||
UINT64_C(1000000000000),
|
||||
UINT64_C(10000000000000),
|
||||
UINT64_C(100000000000000),
|
||||
UINT64_C(1000000000000000),
|
||||
UINT64_C(10000000000000000),
|
||||
UINT64_C(100000000000000000),
|
||||
UINT64_C(1000000000000000000),
|
||||
UINT64_C(10000000000000000000),
|
||||
};
|
||||
92
src/imagination/common/pvr_util.h
Normal file
92
src/imagination/common/pvr_util.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* Copyright © 2022 Imagination Technologies Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef PVR_UTIL_H
|
||||
#define PVR_UTIL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util/bitscan.h"
|
||||
|
||||
/*****************************************************************************
|
||||
Math functions
|
||||
*****************************************************************************/
|
||||
|
||||
static inline uint32_t u32_bin_digits(uint32_t x)
|
||||
{
|
||||
return x == 0 ? 1 : util_last_bit(x);
|
||||
}
|
||||
|
||||
static inline uint32_t u64_bin_digits(uint64_t x)
|
||||
{
|
||||
return x == 0 ? 1 : util_last_bit64(x);
|
||||
}
|
||||
|
||||
extern const uint8_t est_log10_from_log2[64 + 1];
|
||||
extern const uint32_t u32_powers_of_ten[10];
|
||||
extern const uint64_t u64_powers_of_ten[20];
|
||||
|
||||
/*
|
||||
* This function includes parts of a public-domain log10 algorithm from
|
||||
* "Bit Twiddling Hacks", an aggregate collection which is:
|
||||
*
|
||||
* © 1997-2005 Sean Eron Anderson.
|
||||
*
|
||||
* https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
|
||||
*/
|
||||
static inline uint32_t u32_dec_digits(uint32_t x)
|
||||
{
|
||||
/* The value of nr_digits is an estimation of ceil(log10(x)) with variance
|
||||
* +0/-1 (as per the comment on the estimation lut).
|
||||
*/
|
||||
const uint32_t nr_digits = est_log10_from_log2[util_last_bit(x)];
|
||||
|
||||
/* We then convert this approximation to a real value by adding 1 iff
|
||||
* x >= 10**nr_digits using another lut to quickly compute the
|
||||
* exponentiation. See the comment on u32_powers_of_ten for details of this.
|
||||
*/
|
||||
return nr_digits + (x >= u32_powers_of_ten[nr_digits]);
|
||||
}
|
||||
|
||||
/* This function is an extended form of u32_dec_digits(); see the comments on
|
||||
* that function for details.
|
||||
*/
|
||||
static inline uint32_t u64_dec_digits(uint64_t x)
|
||||
{
|
||||
const uint32_t nr_digits = est_log10_from_log2[util_last_bit64(x)];
|
||||
return nr_digits + (x >= u64_powers_of_ten[nr_digits]);
|
||||
}
|
||||
|
||||
static inline uint32_t u32_hex_digits(uint32_t x)
|
||||
{
|
||||
const uint32_t binary_digits = u32_bin_digits(x);
|
||||
return (binary_digits >> 2) + !!(binary_digits & 3);
|
||||
}
|
||||
|
||||
static inline uint32_t u64_hex_digits(uint64_t x)
|
||||
{
|
||||
const uint32_t binary_digits = u64_bin_digits(x);
|
||||
return (binary_digits >> 2) + !!(binary_digits & 3);
|
||||
}
|
||||
|
||||
#endif /* PVR_UTIL_H */
|
||||
Loading…
Add table
Reference in a new issue