xref: /aosp_15_r20/external/mesa3d/src/asahi/compiler/agx_minifloat.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2021 Alyssa Rosenzweig
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #pragma once
7 
8 #include <math.h>
9 #include "util/macros.h"
10 
11 /* AGX includes an 8-bit floating-point format for small dyadic immediates,
12  * consisting of 3 bits for the exponent, 4 bits for the mantissa, and 1-bit
13  * for sign, in the usual order. Zero exponent has special handling. */
14 
15 static inline float
agx_minifloat_decode(uint8_t imm)16 agx_minifloat_decode(uint8_t imm)
17 {
18    float sign = (imm & 0x80) ? -1.0 : 1.0;
19    signed exp = (imm & 0x70) >> 4;
20    unsigned mantissa = (imm & 0xF);
21 
22    if (exp)
23       return ldexpf(sign * (float)(mantissa | 0x10), exp - 7);
24    else
25       return ldexpf(sign * ((float)mantissa), -6);
26 }
27 
28 /* Encodes a float. Results are only valid if the float can be represented
29  * exactly, if not the result of this function is UNDEFINED. However, it is
30  * guaranteed that this function will not crash on out-of-spec inputs, so it is
31  * safe to call on any input. signbit() is used to ensure -0.0 is handled
32  * correctly.
33  */
34 static inline uint8_t
agx_minifloat_encode(float f)35 agx_minifloat_encode(float f)
36 {
37    unsigned sign = signbit(f) ? 0x80 : 0;
38    f = fabsf(f);
39 
40    /* frac is in [0.5, 1) and f = frac * 2^exp */
41    int exp = 0;
42    float frac = frexpf(f, &exp);
43 
44    if (f >= 0.25) {
45       unsigned mantissa = (frac * 32.0);
46       exp -= 5; /* 2^5 = 32 */
47       exp = CLAMP(exp + 7, 0, 7);
48 
49       return sign | (exp << 4) | (mantissa & 0xF);
50    } else {
51       unsigned mantissa = (f * 64.0f);
52 
53       return sign | mantissa;
54    }
55 }
56 
57 static inline bool
agx_minifloat_exact(float f)58 agx_minifloat_exact(float f)
59 {
60    float f_ = agx_minifloat_decode(agx_minifloat_encode(f));
61    return memcmp(&f, &f_, sizeof(float)) == 0;
62 }
63