xref: /aosp_15_r20/external/mesa3d/src/broadcom/cle/v3d_packet_helpers.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 MESA_V3D_PACKET_HELPERS_H
25 #define MESA_V3D_PACKET_HELPERS_H
26 
27 #include "util/bitpack_helpers.h"
28 
29 #ifdef HAVE_VALGRIND
30 #include <valgrind.h>
31 #include <memcheck.h>
32 #define VG(x) x
33 #else
34 #define VG(x) ((void)0)
35 #endif
36 
37 static inline uint64_t
__gen_offset(uint64_t v,uint32_t start,uint32_t end)38 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
39 {
40    util_bitpack_validate_value(v);
41 #ifndef NDEBUG
42    uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
43 
44    assert((v & ~mask) == 0);
45 #endif
46 
47    return v;
48 }
49 
50 static inline uint64_t
__gen_unpack_uint(const uint8_t * restrict cl,uint32_t start,uint32_t end)51 __gen_unpack_uint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
52 {
53    uint64_t val = 0;
54    const int width = end - start + 1;
55    const uint32_t mask = (width == 32 ? ~0 : (1 << width) - 1 );
56 
57    for (uint32_t byte = start / 8; byte <= end / 8; byte++) {
58       val |= cl[byte] << ((byte - start / 8) * 8);
59    }
60 
61    return (val >> (start % 8)) & mask;
62 }
63 
64 static inline uint64_t
__gen_unpack_sint(const uint8_t * restrict cl,uint32_t start,uint32_t end)65 __gen_unpack_sint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
66 {
67    int size = end - start + 1;
68    int64_t val = __gen_unpack_uint(cl, start, end);
69 
70    /* Get the sign bit extended. */
71    return (val << (64 - size)) >> (64 - size);
72 }
73 
74 static inline float
__gen_unpack_sfixed(const uint8_t * restrict cl,uint32_t start,uint32_t end,uint32_t fractional_size)75 __gen_unpack_sfixed(const uint8_t *restrict cl, uint32_t start, uint32_t end,
76                     uint32_t fractional_size)
77 {
78         int32_t bits = __gen_unpack_sint(cl, start, end);
79         return (float)bits / (1 << fractional_size);
80 }
81 
82 static inline float
__gen_unpack_ufixed(const uint8_t * restrict cl,uint32_t start,uint32_t end,uint32_t fractional_size)83 __gen_unpack_ufixed(const uint8_t *restrict cl, uint32_t start, uint32_t end,
84                     uint32_t fractional_size)
85 {
86         int32_t bits = __gen_unpack_uint(cl, start, end);
87         return (float)bits / (1 << fractional_size);
88 }
89 
90 static inline float
__gen_unpack_float(const uint8_t * restrict cl,uint32_t start,uint32_t end)91 __gen_unpack_float(const uint8_t *restrict cl, uint32_t start, uint32_t end)
92 {
93    assert(start % 8 == 0);
94    assert(end - start == 31);
95 
96    struct PACKED { float f; } *f = (void *)(cl + (start / 8));
97 
98    return f->f;
99 }
100 
101 static inline float
__gen_unpack_f187(const uint8_t * restrict cl,uint32_t start,uint32_t end)102 __gen_unpack_f187(const uint8_t *restrict cl, uint32_t start, uint32_t end)
103 {
104    assert(end - start == 15);
105    uint32_t bits = __gen_unpack_uint(cl, start, end);
106    return uif(bits << 16);
107 }
108 
109 #endif
110