/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 | 40.5k | inline uint16_t quantize_fp16(float x) { |
39 | | // Use bit manipulation for FP32 to FP16 conversion |
40 | 40.5k | union { |
41 | 40.5k | float f; |
42 | 40.5k | uint32_t ui; |
43 | 40.5k | } f32 = {x}; |
44 | | |
45 | 40.5k | uint32_t f = f32.ui; |
46 | 40.5k | uint16_t result; |
47 | | |
48 | | // Extract components from FP32 |
49 | 40.5k | uint32_t sign = (f >> 31) & 0x1; |
50 | 40.5k | uint32_t exponent = (f >> 23) & 0xFF; |
51 | 40.5k | uint32_t mantissa = f & 0x7FFFFF; |
52 | | |
53 | | // Handle special cases |
54 | 40.5k | if (exponent == 0xFF) { |
55 | | // Infinity or NaN in FP32 |
56 | 727 | if (mantissa == 0) { |
57 | | // Infinity: exponent=31, mantissa=0 in FP16 |
58 | 602 | result = (sign << 15) | (31 << 10); |
59 | 602 | } else { |
60 | | // NaN: exponent=31, mantissa!=0 in FP16 |
61 | 125 | result = (sign << 15) | (31 << 10) | 0x200; |
62 | 125 | } |
63 | 39.8k | } else if (exponent == 0) { |
64 | | // Denormalized or zero in FP32 |
65 | 780 | result = (sign << 15); |
66 | 39.0k | } 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 | 39.0k | int32_t unbiased_exp = static_cast<int32_t>(exponent) - 127; |
72 | 39.0k | int32_t new_exponent = unbiased_exp + 15; |
73 | | |
74 | 39.0k | if (new_exponent >= 31) { |
75 | | // Overflow to infinity |
76 | 1 | result = (sign << 15) | (31 << 10); |
77 | 39.0k | } else if (new_exponent <= 0) { |
78 | | // FP16 subnormal result: round the shifted significand to nearest even. |
79 | 469 | const int32_t shift = 14 - new_exponent; |
80 | 469 | if (shift > 25) { |
81 | 1 | result = (sign << 15); |
82 | 468 | } else { |
83 | 468 | const uint32_t significand = mantissa | 0x800000; |
84 | 468 | uint32_t half = significand >> shift; |
85 | 468 | const uint32_t remainder = significand & ((1U << shift) - 1U); |
86 | 468 | const uint32_t halfway = 1U << (shift - 1); |
87 | 468 | if (remainder > halfway || (remainder == halfway && (half & 1U))) { |
88 | 0 | ++half; |
89 | 0 | } |
90 | 468 | result = static_cast<uint16_t>((sign << 15) | half); |
91 | 468 | } |
92 | 38.5k | } else { |
93 | | // Normal: combine sign, exponent (5 bits), mantissa (10 bits) |
94 | | // Round to nearest, ties to even |
95 | 38.5k | uint32_t round_bit = (mantissa >> 12) & 0x1; // Bit that will be rounded off |
96 | 38.5k | uint32_t sticky_bits = mantissa & 0xFFF; // All lower bits |
97 | 38.5k | uint16_t new_mantissa = (mantissa >> 13) & 0x3FF; |
98 | | |
99 | | // Round up if: round_bit is 1 AND (sticky_bits != 0 OR new_mantissa is odd) |
100 | 38.5k | if (round_bit && (sticky_bits || (new_mantissa & 1))) { |
101 | 9.70k | new_mantissa++; |
102 | | // Check for mantissa overflow |
103 | 9.70k | if (new_mantissa > 0x3FF) { |
104 | 2 | new_mantissa = 0; |
105 | 2 | new_exponent++; |
106 | 2 | if (new_exponent >= 31) { |
107 | | // Overflow to infinity |
108 | 0 | result = (sign << 15) | (31 << 10); |
109 | 0 | return result; |
110 | 0 | } |
111 | 2 | } |
112 | 9.70k | } |
113 | | |
114 | 38.5k | result = (sign << 15) | ((new_exponent & 0x1F) << 10) | new_mantissa; |
115 | 38.5k | } |
116 | 39.0k | } |
117 | | |
118 | 40.5k | return result; |
119 | 40.5k | } |
120 | | |
121 | | // Convert float16 (IEEE half-precision) to float32 |
122 | 4.08k | inline float dequantize_fp16(uint16_t h) { |
123 | 4.08k | uint32_t sign = (h >> 15) & 0x1; |
124 | 4.08k | uint32_t exponent = (h >> 10) & 0x1F; |
125 | 4.08k | uint32_t mantissa = h & 0x3FF; |
126 | | |
127 | 4.08k | uint32_t result; |
128 | | |
129 | 4.08k | if (exponent == 0x1F) { |
130 | | // Infinity or NaN |
131 | 437 | if (mantissa == 0) { |
132 | | // Infinity |
133 | 388 | result = (sign << 31) | 0x7F800000; |
134 | 388 | } else { |
135 | | // NaN |
136 | 49 | result = (sign << 31) | 0x7FC00000; |
137 | 49 | } |
138 | 3.65k | } else if (exponent == 0) { |
139 | 370 | if (mantissa == 0) { |
140 | 369 | result = (sign << 31); |
141 | 369 | } else { |
142 | | // Subnormal: normalize into an FP32 normal number. |
143 | 1 | uint32_t new_exponent = 113; |
144 | 11 | while ((mantissa & 0x400) == 0) { |
145 | 10 | mantissa <<= 1; |
146 | 10 | --new_exponent; |
147 | 10 | } |
148 | 1 | result = (sign << 31) | (new_exponent << 23) | ((mantissa & 0x3FF) << 13); |
149 | 1 | } |
150 | 3.28k | } else { |
151 | | // Normal number: bias adjustment from 15 to 127 |
152 | 3.28k | uint32_t new_exponent = exponent + (127 - 15); |
153 | 3.28k | uint32_t new_mantissa = mantissa << 13; |
154 | 3.28k | result = (sign << 31) | ((new_exponent & 0xFF) << 23) | new_mantissa; |
155 | 3.28k | } |
156 | | |
157 | 4.08k | union { |
158 | 4.08k | uint32_t ui; |
159 | 4.08k | float f; |
160 | 4.08k | } f32 = {result}; |
161 | | |
162 | 4.08k | return f32.f; |
163 | 4.08k | } |
164 | | |
165 | | } // namespace labneura |