| 1 |
/* |
| 2 |
* Copyright 2024 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef PAN_PACK_HELPERS_H |
| 7 |
#define PAN_PACK_HELPERS_H |
| 8 |
|
| 9 |
#include "compiler/libcl/libcl.h" |
| 10 |
#include "util/bitpack_helpers.h" |
| 11 |
|
| 12 |
#ifndef __OPENCL_VERSION__ |
| 13 |
#include <inttypes.h> |
| 14 |
#include <stdio.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#ifdef __OPENCL_VERSION__ |
| 18 |
#define fprintf(...) \ |
| 19 |
do { \ |
| 20 |
} while (0) |
| 21 |
#endif |
| 22 |
|
| 23 |
static inline uint32_t |
| 24 |
__gen_padded(uint32_t v, uint32_t start, uint32_t end) |
| 25 |
{ |
| 26 |
unsigned shift = __builtin_ctz(v); |
| 27 |
unsigned odd = v >> (shift + 1); |
| 28 |
|
| 29 |
#ifndef NDEBUG |
| 30 |
assert((v >> shift) & 1); |
| 31 |
assert(shift <= 31); |
| 32 |
assert(odd <= 7); |
| 33 |
assert((end - start + 1) == 8); |
| 34 |
#endif |
| 35 |
|
| 36 |
return util_bitpack_uint(shift | (odd << 5), start, end); |
| 37 |
} |
| 38 |
|
| 39 |
#define __gen_unpack_uint(out, cl, start, end) \ |
| 40 |
do { \ |
| 41 |
uint64_t __val = 0; \ |
| 42 |
const int __width = (end) - (start) + 1; \ |
| 43 |
const uint64_t __mask = \ |
| 44 |
(__width == 64) ? ~((uint64_t)0) : ((uint64_t)1 << __width) - 1; \ |
| 45 |
\ |
| 46 |
for (unsigned __word = (start) / 32; __word < ((end) / 32) + 1; \ |
| 47 |
__word++) { \ |
| 48 |
__val |= ((uint64_t)(cl)[__word]) << ((__word - (start) / 32) * 32); \ |
| 49 |
} \ |
| 50 |
\ |
| 51 |
(out) = (__val >> ((start) % 32)) & __mask; \ |
| 52 |
} while (0) |
| 53 |
|
| 54 |
#define __gen_unpack_sint(out, cl, start, end) \ |
| 55 |
do { \ |
| 56 |
int size = (end) - (start) + 1; \ |
| 57 |
int64_t __tmp_sint; \ |
| 58 |
__gen_unpack_uint(__tmp_sint, cl, start, end); \ |
| 59 |
(out) = util_sign_extend(__tmp_sint, size); \ |
| 60 |
} while (0) |
| 61 |
|
| 62 |
#define __gen_unpack_ulod(out, cl, start, end) \ |
| 63 |
do { \ |
| 64 |
uint32_t __tmp_ulod; \ |
| 65 |
__gen_unpack_uint(__tmp_ulod, cl, start, end); \ |
| 66 |
(out) = ((float)__tmp_ulod) / 256.0; \ |
| 67 |
} while (0) |
| 68 |
|
| 69 |
#define __gen_unpack_slod(out, cl, start, end) \ |
| 70 |
do { \ |
| 71 |
int32_t __tmp_slod; \ |
| 72 |
__gen_unpack_sint(__tmp_slod, cl, start, end); \ |
| 73 |
(out) = ((float)__tmp_slod) / 256.0; \ |
| 74 |
} while (0) |
| 75 |
|
| 76 |
#define __gen_unpack_float(out, cl, start, end) \ |
| 77 |
do { \ |
| 78 |
uint32_t __tmp_float; \ |
| 79 |
__gen_unpack_uint(__tmp_float, cl, start, end); \ |
| 80 |
(out) = uif(__tmp_float); \ |
| 81 |
} while (0) |
| 82 |
|
| 83 |
#define __gen_unpack_padded(out, cl, start, end) \ |
| 84 |
do { \ |
| 85 |
uint32_t __tmp_padded; \ |
| 86 |
__gen_unpack_uint(__tmp_padded, cl, start, end); \ |
| 87 |
(out) = (2 * (__tmp_padded >> 5) + 1) << (__tmp_padded & 0b11111); \ |
| 88 |
} while (0) |
| 89 |
|
| 90 |
#define PREFIX1(A) MALI_##A |
| 91 |
#define PREFIX2(A, B) MALI_##A##_##B |
| 92 |
#define PREFIX4(A, B, C, D) MALI_##A##_##B##_##C##_##D |
| 93 |
|
| 94 |
#define pan_pack(dst, T, name) \ |
| 95 |
for (UNUSED struct PREFIX1(T) name = {PREFIX2(T, header)}, \ |
| 96 |
*_loop_terminate = &name; \ |
| 97 |
__builtin_expect(_loop_terminate != NULL, 1); ({ \ |
| 98 |
PREFIX2(T, pack)((dst), &name); \ |
| 99 |
_loop_terminate = NULL; \ |
| 100 |
})) |
| 101 |
|
| 102 |
#define pan_pack_nodefaults(dst, T, name) \ |
| 103 |
for (UNUSED struct PREFIX1(T) name = {0}, *_loop_terminate = &name; \ |
| 104 |
__builtin_expect(_loop_terminate != NULL, 1); ({ \ |
| 105 |
PREFIX2(T, pack)((dst), &name); \ |
| 106 |
_loop_terminate = NULL; \ |
| 107 |
})) |
| 108 |
|
| 109 |
#define pan_unpack(src, T, name) \ |
| 110 |
UNUSED struct PREFIX1(T) name; \ |
| 111 |
PREFIX2(T, unpack)((src), &name) |
| 112 |
|
| 113 |
#define pan_print(fp, T, var, indent) PREFIX2(T, print)(fp, &(var), indent) |
| 114 |
|
| 115 |
#define pan_size(T) PREFIX2(T, LENGTH) |
| 116 |
#define pan_alignment(T) PREFIX2(T, ALIGN) |
| 117 |
|
| 118 |
#define pan_section_offset(A, S) PREFIX4(A, SECTION, S, OFFSET) |
| 119 |
|
| 120 |
/* Those APIs aren't safe in OpenCL C because we lose information on the |
| 121 |
* pointer address space */ |
| 122 |
#ifndef __OPENCL_VERSION__ |
| 123 |
#define pan_cast_and_pack(dst, T, name) \ |
| 124 |
pan_pack((PREFIX2(T, PACKED_T) *)(dst), T, name) |
| 125 |
|
| 126 |
#define pan_cast_and_pack_nodefaults(dst, T, name) \ |
| 127 |
pan_pack_nodefaults((PREFIX2(T, PACKED_T) *)(dst), T, name) |
| 128 |
|
| 129 |
#define pan_cast_and_unpack(src, T, name) \ |
| 130 |
pan_unpack((const PREFIX2(T, PACKED_T) *)(src), T, name) |
| 131 |
|
| 132 |
#define pan_section_ptr(base, A, S) \ |
| 133 |
(&((PREFIX2(A, PACKED_T) *)(base))->S) |
| 134 |
|
| 135 |
#define pan_section_pack(dst, A, S, name) \ |
| 136 |
for (UNUSED PREFIX4(A, SECTION, S, TYPE) \ |
| 137 |
name = {PREFIX4(A, SECTION, S, header)}, \ |
| 138 |
*_loop_terminate = (void *)(dst); \ |
| 139 |
__builtin_expect(_loop_terminate != NULL, 1); ({ \ |
| 140 |
PREFIX4(A, SECTION, S, pack)(pan_section_ptr(dst, A, S), &name); \ |
| 141 |
_loop_terminate = NULL; \ |
| 142 |
})) |
| 143 |
|
| 144 |
#define pan_section_unpack(src, A, S, name) \ |
| 145 |
UNUSED PREFIX4(A, SECTION, S, TYPE) name; \ |
| 146 |
PREFIX4(A, SECTION, S, unpack)(pan_section_ptr(src, A, S), &name) |
| 147 |
#endif |
| 148 |
|
| 149 |
#define pan_section_print(fp, A, S, var, indent) \ |
| 150 |
PREFIX4(A, SECTION, S, print)(fp, &(var), indent) |
| 151 |
|
| 152 |
#define pan_merge(packed1, packed2, type) \ |
| 153 |
do { \ |
| 154 |
size_t __tmp_bytes = pan_size(type); \ |
| 155 |
for (unsigned i = 0; i < (__tmp_bytes / 4); ++i) \ |
| 156 |
(packed1)->opaque[i] |= (packed2)->opaque[i]; \ |
| 157 |
} while (0) |
| 158 |
|
| 159 |
#ifndef __OPENCL_VERSION__ |
| 160 |
static inline const char * |
| 161 |
mali_component_swizzle(unsigned val) |
| 162 |
{ |
| 163 |
static const char swiz_name[] = "RGBA01??"; |
| 164 |
static char out_str[5], *outp; |
| 165 |
outp = out_str; |
| 166 |
for (int i = 0; i < 12; i += 3) { |
| 167 |
*outp++ = swiz_name[(val >> i) & 7]; |
| 168 |
} |
| 169 |
*outp = 0; |
| 170 |
return out_str; |
| 171 |
} |
| 172 |
#endif |
| 173 |
|
| 174 |
/* From presentations, 16x16 tiles externally. Use shift for fast computation |
| 175 |
* of tile numbers. */ |
| 176 |
|
| 177 |
#define MALI_TILE_SHIFT 4 |
| 178 |
#define MALI_TILE_LENGTH (1 << MALI_TILE_SHIFT) |
| 179 |
|
| 180 |
#endif |