1 /*
2 * Copyright (C) 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef UTIL_BITPACK_HELPERS_H
25 #define UTIL_BITPACK_HELPERS_H
26
27 #include <math.h>
28 #include <stdbool.h>
29
30 #include "util/macros.h"
31 #include "util/u_math.h"
32
33 #ifdef HAVE_VALGRIND
34 #include <valgrind.h>
35 #include <memcheck.h>
36 #ifndef NDEBUG
37 #define util_bitpack_validate_value(x) \
38 VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
39 #endif
40 #endif
41
42 #ifndef util_bitpack_validate_value
43 #define util_bitpack_validate_value(x)
44 #endif
45
46 ALWAYS_INLINE static uint64_t
util_bitpack_ones(uint32_t start,uint32_t end)47 util_bitpack_ones(uint32_t start, uint32_t end)
48 {
49 return (~0ull >> (64 - (end - start + 1))) << start;
50 }
51
52 ALWAYS_INLINE static uint64_t
util_bitpack_uint(uint64_t v,uint32_t start,UNUSED uint32_t end)53 util_bitpack_uint(uint64_t v, uint32_t start, UNUSED uint32_t end)
54 {
55 util_bitpack_validate_value(v);
56
57 #ifndef NDEBUG
58 const int bits = end - start + 1;
59 if (bits < 64) {
60 const uint64_t max = u_uintN_max(bits);
61 assert(v <= max);
62 }
63 #endif
64
65 return v << start;
66 }
67
68 ALWAYS_INLINE static uint64_t
util_bitpack_uint_nonzero(uint64_t v,uint32_t start,uint32_t end)69 util_bitpack_uint_nonzero(uint64_t v, uint32_t start, uint32_t end)
70 {
71 assert(v != 0ull);
72 return util_bitpack_uint(v, start, end);
73 }
74
75 ALWAYS_INLINE static uint64_t
util_bitpack_sint(int64_t v,uint32_t start,uint32_t end)76 util_bitpack_sint(int64_t v, uint32_t start, uint32_t end)
77 {
78 const int bits = end - start + 1;
79
80 util_bitpack_validate_value(v);
81
82 #ifndef NDEBUG
83 if (bits < 64) {
84 const int64_t min = u_intN_min(bits);
85 const int64_t max = u_intN_max(bits);
86 assert(min <= v && v <= max);
87 }
88 #endif
89
90 const uint64_t mask = BITFIELD64_MASK(bits);
91
92 return (v & mask) << start;
93 }
94
95 ALWAYS_INLINE static uint64_t
util_bitpack_sint_nonzero(int64_t v,uint32_t start,uint32_t end)96 util_bitpack_sint_nonzero(int64_t v, uint32_t start, uint32_t end)
97 {
98 assert(v != 0ll);
99 return util_bitpack_sint(v, start, end);
100 }
101
102 ALWAYS_INLINE static uint32_t
util_bitpack_float(float v)103 util_bitpack_float(float v)
104 {
105 util_bitpack_validate_value(v);
106 union { float f; uint32_t dw; } x;
107 x.f = v;
108 return x.dw;
109 }
110
111 ALWAYS_INLINE static uint32_t
util_bitpack_float_nonzero(float v)112 util_bitpack_float_nonzero(float v)
113 {
114 assert(v != 0.0f);
115 return util_bitpack_float(v);
116 }
117
118 ALWAYS_INLINE static uint64_t
util_bitpack_sfixed(float v,uint32_t start,uint32_t end,uint32_t fract_bits)119 util_bitpack_sfixed(float v, uint32_t start, uint32_t end,
120 uint32_t fract_bits)
121 {
122 util_bitpack_validate_value(v);
123
124 const float factor = (1 << fract_bits);
125
126 #ifndef NDEBUG
127 const int total_bits = end - start + 1;
128 const float min = u_intN_min(total_bits) / factor;
129 const float max = u_intN_max(total_bits) / factor;
130 assert(min <= v && v <= max);
131 #endif
132
133 const int64_t int_val = llroundf(v * factor);
134 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
135
136 return (int_val & mask) << start;
137 }
138
139 ALWAYS_INLINE static uint64_t
util_bitpack_sfixed_clamp(float v,uint32_t start,uint32_t end,uint32_t fract_bits)140 util_bitpack_sfixed_clamp(float v, uint32_t start, uint32_t end,
141 uint32_t fract_bits)
142 {
143 util_bitpack_validate_value(v);
144
145 const float factor = (1 << fract_bits);
146
147 const int total_bits = end - start + 1;
148 const float min = u_intN_min(total_bits) / factor;
149 const float max = u_intN_max(total_bits) / factor;
150
151 const int64_t int_val = llroundf(CLAMP(v, min, max) * factor);
152 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
153
154 return (int_val & mask) << start;
155 }
156
157 ALWAYS_INLINE static uint64_t
util_bitpack_sfixed_nonzero(float v,uint32_t start,uint32_t end,uint32_t fract_bits)158 util_bitpack_sfixed_nonzero(float v, uint32_t start, uint32_t end,
159 uint32_t fract_bits)
160 {
161 assert(v != 0.0f);
162 return util_bitpack_sfixed(v, start, end, fract_bits);
163 }
164
165 ALWAYS_INLINE static uint64_t
util_bitpack_ufixed(float v,uint32_t start,ASSERTED uint32_t end,uint32_t fract_bits)166 util_bitpack_ufixed(float v, uint32_t start, ASSERTED uint32_t end,
167 uint32_t fract_bits)
168 {
169 util_bitpack_validate_value(v);
170
171 const float factor = (1 << fract_bits);
172
173 #ifndef NDEBUG
174 const int total_bits = end - start + 1;
175 const float min = 0.0f;
176 const float max = u_uintN_max(total_bits) / factor;
177 assert(min <= v && v <= max);
178 #endif
179
180 const uint64_t uint_val = llroundf(v * factor);
181
182 return uint_val << start;
183 }
184
185 ALWAYS_INLINE static uint64_t
util_bitpack_ufixed_clamp(float v,uint32_t start,ASSERTED uint32_t end,uint32_t fract_bits)186 util_bitpack_ufixed_clamp(float v, uint32_t start, ASSERTED uint32_t end,
187 uint32_t fract_bits)
188 {
189 util_bitpack_validate_value(v);
190
191 const float factor = (1 << fract_bits);
192
193 const int total_bits = end - start + 1;
194 const float min = 0.0f;
195 const float max = u_uintN_max(total_bits) / factor;
196
197 const uint64_t uint_val = llroundf(CLAMP(v, min, max) * factor);
198
199 return uint_val << start;
200 }
201
202 ALWAYS_INLINE static uint64_t
util_bitpack_ufixed_nonzero(float v,uint32_t start,uint32_t end,uint32_t fract_bits)203 util_bitpack_ufixed_nonzero(float v, uint32_t start, uint32_t end,
204 uint32_t fract_bits)
205 {
206 assert(v != 0.0f);
207 return util_bitpack_ufixed(v, start, end, fract_bits);
208 }
209
210 #endif /* UTIL_BITPACK_HELPERS_H */
211