Path: blob/21.2-virgl/src/broadcom/cle/v3d_packet_helpers.h
4560 views
/*1* Copyright (C) 2016 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef MESA_V3D_PACKET_HELPERS_H24#define MESA_V3D_PACKET_HELPERS_H2526#include <stdio.h>27#include <stdint.h>28#include <stdbool.h>29#include <assert.h>30#include <math.h>31#include "util/u_math.h"3233#ifdef HAVE_VALGRIND34#include <valgrind.h>35#include <memcheck.h>36#define VG(x) x37#ifndef NDEBUG38#define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))39#endif40#else41#define VG(x) ((void)0)42#endif4344#ifndef __gen_validate_value45#define __gen_validate_value(x)46#endif47/*48#ifndef __gen_address_type49#error #define __gen_address_type before including this file50#endif5152#ifndef __gen_user_data53#error #define __gen_combine_address before including this file54#endif55*/56union __gen_value {57float f;58uint32_t dw;59};6061static inline uint64_t62__gen_mbo(uint32_t start, uint32_t end)63{64return (~0ull >> (64 - (end - start + 1))) << start;65}6667static inline uint64_t68__gen_uint(uint64_t v, uint32_t start, uint32_t end)69{70__gen_validate_value(v);7172#ifndef NDEBUG73const int width = end - start + 1;74if (width < 64) {75const uint64_t max = (1ull << width) - 1;76assert(v <= max);77}78#endif7980return v << start;81}8283static inline uint64_t84__gen_sint(int64_t v, uint32_t start, uint32_t end)85{86const int width = end - start + 1;8788__gen_validate_value(v);8990#ifndef NDEBUG91if (width < 64) {92const int64_t max = (1ll << (width - 1)) - 1;93const int64_t min = -(1ll << (width - 1));94assert(min <= v && v <= max);95}96#endif9798const uint64_t mask = ~0ull >> (64 - width);99100return (v & mask) << start;101}102103static inline uint64_t104__gen_offset(uint64_t v, uint32_t start, uint32_t end)105{106__gen_validate_value(v);107#ifndef NDEBUG108uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;109110assert((v & ~mask) == 0);111#endif112113return v;114}115116static inline uint32_t117__gen_float(float v)118{119__gen_validate_value(v);120return ((union __gen_value) { .f = (v) }).dw;121}122123static inline uint64_t124__gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)125{126__gen_validate_value(v);127128const float factor = (1 << fract_bits);129130#ifndef NDEBUG131const float max = ((1 << (end - start)) - 1) / factor;132const float min = -(1 << (end - start)) / factor;133assert(min <= v && v <= max);134#endif135136const int64_t int_val = llroundf(v * factor);137const uint64_t mask = ~0ull >> (64 - (end - start + 1));138139return (int_val & mask) << start;140}141142static inline uint64_t143__gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)144{145__gen_validate_value(v);146147const float factor = (1 << fract_bits);148149#ifndef NDEBUG150const float max = ((1 << (end - start + 1)) - 1) / factor;151const float min = 0.0f;152assert(min <= v && v <= max);153#endif154155const uint64_t uint_val = llroundf(v * factor);156157return uint_val << start;158}159160static inline uint64_t161__gen_unpack_uint(const uint8_t *restrict cl, uint32_t start, uint32_t end)162{163uint64_t val = 0;164const int width = end - start + 1;165const uint32_t mask = (width == 32 ? ~0 : (1 << width) - 1 );166167for (uint32_t byte = start / 8; byte <= end / 8; byte++) {168val |= cl[byte] << ((byte - start / 8) * 8);169}170171return (val >> (start % 8)) & mask;172}173174static inline uint64_t175__gen_unpack_sint(const uint8_t *restrict cl, uint32_t start, uint32_t end)176{177int size = end - start + 1;178int64_t val = __gen_unpack_uint(cl, start, end);179180/* Get the sign bit extended. */181return (val << (64 - size)) >> (64 - size);182}183184static inline float185__gen_unpack_sfixed(const uint8_t *restrict cl, uint32_t start, uint32_t end,186uint32_t fractional_size)187{188int32_t bits = __gen_unpack_sint(cl, start, end);189return (float)bits / (1 << fractional_size);190}191192static inline float193__gen_unpack_ufixed(const uint8_t *restrict cl, uint32_t start, uint32_t end,194uint32_t fractional_size)195{196int32_t bits = __gen_unpack_uint(cl, start, end);197return (float)bits / (1 << fractional_size);198}199200static inline float201__gen_unpack_float(const uint8_t *restrict cl, uint32_t start, uint32_t end)202{203assert(start % 8 == 0);204assert(end - start == 31);205206struct PACKED { float f; } *f = (void *)(cl + (start / 8));207208return f->f;209}210211static inline float212__gen_unpack_f187(const uint8_t *restrict cl, uint32_t start, uint32_t end)213{214assert(end - start == 15);215uint32_t bits = __gen_unpack_uint(cl, start, end);216return uif(bits << 16);217}218219#endif220221222