1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define DATA_BITS (1 << (3 + SHIFT))
21
22#if DATA_BITS == 8
23#define SUFFIX b
24#define DATA_TYPE uint8_t
25#elif DATA_BITS == 16
26#define SUFFIX w
27#define DATA_TYPE uint16_t
28#elif DATA_BITS == 32
29#define SUFFIX l
30#define DATA_TYPE uint32_t
31#elif DATA_BITS == 64
32#define SUFFIX q
33#define DATA_TYPE uint64_t
34#else
35#error unhandled operand size
36#endif
37
38#define SIGN_MASK (((DATA_TYPE)1) << (DATA_BITS - 1))
39
40
41
42static int glue(compute_all_add, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
43{
44 int cf, pf, af, zf, sf, of;
45 DATA_TYPE src2 = dst - src1;
46
47 cf = dst < src1;
48 pf = parity_table[(uint8_t)dst];
49 af = (dst ^ src1 ^ src2) & CC_A;
50 zf = (dst == 0) * CC_Z;
51 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
52 of = lshift((src1 ^ src2 ^ -1) & (src1 ^ dst), 12 - DATA_BITS) & CC_O;
53 return cf | pf | af | zf | sf | of;
54}
55
56static int glue(compute_c_add, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
57{
58 return dst < src1;
59}
60
61static int glue(compute_all_adc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1,
62 DATA_TYPE src3)
63{
64 int cf, pf, af, zf, sf, of;
65 DATA_TYPE src2 = dst - src1 - src3;
66
67 cf = (src3 ? dst <= src1 : dst < src1);
68 pf = parity_table[(uint8_t)dst];
69 af = (dst ^ src1 ^ src2) & 0x10;
70 zf = (dst == 0) << 6;
71 sf = lshift(dst, 8 - DATA_BITS) & 0x80;
72 of = lshift((src1 ^ src2 ^ -1) & (src1 ^ dst), 12 - DATA_BITS) & CC_O;
73 return cf | pf | af | zf | sf | of;
74}
75
76static int glue(compute_c_adc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1,
77 DATA_TYPE src3)
78{
79 return src3 ? dst <= src1 : dst < src1;
80}
81
82static int glue(compute_all_sub, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2)
83{
84 int cf, pf, af, zf, sf, of;
85 DATA_TYPE src1 = dst + src2;
86
87 cf = src1 < src2;
88 pf = parity_table[(uint8_t)dst];
89 af = (dst ^ src1 ^ src2) & CC_A;
90 zf = (dst == 0) * CC_Z;
91 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
92 of = lshift((src1 ^ src2) & (src1 ^ dst), 12 - DATA_BITS) & CC_O;
93 return cf | pf | af | zf | sf | of;
94}
95
96static int glue(compute_c_sub, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2)
97{
98 DATA_TYPE src1 = dst + src2;
99
100 return src1 < src2;
101}
102
103static int glue(compute_all_sbb, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2,
104 DATA_TYPE src3)
105{
106 int cf, pf, af, zf, sf, of;
107 DATA_TYPE src1 = dst + src2 + src3;
108
109 cf = (src3 ? src1 <= src2 : src1 < src2);
110 pf = parity_table[(uint8_t)dst];
111 af = (dst ^ src1 ^ src2) & 0x10;
112 zf = (dst == 0) << 6;
113 sf = lshift(dst, 8 - DATA_BITS) & 0x80;
114 of = lshift((src1 ^ src2) & (src1 ^ dst), 12 - DATA_BITS) & CC_O;
115 return cf | pf | af | zf | sf | of;
116}
117
118static int glue(compute_c_sbb, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2,
119 DATA_TYPE src3)
120{
121 DATA_TYPE src1 = dst + src2 + src3;
122
123 return (src3 ? src1 <= src2 : src1 < src2);
124}
125
126static int glue(compute_all_logic, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
127{
128 int cf, pf, af, zf, sf, of;
129
130 cf = 0;
131 pf = parity_table[(uint8_t)dst];
132 af = 0;
133 zf = (dst == 0) * CC_Z;
134 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
135 of = 0;
136 return cf | pf | af | zf | sf | of;
137}
138
139static int glue(compute_all_inc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
140{
141 int cf, pf, af, zf, sf, of;
142 DATA_TYPE src2;
143
144 cf = src1;
145 src1 = dst - 1;
146 src2 = 1;
147 pf = parity_table[(uint8_t)dst];
148 af = (dst ^ src1 ^ src2) & CC_A;
149 zf = (dst == 0) * CC_Z;
150 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
151 of = (dst == SIGN_MASK) * CC_O;
152 return cf | pf | af | zf | sf | of;
153}
154
155static int glue(compute_all_dec, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
156{
157 int cf, pf, af, zf, sf, of;
158 DATA_TYPE src2;
159
160 cf = src1;
161 src1 = dst + 1;
162 src2 = 1;
163 pf = parity_table[(uint8_t)dst];
164 af = (dst ^ src1 ^ src2) & CC_A;
165 zf = (dst == 0) * CC_Z;
166 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
167 of = (dst == SIGN_MASK - 1) * CC_O;
168 return cf | pf | af | zf | sf | of;
169}
170
171static int glue(compute_all_shl, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
172{
173 int cf, pf, af, zf, sf, of;
174
175 cf = (src1 >> (DATA_BITS - 1)) & CC_C;
176 pf = parity_table[(uint8_t)dst];
177 af = 0;
178 zf = (dst == 0) * CC_Z;
179 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
180
181 of = lshift(src1 ^ dst, 12 - DATA_BITS) & CC_O;
182 return cf | pf | af | zf | sf | of;
183}
184
185static int glue(compute_c_shl, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
186{
187 return (src1 >> (DATA_BITS - 1)) & CC_C;
188}
189
190static int glue(compute_all_sar, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
191{
192 int cf, pf, af, zf, sf, of;
193
194 cf = src1 & 1;
195 pf = parity_table[(uint8_t)dst];
196 af = 0;
197 zf = (dst == 0) * CC_Z;
198 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
199
200 of = lshift(src1 ^ dst, 12 - DATA_BITS) & CC_O;
201 return cf | pf | af | zf | sf | of;
202}
203
204
205
206
207static int glue(compute_all_mul, SUFFIX)(DATA_TYPE dst, target_long src1)
208{
209 int cf, pf, af, zf, sf, of;
210
211 cf = (src1 != 0);
212 pf = parity_table[(uint8_t)dst];
213 af = 0;
214 zf = (dst == 0) * CC_Z;
215 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
216 of = cf * CC_O;
217 return cf | pf | af | zf | sf | of;
218}
219
220static int glue(compute_all_bmilg, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
221{
222 int cf, pf, af, zf, sf, of;
223
224 cf = (src1 == 0);
225 pf = 0;
226 af = 0;
227 zf = (dst == 0) * CC_Z;
228 sf = lshift(dst, 8 - DATA_BITS) & CC_S;
229 of = 0;
230 return cf | pf | af | zf | sf | of;
231}
232
233static int glue(compute_c_bmilg, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
234{
235 return src1 == 0;
236}
237
238#undef DATA_BITS
239#undef SIGN_MASK
240#undef DATA_TYPE
241#undef DATA_MASK
242#undef SUFFIX
243