-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md
More file actions
38 lines (31 loc) · 945 Bytes
/
README.md
File metadata and controls
38 lines (31 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# bround
bround, broundf, broundh, ubround, ubroundf, ubroundh - mantissa overflow round to nearest un/signed byte for C23 and above
## About
```c
static inline uint8_t ubroundf(float x)
{
x += (float)((1u << (FLT_MANT_DIG - 1)) + (1u << (FLT_MANT_DIG - 2)));
return *(uint8_t*)&x;
}
static inline uint8_t ubroundh(_Float16 x)
{
x += (_Float16)((1u << (HALF_MANT_DIG - 1)) + (1u << (HALF_MANT_DIG - 2)));
return *(uint8_t*)&x;
}
```
## Example
VGA6 to RGB8 using `ubroundf` compared to SHR6
```c
#include <stdlib.h>
#include <stdio.h>
#include "bround.h"
static inline uint8_t vga2rgb(uint8_t v6) { return ubroundf(v6*255.0f/63.0f); }
static inline uint8_t shr6lvl(uint8_t v6) { return (v6 << 2) | (v6 >> 4); }
int main(int argc, char** argv)
{
printf("vga: man/shr\n");
for(uint8_t i = 0; i < 64; i++)
printf("%03hhu: %03hhu/%03hhu\n", i, vga2rgb(i), shr6lvl(i));
exit(EXIT_SUCCESS);
}
```