| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* based in part on anv driver which is: |
| 5 |
* Copyright © 2016 Intel Corporation |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice (including the next |
| 15 |
* paragraph) shall be included in all copies or substantial portions of the |
| 16 |
* Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
* SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef PVR_PACKET_HELPERS_H |
| 28 |
#define PVR_PACKET_HELPERS_H |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
#include <math.h> |
| 32 |
#include <stdbool.h> |
| 33 |
#include <stddef.h> |
| 34 |
#include <stdint.h> |
| 35 |
#include <stdio.h> |
| 36 |
|
| 37 |
#ifndef __pvr_validate_value |
| 38 |
# define __pvr_validate_value(x) |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef NDEBUG |
| 42 |
# define NDEBUG_UNUSED __attribute__((unused)) |
| 43 |
#else |
| 44 |
# define NDEBUG_UNUSED |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifndef __pvr_address_type |
| 48 |
# error #define __pvr_address_type before including this file |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifndef __pvr_get_address |
| 52 |
# error #define __pvr_get_address before including this file |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef __pvr_make_address |
| 56 |
# error #define __pvr_make_address before including this file |
| 57 |
#endif |
| 58 |
|
| 59 |
union __pvr_value { |
| 60 |
float f; |
| 61 |
uint32_t dw; |
| 62 |
}; |
| 63 |
|
| 64 |
static inline __attribute__((always_inline)) uint64_t __pvr_mbo(uint32_t start, |
| 65 |
uint32_t end) |
| 66 |
{ |
| 67 |
return (~UINT64_C(0) >> (64 - (end - start + 1))) << start; |
| 68 |
} |
| 69 |
|
| 70 |
static inline __attribute__((always_inline)) uint64_t |
| 71 |
__pvr_uint(uint64_t v, uint32_t start, NDEBUG_UNUSED uint32_t end) |
| 72 |
{ |
| 73 |
__pvr_validate_value(v); |
| 74 |
|
| 75 |
#ifndef NDEBUG |
| 76 |
const int width = end - start + 1; |
| 77 |
if (width < 64) { |
| 78 |
const uint64_t max = (UINT64_C(1) << width) - 1; |
| 79 |
assert(v <= max); |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
return v << start; |
| 84 |
} |
| 85 |
|
| 86 |
static inline __attribute__((always_inline)) uint64_t |
| 87 |
__pvr_uint_unpack(uint64_t packed, uint32_t start, uint32_t end) |
| 88 |
{ |
| 89 |
const int width = end - start + 1; |
| 90 |
const uint64_t mask = ~UINT64_C(0) >> (64 - width); |
| 91 |
|
| 92 |
return (packed >> start) & mask; |
| 93 |
} |
| 94 |
|
| 95 |
static inline __attribute__((always_inline)) uint64_t |
| 96 |
__pvr_sint(int64_t v, uint32_t start, uint32_t end) |
| 97 |
{ |
| 98 |
const int width = end - start + 1; |
| 99 |
|
| 100 |
__pvr_validate_value(v); |
| 101 |
|
| 102 |
#ifndef NDEBUG |
| 103 |
if (width < 64) { |
| 104 |
const int64_t max = (1ll << (width - 1)) - 1; |
| 105 |
const int64_t min = -(1ll << (width - 1)); |
| 106 |
assert(min <= v && v <= max); |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
const uint64_t mask = ~UINT64_C(0) >> (64 - width); |
| 111 |
|
| 112 |
return (v & mask) << start; |
| 113 |
} |
| 114 |
|
| 115 |
static inline __attribute__((always_inline)) int64_t |
| 116 |
__pvr_sint_unpack(uint64_t packed, uint32_t start, uint32_t end) |
| 117 |
{ |
| 118 |
const int width = end - start + 1; |
| 119 |
const uint64_t mask = ~UINT64_C(0) >> (64 - width); |
| 120 |
|
| 121 |
return (int64_t)((packed >> start) & mask); |
| 122 |
} |
| 123 |
|
| 124 |
static inline __attribute__((always_inline)) uint64_t |
| 125 |
__pvr_offset(uint64_t v, |
| 126 |
NDEBUG_UNUSED uint32_t start, |
| 127 |
NDEBUG_UNUSED uint32_t end) |
| 128 |
{ |
| 129 |
__pvr_validate_value(v); |
| 130 |
#ifndef NDEBUG |
| 131 |
uint64_t mask = (~UINT64_C(0) >> (64 - (end - start + 1))) << start; |
| 132 |
|
| 133 |
assert((v & ~mask) == 0); |
| 134 |
#endif |
| 135 |
|
| 136 |
return v; |
| 137 |
} |
| 138 |
|
| 139 |
static inline __attribute__((always_inline)) uint64_t |
| 140 |
__pvr_offset_unpack(uint64_t packed, |
| 141 |
NDEBUG_UNUSED uint32_t start, |
| 142 |
NDEBUG_UNUSED uint32_t end) |
| 143 |
{ |
| 144 |
#ifndef NDEBUG |
| 145 |
uint64_t mask = (~UINT64_C(0) >> (64 - (end - start + 1))) << start; |
| 146 |
|
| 147 |
assert((packed & ~mask) == 0); |
| 148 |
#endif |
| 149 |
|
| 150 |
return packed; |
| 151 |
} |
| 152 |
|
| 153 |
static inline __attribute__((always_inline)) uint64_t |
| 154 |
__pvr_address(__pvr_address_type address, |
| 155 |
uint32_t shift, |
| 156 |
uint32_t start, |
| 157 |
uint32_t end) |
| 158 |
{ |
| 159 |
uint64_t addr_u64 = __pvr_get_address(address); |
| 160 |
uint64_t mask = (~UINT64_C(0) >> (64 - (end - start + 1))) << start; |
| 161 |
|
| 162 |
return ((addr_u64 >> shift) << start) & mask; |
| 163 |
} |
| 164 |
|
| 165 |
static inline __attribute__((always_inline)) __pvr_address_type |
| 166 |
__pvr_address_unpack(uint64_t packed, |
| 167 |
uint32_t shift, |
| 168 |
uint32_t start, |
| 169 |
uint32_t end) |
| 170 |
{ |
| 171 |
uint64_t mask = (~UINT64_C(0) >> (64 - (end - start + 1))) << start; |
| 172 |
uint64_t addr_u64 = ((packed & mask) >> start) << shift; |
| 173 |
|
| 174 |
return __pvr_make_address(addr_u64); |
| 175 |
} |
| 176 |
|
| 177 |
static inline __attribute__((always_inline)) uint32_t __pvr_float(float v) |
| 178 |
{ |
| 179 |
__pvr_validate_value(v); |
| 180 |
return ((union __pvr_value){ .f = (v) }).dw; |
| 181 |
} |
| 182 |
|
| 183 |
static inline __attribute__((always_inline)) float |
| 184 |
__pvr_float_unpack(uint32_t packed) |
| 185 |
{ |
| 186 |
return ((union __pvr_value){ .dw = (packed) }).f; |
| 187 |
} |
| 188 |
|
| 189 |
static inline __attribute__((always_inline)) uint64_t |
| 190 |
__pvr_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits) |
| 191 |
{ |
| 192 |
__pvr_validate_value(v); |
| 193 |
|
| 194 |
const float factor = (1 << fract_bits); |
| 195 |
|
| 196 |
#ifndef NDEBUG |
| 197 |
const float max = ((1 << (end - start)) - 1) / factor; |
| 198 |
const float min = -(1 << (end - start)) / factor; |
| 199 |
assert(min <= v && v <= max); |
| 200 |
#endif |
| 201 |
|
| 202 |
const int64_t int_val = llroundf(v * factor); |
| 203 |
const uint64_t mask = ~UINT64_C(0) >> (64 - (end - start + 1)); |
| 204 |
|
| 205 |
return (int_val & mask) << start; |
| 206 |
} |
| 207 |
|
| 208 |
static inline __attribute__((always_inline)) uint64_t |
| 209 |
__pvr_ufixed(float v, |
| 210 |
uint32_t start, |
| 211 |
NDEBUG_UNUSED uint32_t end, |
| 212 |
uint32_t fract_bits) |
| 213 |
{ |
| 214 |
__pvr_validate_value(v); |
| 215 |
|
| 216 |
const float factor = (1 << fract_bits); |
| 217 |
|
| 218 |
#ifndef NDEBUG |
| 219 |
const float max = ((1 << (end - start + 1)) - 1) / factor; |
| 220 |
const float min = 0.0f; |
| 221 |
assert(min <= v && v <= max); |
| 222 |
#endif |
| 223 |
|
| 224 |
const uint64_t uint_val = llroundf(v * factor); |
| 225 |
|
| 226 |
return uint_val << start; |
| 227 |
} |
| 228 |
|
| 229 |
#undef NDEBUG_UNUSED |
| 230 |
|
| 231 |
#endif /* PVR_PACKET_HELPERS_H */ |