util,intel: Pull the bit packing helpers from genxml to a common header

Reviewed-by: Eric Engestrom <eric@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18062>
This commit is contained in:
Jason Ekstrand
2022-08-15 08:52:20 -05:00
committed by Marge Bot
parent 7fc1ab4928
commit c52d5acf15
5 changed files with 186 additions and 148 deletions

View File

@@ -77,9 +77,6 @@
#include <memcheck.h>
#include <valgrind.h>
#define VG(x) x
#ifdef DEBUG
#define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
#endif
#else
#define VG(x)
#endif

View File

@@ -77,9 +77,6 @@
#include <valgrind.h>
#include <memcheck.h>
#define VG(x) x
#ifdef DEBUG
#define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
#endif
#else
#define VG(x)
#endif

View File

@@ -44,10 +44,8 @@ pack_header = """%(license)s
#define %(guard)s
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include "util/bitpack_helpers.h"
#ifndef __gen_validate_value
#define __gen_validate_value(x)
@@ -62,67 +60,6 @@ pack_header = """%(license)s
#define NDEBUG_UNUSED
#endif
union __intel_value {
float f;
uint32_t dw;
};
static inline __attribute__((always_inline)) uint64_t
__gen_mbo(uint32_t start, uint32_t end)
{
return (~0ull >> (64 - (end - start + 1))) << start;
}
static inline __attribute__((always_inline)) uint64_t
__gen_uint(uint64_t v, uint32_t start, NDEBUG_UNUSED uint32_t end)
{
__gen_validate_value(v);
#ifndef NDEBUG
const int width = end - start + 1;
if (width < 64) {
const uint64_t max = (1ull << width) - 1;
assert(v <= max);
}
#endif
return v << start;
}
static inline __attribute__((always_inline)) uint64_t
__gen_uint_nonzero(uint64_t v, uint32_t start, uint32_t end)
{
assert(v != 0ull);
return __gen_uint(v, start, end);
}
static inline __attribute__((always_inline)) uint64_t
__gen_sint(int64_t v, uint32_t start, uint32_t end)
{
const int width = end - start + 1;
__gen_validate_value(v);
#ifndef NDEBUG
if (width < 64) {
const int64_t max = (1ll << (width - 1)) - 1;
const int64_t min = -(1ll << (width - 1));
assert(min <= v && v <= max);
}
#endif
const uint64_t mask = ~0ull >> (64 - width);
return (v & mask) << start;
}
static inline __attribute__((always_inline)) uint64_t
__gen_sint_nonzero(int64_t v, uint32_t start, uint32_t end)
{
assert(v != 0ll);
return __gen_sint(v, start, end);
}
static inline __attribute__((always_inline)) uint64_t
__gen_offset(uint64_t v, NDEBUG_UNUSED uint32_t start, NDEBUG_UNUSED uint32_t end)
{
@@ -159,71 +96,6 @@ __gen_address(__gen_user_data *data, void *location,
}
}
static inline __attribute__((always_inline)) uint32_t
__gen_float(float v)
{
__gen_validate_value(v);
return ((union __intel_value) { .f = (v) }).dw;
}
static inline __attribute__((always_inline)) uint32_t
__gen_float_nonzero(float v)
{
assert(v != 0.0f);
return __gen_float(v);
}
static inline __attribute__((always_inline)) uint64_t
__gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
{
__gen_validate_value(v);
const float factor = (1 << fract_bits);
#ifndef NDEBUG
const float max = ((1 << (end - start)) - 1) / factor;
const float min = -(1 << (end - start)) / factor;
assert(min <= v && v <= max);
#endif
const int64_t int_val = llroundf(v * factor);
const uint64_t mask = ~0ull >> (64 - (end - start + 1));
return (int_val & mask) << start;
}
static inline __attribute__((always_inline)) uint64_t
__gen_sfixed_nonzero(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
{
assert(v != 0.0f);
return __gen_sfixed(v, start, end, fract_bits);
}
static inline __attribute__((always_inline)) uint64_t
__gen_ufixed(float v, uint32_t start, NDEBUG_UNUSED uint32_t end, uint32_t fract_bits)
{
__gen_validate_value(v);
const float factor = (1 << fract_bits);
#ifndef NDEBUG
const float max = ((1 << (end - start + 1)) - 1) / factor;
const float min = 0.0f;
assert(min <= v && v <= max);
#endif
const uint64_t uint_val = llroundf(v * factor);
return uint_val << start;
}
static inline __attribute__((always_inline)) uint64_t
__gen_ufixed_nonzero(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
{
assert(v != 0.0f);
return __gen_ufixed(v, start, end, fract_bits);
}
#ifndef __gen_address_type
#error #define __gen_address_type before including this file
#endif
@@ -486,37 +358,37 @@ class Group(object):
nz = "_nonzero" if field.nonzero else ""
if field.type == "mbo":
non_address_fields.append("__gen_mbo(%d, %d)" % \
non_address_fields.append("util_bitpack_ones(%d, %d)" % \
(field.start - dword_start, field.end - dword_start))
elif field.type == "mbz":
assert not field.nonzero
elif field.type == "address":
pass
elif field.type == "uint":
non_address_fields.append("__gen_uint%s(values->%s, %d, %d)" % \
non_address_fields.append("util_bitpack_uint%s(values->%s, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start))
elif field.is_enum_type():
non_address_fields.append("__gen_uint%s(values->%s, %d, %d)" % \
non_address_fields.append("util_bitpack_uint%s(values->%s, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start))
elif field.type == "int":
non_address_fields.append("__gen_sint%s(values->%s, %d, %d)" % \
non_address_fields.append("util_bitpack_sint%s(values->%s, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start))
elif field.type == "bool":
non_address_fields.append("__gen_uint%s(values->%s, %d, %d)" % \
non_address_fields.append("util_bitpack_uint%s(values->%s, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start))
elif field.type == "float":
non_address_fields.append("__gen_float%s(values->%s)" % (nz, name))
non_address_fields.append("util_bitpack_float%s(values->%s)" % (nz, name))
elif field.type == "offset":
non_address_fields.append("__gen_offset%s(values->%s, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start))
elif field.type == 'ufixed':
non_address_fields.append("__gen_ufixed%s(values->%s, %d, %d, %d)" % \
non_address_fields.append("util_bitpack_ufixed%s(values->%s, %d, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start, field.fractional_size))
elif field.type == 'sfixed':
non_address_fields.append("__gen_sfixed%s(values->%s, %d, %d, %d)" % \
non_address_fields.append("util_bitpack_sfixed%s(values->%s, %d, %d, %d)" % \
(nz, name, field.start - dword_start, field.end - dword_start, field.fractional_size))
elif field.is_struct_type():
non_address_fields.append("__gen_uint(v%d_%d, %d, %d)" % \
non_address_fields.append("util_bitpack_uint(v%d_%d, %d, %d)" % \
(index, field_index, field.start - dword_start, field.end - dword_start))
field_index = field_index + 1
else:

View File

@@ -37,9 +37,6 @@
#include <valgrind.h>
#include <memcheck.h>
#define VG(x) x
#ifndef NDEBUG
#define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
#endif
#else
#define VG(x) ((void)0)
#endif

175
src/util/bitpack_helpers.h Normal file
View File

@@ -0,0 +1,175 @@
/*
* Copyright (C) 2016 Intel Corporation
*
* 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 UTIL_BITPACK_HELPERS_H
#define UTIL_BITPACK_HELPERS_H
#include <math.h>
#include <stdbool.h>
#include "util/macros.h"
#include "util/u_math.h"
#ifdef HAVE_VALGRIND
#include <valgrind.h>
#include <memcheck.h>
#ifndef NDEBUG
#define util_bitpack_validate_value(x) \
VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
#endif
#endif
#ifndef util_bitpack_validate_value
#define util_bitpack_validate_value(x)
#endif
ALWAYS_INLINE static uint64_t
util_bitpack_ones(uint32_t start, uint32_t end)
{
return (~0ull >> (64 - (end - start + 1))) << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_uint(uint64_t v, uint32_t start, UNUSED uint32_t end)
{
util_bitpack_validate_value(v);
#ifndef NDEBUG
const int bits = end - start + 1;
if (bits < 64) {
const uint64_t max = u_uintN_max(bits);
assert(v <= max);
}
#endif
return v << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_uint_nonzero(uint64_t v, uint32_t start, uint32_t end)
{
assert(v != 0ull);
return util_bitpack_uint(v, start, end);
}
ALWAYS_INLINE static uint64_t
util_bitpack_sint(int64_t v, uint32_t start, uint32_t end)
{
const int bits = end - start + 1;
util_bitpack_validate_value(v);
#ifndef NDEBUG
if (bits < 64) {
const int64_t min = u_intN_min(bits);
const int64_t max = u_intN_max(bits);
assert(min <= v && v <= max);
}
#endif
const uint64_t mask = BITFIELD64_MASK(bits);
return (v & mask) << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_sint_nonzero(int64_t v, uint32_t start, uint32_t end)
{
assert(v != 0ll);
return util_bitpack_sint(v, start, end);
}
ALWAYS_INLINE static uint32_t
util_bitpack_float(float v)
{
util_bitpack_validate_value(v);
union { float f; uint32_t dw; } x;
x.f = v;
return x.dw;
}
ALWAYS_INLINE static uint32_t
util_bitpack_float_nonzero(float v)
{
assert(v != 0.0f);
return util_bitpack_float(v);
}
ALWAYS_INLINE static uint64_t
util_bitpack_sfixed(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)
{
util_bitpack_validate_value(v);
const float factor = (1 << fract_bits);
#ifndef NDEBUG
const int total_bits = end - start + 1;
const float min = u_intN_min(total_bits) / factor;
const float max = u_intN_max(total_bits) / factor;
assert(min <= v && v <= max);
#endif
const int64_t int_val = llroundf(v * factor);
const uint64_t mask = ~0ull >> (64 - (end - start + 1));
return (int_val & mask) << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_sfixed_nonzero(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)
{
assert(v != 0.0f);
return util_bitpack_sfixed(v, start, end, fract_bits);
}
ALWAYS_INLINE static uint64_t
util_bitpack_ufixed(float v, uint32_t start, ASSERTED uint32_t end,
uint32_t fract_bits)
{
util_bitpack_validate_value(v);
const float factor = (1 << fract_bits);
#ifndef NDEBUG
const int total_bits = end - start + 1;
const float min = 0.0f;
const float max = u_uintN_max(total_bits) / factor;
assert(min <= v && v <= max);
#endif
const uint64_t uint_val = llroundf(v * factor);
return uint_val << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_ufixed_nonzero(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)
{
assert(v != 0.0f);
return util_bitpack_ufixed(v, start, end, fract_bits);
}
#endif /* UTIL_BITPACK_HELPERS_H */