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/macros.h"
34
pvr_dev_addr_is_aligned(pvr_dev_addr_t addr,const uint32_t alignment)35 static inline bool pvr_dev_addr_is_aligned(pvr_dev_addr_t addr,
36 const uint32_t alignment)
37 {
38 assert(util_is_power_of_two_nonzero(alignment));
39 return ((uintptr_t)(addr.addr) & (alignment - 1)) == 0;
40 }
41
ptr_is_aligned(const void * const ptr,const uint32_t alignment)42 static inline bool ptr_is_aligned(const void *const ptr,
43 const uint32_t alignment)
44 {
45 assert(util_is_power_of_two_nonzero(alignment));
46 return ((uintptr_t)(ptr) & (alignment - 1)) == 0;
47 }
48
49 /*****************************************************************************
50 Math functions
51 *****************************************************************************/
52
u32_bin_digits(uint32_t x)53 static inline uint32_t u32_bin_digits(uint32_t x)
54 {
55 return x == 0 ? 1 : util_last_bit(x);
56 }
57
u64_bin_digits(uint64_t x)58 static inline uint32_t u64_bin_digits(uint64_t x)
59 {
60 return x == 0 ? 1 : util_last_bit64(x);
61 }
62
63 extern const uint8_t est_log10_from_log2[64 + 1];
64 extern const uint32_t u32_powers_of_ten[10];
65 extern const uint64_t u64_powers_of_ten[20];
66
67 /*
68 * This function includes parts of a public-domain log10 algorithm from
69 * "Bit Twiddling Hacks", an aggregate collection which is:
70 *
71 * © 1997-2005 Sean Eron Anderson.
72 *
73 * https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
74 */
u32_dec_digits(uint32_t x)75 static inline uint32_t u32_dec_digits(uint32_t x)
76 {
77 /* The value of nr_digits is an estimation of ceil(log10(x)) with variance
78 * +0/-1 (as per the comment on the estimation lut).
79 */
80 const uint32_t nr_digits = est_log10_from_log2[util_last_bit(x)];
81
82 /* We then convert this approximation to a real value by adding 1 iff
83 * x >= 10**nr_digits using another lut to quickly compute the
84 * exponentiation. See the comment on u32_powers_of_ten for details of this.
85 */
86 return nr_digits + (x >= u32_powers_of_ten[nr_digits]);
87 }
88
89 /* This function is an extended form of u32_dec_digits(); see the comments on
90 * that function for details.
91 */
u64_dec_digits(uint64_t x)92 static inline uint32_t u64_dec_digits(uint64_t x)
93 {
94 const uint32_t nr_digits = est_log10_from_log2[util_last_bit64(x)];
95 return nr_digits + (x >= u64_powers_of_ten[nr_digits]);
96 }
97
u32_hex_digits(uint32_t x)98 static inline uint32_t u32_hex_digits(uint32_t x)
99 {
100 const uint32_t binary_digits = u32_bin_digits(x);
101 return (binary_digits >> 2) + !!(binary_digits & 3);
102 }
103
u64_hex_digits(uint64_t x)104 static inline uint32_t u64_hex_digits(uint64_t x)
105 {
106 const uint32_t binary_digits = u64_bin_digits(x);
107 return (binary_digits >> 2) + !!(binary_digits & 3);
108 }
109
110 #endif /* PVR_UTIL_H */
111