chiark / gitweb /
Initial checkin
[clg] / gffi / alien / memory.c
1 #include <stdint.h>
2
3 uint16_t gffi_get_uint_16_swapped (uint8_t* location, int offset)
4 {
5   uint8_t buf[2];
6   buf[1] = location[offset];
7   buf[0] = location[offset + 1];
8   return *(uint16_t*)buf;
9 }
10
11 void gffi_set_uint_16_swapped (uint8_t* location, int offset, uint16_t value)
12 {
13   uint8_t *buf = (uint8_t*)&value;
14   
15   location[offset + 0] = buf[1];
16   location[offset + 1] = buf[0];
17 }
18
19 uint32_t gffi_get_uint_32_swapped (uint8_t* location, int offset)
20 {
21   uint8_t buf[4];
22   buf[3] = location[offset];
23   buf[2] = location[offset + 1];
24   buf[1] = location[offset + 2];
25   buf[0] = location[offset + 3];
26   return *(uint32_t*)buf;
27 }
28
29 void gffi_set_uint_32_swapped (uint8_t* location, int offset, uint32_t value)
30 {
31   uint8_t *buf = (uint8_t*)&value;
32   
33   location[offset + 0] = buf[3];
34   location[offset + 1] = buf[2];
35   location[offset + 2] = buf[1];
36   location[offset + 3] = buf[0];
37 }
38
39 uint64_t gffi_get_uint_64_swapped (uint8_t* location, int offset)
40 {
41   uint8_t buf[8];
42   buf[7] = location[offset];
43   buf[6] = location[offset + 1];
44   buf[5] = location[offset + 2];
45   buf[4] = location[offset + 3];
46   buf[3] = location[offset + 4];
47   buf[2] = location[offset + 5];
48   buf[1] = location[offset + 6];
49   buf[0] = location[offset + 7];
50   return *(uint64_t*)buf;
51 }
52
53 void gffi_set_uint_64_swapped (uint8_t* location, int offset, uint64_t value)
54 {
55   uint8_t *buf = (uint8_t*)&value;
56   
57   location[offset + 0] = buf[7];
58   location[offset + 1] = buf[6];
59   location[offset + 2] = buf[5];
60   location[offset + 3] = buf[4];
61   location[offset + 4] = buf[3];
62   location[offset + 5] = buf[2];
63   location[offset + 6] = buf[1];
64   location[offset + 7] = buf[0];
65 }
66
67 int16_t gffi_get_int_16_swapped (int8_t* location, int offset)
68 {
69   int8_t buf[2];
70   buf[1] = location[offset];
71   buf[0] = location[offset + 1];
72   return *(int16_t*)buf;
73 }
74
75 void gffi_set_int_16_swapped (uint8_t* location, int offset, int16_t value)
76 {
77   uint8_t *buf = (uint8_t*)&value;
78   
79   location[offset + 0] = buf[1];
80   location[offset + 1] = buf[0];
81 }
82
83 int32_t gffi_get_int_32_swapped (int8_t* location, int offset)
84 {
85   int8_t buf[4];
86   buf[3] = location[offset];
87   buf[2] = location[offset + 1];
88   buf[1] = location[offset + 2];
89   buf[0] = location[offset + 3];
90   return *(int32_t*)buf;
91 }
92
93 void gffi_set_int_32_swapped (uint8_t* location, int offset, int32_t value)
94 {
95   uint8_t *buf = (uint8_t*)&value;
96   
97   location[offset + 0] = buf[3];
98   location[offset + 1] = buf[2];
99   location[offset + 2] = buf[1];
100   location[offset + 3] = buf[0];
101 }
102
103 int64_t gffi_get_int_64_swapped (int8_t* location, int offset)
104 {
105   int8_t buf[8];
106   buf[7] = location[offset];
107   buf[6] = location[offset + 1];
108   buf[5] = location[offset + 2];
109   buf[4] = location[offset + 3];
110   buf[3] = location[offset + 4];
111   buf[2] = location[offset + 5];
112   buf[1] = location[offset + 6];
113   buf[0] = location[offset + 7];
114   return *(int64_t*)buf;
115 }
116
117 void gffi_set_int_64_swapped (uint8_t* location, int offset, int64_t value)
118 {
119   uint8_t *buf = (uint8_t*)&value;
120   
121   location[offset + 0] = buf[7];
122   location[offset + 1] = buf[6];
123   location[offset + 2] = buf[5];
124   location[offset + 3] = buf[4];
125   location[offset + 4] = buf[3];
126   location[offset + 5] = buf[2];
127   location[offset + 6] = buf[1];
128   location[offset + 7] = buf[0];
129 }
130
131 float gffi_get_single_float_swapped (int8_t* location, int offset)
132 {
133   int8_t buf[4];
134   buf[3] = location[offset];
135   buf[2] = location[offset + 1];
136   buf[1] = location[offset + 2];
137   buf[0] = location[offset + 3];
138   return *(float*)buf;
139 }
140
141 void gffi_set_single_float_swapped (uint8_t* location, int offset, float value)
142 {
143   uint8_t *buf = (uint8_t*)&value;
144   
145   location[offset + 0] = buf[3];
146   location[offset + 1] = buf[2];
147   location[offset + 2] = buf[1];
148   location[offset + 3] = buf[0];
149 }
150
151 double gffi_get_double_float_swapped (int8_t* location, int offset)
152 {
153   int8_t buf[8];
154   buf[7] = location[offset];
155   buf[6] = location[offset + 1];
156   buf[5] = location[offset + 2];
157   buf[4] = location[offset + 3];
158   buf[3] = location[offset + 4];
159   buf[2] = location[offset + 5];
160   buf[1] = location[offset + 6];
161   buf[0] = location[offset + 7];
162   return *(double*)buf;
163 }
164
165 void gffi_set_double_float_swapped (uint8_t* location, int offset, double value)
166 {
167   uint8_t *buf = (uint8_t*)&value;
168   
169   location[offset + 0] = buf[7];
170   location[offset + 1] = buf[6];
171   location[offset + 2] = buf[5];
172   location[offset + 3] = buf[4];
173   location[offset + 4] = buf[3];
174   location[offset + 5] = buf[2];
175   location[offset + 6] = buf[1];
176   location[offset + 7] = buf[0];
177 }