-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_3.c
96 lines (75 loc) · 2.6 KB
/
test_3.c
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
87
88
89
90
91
92
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
// if struct field have different types, it will check the original type rather than the bitfield hints
//
typedef struct Format {
uint32_t frac : 23 ; // 4 bytes
uint8_t expo : 8 ; // 1 byte
bool sign : 1 ; // 1 byte
// padding 2 bytes
} Format;
typedef union __attribute__((__packed__)) Float1 {
float num;
struct __attribute__((__packed__)) {
uint32_t frac : 23; // 4 bytes
uint8_t expo : 8 ; // 1 byte
bool sign : 1 ; // 1 byte
};
} Float1; // max bytes
typedef union __attribute__((__packed__)) Float2 {
float num;
struct {
uint32_t frac : 23; // 4 bytes
uint8_t expo : 8; // 1 byte
bool sign : 1; // 1 byte
};
} Float2;
typedef union Float3 {
float num;
struct __attribute__((__packed__)) {
uint32_t frac : 23; // 4 bytes
uint8_t expo : 8; // 1 byte
bool sign : 1; // 1 byte
};
} Float3;
typedef union Float4 {
float num;
struct {
uint32_t frac : 23; // 4 bytes
uint32_t expo : 8; // 1 byte
uint32_t sign : 1; // 1 byte
};
} Float4;
typedef union Float5 {
float num;
struct __attribute__((__packed__)){
uint32_t frac : 23; // 4 bytes
uint8_t expo : 8; // 1 byte
uint8_t sign : 1; // 1 byte
};
} Float5;
// <Question>
// why typedef union Float {
// float num;
// struct {
// uint32_t frac : 23;
// uint8_t expo : 8;
// bool sign : 1;
// };
// } Float;
// is 64 bits;
int main(){
// Float my_float;
// my_float.num = 65.65625;
printf("booling size : %d\n", sizeof(bool) );
printf("uint_8 size : %d\n", sizeof(uint8_t) );
printf("Format size : %d\n", sizeof(Format) );
printf("floating points 1 size : %d\n", sizeof(Float1) );
printf("floating points 2 size : %d\n", sizeof(Float2) );
printf("floating points 3 size : %d\n", sizeof(Float3) );
printf("floating points 4 size : %d\n", sizeof(Float4) );
printf("floating points 5 size : %d\n", sizeof(Float5) );
// printf("floating number : %x\n", my_float.num);
// printf("sign: %d, expo: %d, frac: %x\n", my_float.sign, my_float.expo, my_float.frac);
}