/home/runner/work/LabNeura/LabNeura/include/labneura/types.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Simple, tiny header to expose a couple of numeric helpers. |
2 | | #pragma once |
3 | | |
4 | | #include <cstdint> |
5 | | #include <cmath> |
6 | | |
7 | | namespace labneura { |
8 | | |
9 | | // Quantize a single float into signed 8-bit using scale and zero-point. |
10 | 7 | inline int8_t quantize_int8(float x, float scale, int32_t zero_point = 0) { |
11 | 7 | if (scale == 0.0f) return static_cast<int8_t>(zero_point); |
12 | 5 | int32_t q = static_cast<int32_t>(std::nearbyint(x / scale)) + zero_point; |
13 | 5 | if (q > 127) q = 127; |
14 | 5 | if (q < -128) q = -128; |
15 | 5 | return static_cast<int8_t>(q); |
16 | 7 | } |
17 | | |
18 | | // Dequantize signed 8-bit back to float. |
19 | 6 | inline float dequantize_int8(int8_t q, float scale, int32_t zero_point = 0) { |
20 | 6 | return scale * (static_cast<int32_t>(q) - zero_point); |
21 | 6 | } |
22 | | |
23 | | // Quantize a single float into signed 16-bit using scale and zero-point. |
24 | 61 | inline int16_t quantize_int16(float x, float scale, int32_t zero_point = 0) { |
25 | 61 | if (scale == 0.0f) return static_cast<int16_t>(zero_point); |
26 | 60 | int32_t q = static_cast<int32_t>(std::nearbyint(x / scale)) + zero_point; |
27 | 60 | if (q > 32767) q = 32767; |
28 | 60 | if (q < -32768) q = -32768; |
29 | 60 | return static_cast<int16_t>(q); |
30 | 61 | } |
31 | | |
32 | | // Dequantize signed 16-bit back to float. |
33 | 58 | inline float dequantize_int16(int16_t q, float scale, int32_t zero_point = 0) { |
34 | 58 | return scale * (static_cast<int32_t>(q) - zero_point); |
35 | 58 | } |
36 | | |
37 | | // Convert float32 to float16 (IEEE half-precision) |
38 | 39.3k | inline uint16_t quantize_fp16(float x) { |
39 | | // Use bit manipulation for FP32 to FP16 conversion |
40 | 39.3k | union { |
41 | 39.3k | float f; |
42 | 39.3k | uint32_t ui; |
43 | 39.3k | } f32 = {x}; |
44 | | |
45 | 39.3k | uint32_t f = f32.ui; |
46 | 39.3k | uint16_t result; |
47 | | |
48 | | // Extract components from FP32 |
49 | 39.3k | uint32_t sign = (f >> 31) & 0x1; |
50 | 39.3k | uint32_t exponent = (f >> 23) & 0xFF; |
51 | 39.3k | uint32_t mantissa = f & 0x7FFFFF; |
52 | | |
53 | | // Handle special cases |
54 | 39.3k | if (exponent == 0xFF) { |
55 | | // Infinity or NaN in FP32 |
56 | 5 | if (mantissa == 0) { |
57 | | // Infinity: exponent=31, mantissa=0 in FP16 |
58 | 4 | result = (sign << 15) | (31 << 10); |
59 | 4 | } else { |
60 | | // NaN: exponent=31, mantissa!=0 in FP16 |
61 | 1 | result = (sign << 15) | (31 << 10) | 0x200; |
62 | 1 | } |
63 | 39.3k | } else if (exponent == 0) { |
64 | | // Denormalized or zero in FP32 |
65 | 599 | result = (sign << 15); |
66 | 38.7k | } else { |
67 | | // Normal number: bias adjustment |
68 | | // FP32: exponent - bias(127) = exponent - 127 |
69 | | // FP16: new_exponent - bias(15) = result |
70 | | // So: new_exponent = exponent - 127 + 15 |
71 | 38.7k | int32_t unbiased_exp = static_cast<int32_t>(exponent) - 127; |
72 | 38.7k | int32_t new_exponent = unbiased_exp + 15; |
73 | | |
74 | 38.7k | if (new_exponent >= 31) { |
75 | | // Overflow to infinity |
76 | 257 | result = (sign << 15) | (31 << 10); |
77 | 38.5k | } else if (new_exponent <= 0) { |
78 | | // Underflow to zero |
79 | 1 | result = (sign << 15); |
80 | 38.5k | } else { |
81 | | // Normal: combine sign, exponent (5 bits), mantissa (10 bits) |
82 | | // Round to nearest, ties to even |
83 | 38.5k | uint32_t round_bit = (mantissa >> 12) & 0x1; // Bit that will be rounded off |
84 | 38.5k | uint32_t sticky_bits = mantissa & 0xFFF; // All lower bits |
85 | 38.5k | uint16_t new_mantissa = (mantissa >> 13) & 0x3FF; |
86 | | |
87 | | // Round up if: round_bit is 1 AND (sticky_bits != 0 OR new_mantissa is odd) |
88 | 38.5k | if (round_bit && (sticky_bits || (new_mantissa & 1))) { |
89 | 11.5k | new_mantissa++; |
90 | | // Check for mantissa overflow |
91 | 11.5k | if (new_mantissa > 0x3FF) { |
92 | 3 | new_mantissa = 0; |
93 | 3 | new_exponent++; |
94 | 3 | if (new_exponent >= 31) { |
95 | | // Overflow to infinity |
96 | 0 | result = (sign << 15) | (31 << 10); |
97 | 0 | return result; |
98 | 0 | } |
99 | 3 | } |
100 | 11.5k | } |
101 | | |
102 | 38.5k | result = (sign << 15) | ((new_exponent & 0x1F) << 10) | new_mantissa; |
103 | 38.5k | } |
104 | 38.7k | } |
105 | | |
106 | 39.3k | return result; |
107 | 39.3k | } |
108 | | |
109 | | // Convert float16 (IEEE half-precision) to float32 |
110 | 15.7k | inline float dequantize_fp16(uint16_t h) { |
111 | 15.7k | uint32_t sign = (h >> 15) & 0x1; |
112 | 15.7k | uint32_t exponent = (h >> 10) & 0x1F; |
113 | 15.7k | uint32_t mantissa = h & 0x3FF; |
114 | | |
115 | 15.7k | uint32_t result; |
116 | | |
117 | 15.7k | if (exponent == 0x1F) { |
118 | | // Infinity or NaN |
119 | 5 | if (mantissa == 0) { |
120 | | // Infinity |
121 | 4 | result = (sign << 31) | 0x7F800000; |
122 | 4 | } else { |
123 | | // NaN |
124 | 1 | result = (sign << 31) | 0x7FC00000; |
125 | 1 | } |
126 | 15.7k | } else if (exponent == 0) { |
127 | | // Zero (denormalized numbers are treated as zero for simplicity) |
128 | 49 | result = (sign << 31); |
129 | 15.7k | } else { |
130 | | // Normal number: bias adjustment from 15 to 127 |
131 | 15.7k | uint32_t new_exponent = exponent + (127 - 15); |
132 | 15.7k | uint32_t new_mantissa = mantissa << 13; |
133 | 15.7k | result = (sign << 31) | ((new_exponent & 0xFF) << 23) | new_mantissa; |
134 | 15.7k | } |
135 | | |
136 | 15.7k | union { |
137 | 15.7k | uint32_t ui; |
138 | 15.7k | float f; |
139 | 15.7k | } f32 = {result}; |
140 | | |
141 | 15.7k | return f32.f; |
142 | 15.7k | } |
143 | | |
144 | | } // namespace labneura |