-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqnotation.h
More file actions
86 lines (74 loc) · 1.51 KB
/
qnotation.h
File metadata and controls
86 lines (74 loc) · 1.51 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// https://en.wikipedia.org/wiki/Q_(number_format)
#ifndef QNOTATION_H
#define QNOTATION_H
#define Q_TO_FLOAT(fp_type, q_type) _Generic((q_type),\
uq6_2_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/2),\
uq4_4_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/4),\
\
uq14_2_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/2),\
uq8_8_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/8),\
\
uq30_2_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/2),\
uq16_16_t: (((fp_type)q_type.integer) + ((fp_type)q_type.fraction)/16),\
default: (fp_type)NAN\
)
// unsigned 8-bit
typedef union {
uint8_t raw;
struct { uint8_t
fraction :1-0 +1,
integer :7-2 +1;
};
} uq6_2_t;
typedef union {
uint8_t raw;
struct { uint8_t
fraction :3-0 +1,
integer :7-4 +1;
};
} uq4_4_t;
// unsigned 16-bit
typedef union {
uint16_t raw;
struct { uint16_t
fraction :1-0 +1,
integer :15-2 +1;
};
} uq14_2_t;
typedef union {
uint16_t raw;
struct { uint16_t
fraction :7-0 +1,
integer :15-8 +1;
};
} uq8_8_t;
// unsigned 32-bit
typedef union {
uint32_t raw;
struct { uint32_t
fraction :1-0 +1,
integer :31-2 +1;
};
} uq30_2_t;
typedef union {
uint32_t raw;
struct { uint32_t
fraction :15-0 +1,
integer :31-16 +1;
};
} uq16_16_t;
#pragma pack(push, 1)
struct _q_fourteen_bytes {
// 8-bit
uq6_2_t a;
uq4_4_t b;
// 16-bit
uq14_2_t c;
uq8_8_t d;
// 32-bit
uq30_2_t e;
uq16_16_t f;
};
#pragma pack(pop)
static_assert(sizeof(struct _q_fourteen_bytes) == 14);
#endif