fixed_point_add.c
21.4 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -fpadding-on-unsigned-fixed-point -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,UNSIGNED
// Addition between different fixed point types
short _Accum sa_const = 1.0hk + 2.0hk; // CHECK-DAG: @sa_const = {{.*}}global i16 384, align 2
_Accum a_const = 1.0hk + 2.0k; // CHECK-DAG: @a_const = {{.*}}global i32 98304, align 4
long _Accum la_const = 1.0hk + 2.0lk; // CHECK-DAG: @la_const = {{.*}}global i64 6442450944, align 8
short _Accum sa_const2 = 0.5hr + 2.0hk; // CHECK-DAG: @sa_const2 = {{.*}}global i16 320, align 2
short _Accum sa_const3 = 0.5r + 2.0hk; // CHECK-DAG: @sa_const3 = {{.*}}global i16 320, align 2
short _Accum sa_const4 = 0.5lr + 2.0hk; // CHECK-DAG: @sa_const4 = {{.*}}global i16 320, align 2
// Unsigned addition
unsigned short _Accum usa_const = 1.0uhk + 2.0uhk;
// CHECK-SIGNED-DAG: @usa_const = {{.*}}global i16 768, align 2
// CHECK-UNSIGNED-DAG: @usa_const = {{.*}}global i16 384, align 2
// Unsigned + signed
short _Accum sa_const5 = 1.0uhk + 2.0hk;
// CHECK-DAG: @sa_const5 = {{.*}}global i16 384, align 2
// Addition with negative number
short _Accum sa_const6 = 0.5hr + (-2.0hk);
// CHECK-DAG: @sa_const6 = {{.*}}global i16 -192, align 2
// Int addition
unsigned short _Accum usa_const2 = 2 + 0.5uhk;
// CHECK-SIGNED-DAG: @usa_const2 = {{.*}}global i16 640, align 2
// CHECK-UNSIGNED-DAG: @usa_const2 = {{.*}}global i16 320, align 2
short _Accum sa_const7 = 2 + (-0.5hk); // CHECK-DAG: @sa_const7 = {{.*}}global i16 192, align 2
short _Accum sa_const8 = 257 + (-2.0hk); // CHECK-DAG: @sa_const8 = {{.*}}global i16 32640, align 2
long _Fract lf_const = -0.5lr + 1; // CHECK-DAG: @lf_const = {{.*}}global i32 1073741824, align 4
// Saturated addition
_Sat short _Accum sat_sa_const = (_Sat short _Accum)128.0hk + 128.0hk;
// CHECK-DAG: @sat_sa_const = {{.*}}global i16 32767, align 2
_Sat unsigned short _Accum sat_usa_const = (_Sat unsigned short _Accum)128.0uhk + 128.0uhk;
// CHECK-SIGNED-DAG: @sat_usa_const = {{.*}}global i16 65535, align 2
// CHECK-UNSIGNED-DAG: @sat_usa_const = {{.*}}global i16 32767, align 2
_Sat short _Accum sat_sa_const2 = (_Sat short _Accum)128.0hk + 128;
// CHECK-DAG: @sat_sa_const2 = {{.*}}global i16 32767, align 2
_Sat unsigned short _Accum sat_usa_const2 = (_Sat unsigned short _Accum)128.0uhk + 128;
// CHECK-SIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 65535, align 2
// CHECK-UNSIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 32767, align 2
_Sat unsigned short _Accum sat_usa_const3 = (_Sat unsigned short _Accum)0.5uhk + (-2);
// CHECK-DAG: @sat_usa_const3 = {{.*}}global i16 0, align 2
void SignedAddition() {
// CHECK-LABEL: SignedAddition
short _Accum sa;
_Accum a, b, c, d;
long _Accum la;
unsigned short _Accum usa;
unsigned _Accum ua;
unsigned long _Accum ula;
short _Fract sf;
_Fract f;
long _Fract lf;
unsigned short _Fract usf;
unsigned _Fract uf;
unsigned long _Fract ulf;
// Same type
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[SA2:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i16 [[SA]], [[SA2]]
// CHECK-NEXT: store i16 [[SUM]], i16* %sa, align 2
sa = sa + sa;
// To larger scale and larger width
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[A:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[EXT_SA:%[a-z0-9]+]] = sext i16 [[SA]] to i32
// CHECK-NEXT: [[SA:%[a-z0-9]+]] = shl i32 [[EXT_SA]], 8
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i32 [[SA]], [[A]]
// CHECK-NEXT: store i32 [[SUM]], i32* %a, align 4
a = sa + a;
// To same scale and smaller width
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[SF:%[0-9]+]] = load i8, i8* %sf, align 1
// CHECK-NEXT: [[EXT_SF:%[a-z0-9]+]] = sext i8 [[SF]] to i16
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i16 [[SA]], [[EXT_SF]]
// CHECK-NEXT: store i16 [[SUM]], i16* %sa, align 2
sa = sa + sf;
// To smaller scale and same width.
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[F:%[0-9]+]] = load i16, i16* %f, align 2
// CHECK-NEXT: [[EXT_SA:%[a-z0-9]+]] = sext i16 [[SA]] to i24
// CHECK-NEXT: [[SA:%[a-z0-9]+]] = shl i24 [[EXT_SA]], 8
// CHECK-NEXT: [[EXT_F:%[a-z0-9]+]] = sext i16 [[F]] to i24
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i24 [[SA]], [[EXT_F]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = ashr i24 [[SUM]], 8
// CHECK-NEXT: [[TRUNC_RES:%[a-z0-9]+]] = trunc i24 [[RES]] to i16
// CHECK-NEXT: store i16 [[TRUNC_RES]], i16* %sa, align 2
sa = sa + f;
// To smaller scale and smaller width
// CHECK: [[A:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[SF:%[0-9]+]] = load i8, i8* %sf, align 1
// CHECK-NEXT: [[EXT_SF:%[a-z0-9]+]] = sext i8 [[SF]] to i32
// CHECK-NEXT: [[SF:%[a-z0-9]+]] = shl i32 [[EXT_SF]], 8
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i32 [[A]], [[SF]]
// CHECK-NEXT: store i32 [[SUM]], i32* %a, align 4
a = a + sf;
// To larger scale and same width
// CHECK: [[A:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[LF:%[0-9]+]] = load i32, i32* %lf, align 4
// CHECK-NEXT: [[EXT_A:%[a-z0-9]+]] = sext i32 [[A]] to i48
// CHECK-NEXT: [[A:%[a-z0-9]+]] = shl i48 [[EXT_A]], 16
// CHECK-NEXT: [[EXT_LF:%[a-z0-9]+]] = sext i32 [[LF]] to i48
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i48 [[A]], [[EXT_LF]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = ashr i48 [[SUM]], 16
// CHECK-NEXT: [[TRUNC_RES:%[a-z0-9]+]] = trunc i48 [[RES]] to i32
// CHECK-NEXT: store i32 [[TRUNC_RES]], i32* %a, align 4
a = a + lf;
// With corresponding unsigned type
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// SIGNED-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i17
// SIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i17 [[SA_EXT]], 1
// SIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i17
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i17 [[SA]], [[USA_EXT]]
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = ashr i17 [[SUM]], 1
// SIGNED-NEXT: [[SUM:%[a-z0-9]+]] = trunc i17 [[RESULT]] to i16
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i16 [[SA]], [[USA]]
// CHECK-NEXT: store i16 [[SUM]], i16* %sa, align 2
sa = sa + usa;
// With unsigned of larger scale
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[USA:%[0-9]+]] = load i32, i32* %ua, align 4
// SIGNED-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i33
// SIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i33 [[SA_EXT]], 9
// SIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i32 [[USA]] to i33
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i33 [[SA]], [[USA_EXT]]
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = ashr i33 [[SUM]], 1
// SIGNED-NEXT: [[SUM:%[a-z0-9]+]] = trunc i33 [[RESULT]] to i32
// UNSIGNED-NEXT: [[EXT_SA:%[a-z0-9]+]] = sext i16 [[SA]] to i32
// UNSIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i32 [[EXT_SA]], 8
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i32 [[SA]], [[USA]]
// CHECK-NEXT: store i32 [[SUM]], i32* %a, align 4
a = sa + ua;
// With unsigned of smaller width
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[USF:%[0-9]+]] = load i8, i8* %usf, align 1
// SIGNED-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i17
// SIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i17 [[SA_EXT]], 1
// SIGNED-NEXT: [[USF_EXT:%[a-z0-9]+]] = zext i8 [[USF]] to i17
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i17 [[SA]], [[USF_EXT]]
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = ashr i17 [[SUM]], 1
// SIGNED-NEXT: [[SUM:%[a-z0-9]+]] = trunc i17 [[RESULT]] to i16
// UNSIGNED-NEXT: [[EXT_USF:%[a-z0-9]+]] = zext i8 [[USF]] to i16
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i16 [[SA]], [[EXT_USF]]
// CHECK-NEXT: store i16 [[SUM]], i16* %sa, align 2
sa = sa + usf;
// With unsigned of larger width and smaller scale
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[ULF:%[0-9]+]] = load i32, i32* %ulf, align 4
// SIGNED-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i41
// SIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i41 [[SA_EXT]], 25
// SIGNED-NEXT: [[ULF_EXT:%[a-z0-9]+]] = zext i32 [[ULF]] to i41
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i41 [[SA]], [[ULF_EXT]]
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = ashr i41 [[SUM]], 25
// SIGNED-NEXT: [[RES_TRUNC:%[a-z0-9]+]] = trunc i41 [[RESULT]] to i16
// UNSIGNED-NEXT: [[EXT_SA:%[a-z0-9]+]] = sext i16 [[SA]] to i40
// UNSIGNED-NEXT: [[SA:%[a-z0-9]+]] = shl i40 [[EXT_SA]], 24
// UNSIGNED-NEXT: [[EXT_ULF:%[a-z0-9]+]] = zext i32 [[ULF]] to i40
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i40 [[SA]], [[EXT_ULF]]
// UNSIGNED-NEXT: [[RES:%[a-z0-9]+]] = ashr i40 [[SUM]], 24
// UNSIGNED-NEXT: [[RES_TRUNC:%[a-z0-9]+]] = trunc i40 [[RES]] to i16
// CHECK-NEXT: store i16 [[RES_TRUNC]], i16* %sa, align 2
sa = sa + ulf;
// Chained additions of the same signed type should result in the same
// semantics width.
// CHECK: [[A:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[B:%[0-9]+]] = load i32, i32* %b, align 4
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i32 [[A]], [[B]]
// CHECK-NEXT: [[C:%[0-9]+]] = load i32, i32* %c, align 4
// CHECK-NEXT: [[SUM2:%[0-9]+]] = add i32 [[SUM]], [[C]]
// CHECK-NEXT: [[D:%[0-9]+]] = load i32, i32* %d, align 4
// CHECK-NEXT: [[SUM3:%[0-9]+]] = add i32 [[SUM2]], [[D]]
// CHECK-NEXT: store i32 [[SUM3]], i32* %a, align 4
a = a + b + c + d;
}
void UnsignedAddition() {
// CHECK-LABEL: UnsignedAddition
unsigned short _Accum usa;
unsigned _Accum ua;
unsigned long _Accum ula;
unsigned short _Fract usf;
unsigned _Fract uf;
unsigned long _Fract ulf;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[USA2:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i16 [[USA]], [[USA2]]
// CHECK-NEXT: store i16 [[SUM]], i16* %usa, align 2
usa = usa + usa;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[UA:%[0-9]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[EXT_USA:%[a-z0-9]+]] = zext i16 [[USA]] to i32
// CHECK-NEXT: [[USA:%[a-z0-9]+]] = shl i32 [[EXT_USA]], 8
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i32 [[USA]], [[UA]]
// CHECK-NEXT: store i32 [[SUM]], i32* %ua, align 4
ua = usa + ua;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[USF:%[0-9]+]] = load i8, i8* %usf, align 1
// CHECK-NEXT: [[EXT_USF:%[a-z0-9]+]] = zext i8 [[USF]] to i16
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i16 [[USA]], [[EXT_USF]]
// CHECK-NEXT: store i16 [[SUM]], i16* %usa, align 2
usa = usa + usf;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[UF:%[0-9]+]] = load i16, i16* %uf, align 2
// CHECK-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i24
// CHECK-NEXT: [[USA:%[a-z0-9]+]] = shl i24 [[USA_EXT]], 8
// CHECK-NEXT: [[UF_EXT:%[a-z0-9]+]] = zext i16 [[UF]] to i24
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i24 [[USA]], [[UF_EXT]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = lshr i24 [[SUM]], 8
// CHECK-NEXT: [[RES_TRUNC:%[a-z0-9]+]] = trunc i24 [[RES]] to i16
// CHECK-NEXT: store i16 [[RES_TRUNC]], i16* %usa, align 2
usa = usa + uf;
}
void IntAddition() {
// CHECK-LABEL: IntAddition
short _Accum sa;
_Accum a;
unsigned short _Accum usa;
_Sat short _Accum sa_sat;
int i;
unsigned int ui;
long _Fract lf;
_Bool b;
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %i, align 4
// CHECK-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i39
// CHECK-NEXT: [[I_EXT:%[a-z0-9]+]] = sext i32 [[I]] to i39
// CHECK-NEXT: [[I:%[a-z0-9]+]] = shl i39 [[I_EXT]], 7
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i39 [[SA_EXT]], [[I]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = trunc i39 [[SUM]] to i16
// CHECK-NEXT: store i16 [[RES]], i16* %sa, align 2
sa = sa + i;
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[UI:%[0-9]+]] = load i32, i32* %ui, align 4
// CHECK-NEXT: [[SA_EXT:%[a-z0-9]+]] = sext i16 [[SA]] to i40
// CHECK-NEXT: [[UI_EXT:%[a-z0-9]+]] = zext i32 [[UI]] to i40
// CHECK-NEXT: [[UI:%[a-z0-9]+]] = shl i40 [[UI_EXT]], 7
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i40 [[SA_EXT]], [[UI]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = trunc i40 [[SUM]] to i16
// CHECK-NEXT: store i16 [[RES]], i16* %sa, align 2
sa = sa + ui;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %i, align 4
// SIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i40
// SIGNED-NEXT: [[I_EXT:%[a-z0-9]+]] = sext i32 [[I]] to i40
// SIGNED-NEXT: [[I:%[a-z0-9]+]] = shl i40 [[I_EXT]], 8
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i40 [[USA_EXT]], [[I]]
// SIGNED-NEXT: [[RES:%[a-z0-9]+]] = trunc i40 [[SUM]] to i16
// UNSIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i39
// UNSIGNED-NEXT: [[I_EXT:%[a-z0-9]+]] = sext i32 [[I]] to i39
// UNSIGNED-NEXT: [[I:%[a-z0-9]+]] = shl i39 [[I_EXT]], 7
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i39 [[USA_EXT]], [[I]]
// UNSIGNED-NEXT: [[RES:%[a-z0-9]+]] = trunc i39 [[SUM]] to i16
// CHECK-NEXT: store i16 [[RES]], i16* %usa, align 2
usa = usa + i;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %ui, align 4
// SIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i40
// SIGNED-NEXT: [[I_EXT:%[a-z0-9]+]] = zext i32 [[I]] to i40
// SIGNED-NEXT: [[I:%[a-z0-9]+]] = shl i40 [[I_EXT]], 8
// SIGNED-NEXT: [[SUM:%[0-9]+]] = add i40 [[USA_EXT]], [[I]]
// SIGNED-NEXT: [[RES:%[a-z0-9]+]] = trunc i40 [[SUM]] to i16
// UNSIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i39
// UNSIGNED-NEXT: [[I_EXT:%[a-z0-9]+]] = zext i32 [[I]] to i39
// UNSIGNED-NEXT: [[I:%[a-z0-9]+]] = shl i39 [[I_EXT]], 7
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = add i39 [[USA_EXT]], [[I]]
// UNSIGNED-NEXT: [[RES:%[a-z0-9]+]] = trunc i39 [[SUM]] to i16
// CHECK-NEXT: store i16 [[RES]], i16* %usa, align 2
usa = usa + ui;
// CHECK: [[LF:%[0-9]+]] = load i32, i32* %lf, align 4
// CHECK-NEXT: [[UI:%[0-9]+]] = load i32, i32* %ui, align 4
// CHECK-NEXT: [[LF_EXT:%[a-z0-9]+]] = sext i32 [[LF]] to i64
// CHECK-NEXT: [[UI_EXT:%[a-z0-9]+]] = zext i32 [[UI]] to i64
// CHECK-NEXT: [[UI:%[a-z0-9]+]] = shl i64 [[UI_EXT]], 31
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i64 [[LF_EXT]], [[UI]]
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = trunc i64 [[SUM]] to i32
// CHECK-NEXT: store i32 [[RES]], i32* %lf, align 4
lf = lf + ui;
// CHECK: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[BOOL:%[0-9]+]] = load i8, i8* %b, align 1
// CHECK-NEXT: [[AS_BOOL:%[a-z0-9]+]] = trunc i8 [[BOOL]] to i1
// CHECK-NEXT: [[BOOL_EXT:%[a-z0-9]+]] = zext i1 [[AS_BOOL]] to i32
// CHECK-NEXT: [[ACCUM_EXT:%[a-z0-9]+]] = sext i32 [[ACCUM]] to i47
// CHECK-NEXT: [[BOOL:%[a-z0-9]+]] = sext i32 [[BOOL_EXT]] to i47
// CHECK-NEXT: [[BOOL_EXT:%[a-z0-9]+]] = shl i47 [[BOOL]], 15
// CHECK-NEXT: [[SUM:%[0-9]+]] = add i47 [[ACCUM_EXT]], [[BOOL_EXT]]
// CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = trunc i47 [[SUM]] to i32
// CHECK-NEXT: store i32 [[RESULT]], i32* %a, align 4
a = a + b;
}
void SaturatedAddition() {
// CHECK-LABEL: SaturatedAddition
short _Accum sa;
_Accum a;
long _Accum la;
unsigned short _Accum usa;
unsigned _Accum ua;
unsigned long _Accum ula;
_Sat short _Accum sa_sat;
_Sat _Accum a_sat;
_Sat long _Accum la_sat;
_Sat unsigned short _Accum usa_sat;
_Sat unsigned _Accum ua_sat;
_Sat unsigned long _Accum ula_sat;
_Sat unsigned _Fract uf_sat;
int i;
unsigned int ui;
// CHECK: [[SA:%[0-9]+]] = load i16, i16* %sa, align 2
// CHECK-NEXT: [[SA_SAT:%[0-9]+]] = load i16, i16* %sa_sat, align 2
// CHECK-NEXT: [[SUM:%[0-9]+]] = call i16 @llvm.sadd.sat.i16(i16 [[SA]], i16
// [[SA_SAT]])
// CHECK-NEXT: store i16 [[SUM]], i16* %sa_sat, align 2
sa_sat = sa + sa_sat;
// CHECK: [[USA:%[0-9]+]] = load i16, i16* %usa, align 2
// CHECK-NEXT: [[USA_SAT:%[0-9]+]] = load i16, i16* %usa_sat, align 2
// SIGNED-NEXT: [[SUM:%[0-9]+]] = call i16 @llvm.uadd.sat.i16(i16 [[USA]], i16 [[USA_SAT]])
// SIGNED-NEXT: store i16 [[SUM]], i16* %usa_sat, align 2
// UNSIGNED-NEXT: [[USA_TRUNC:%[a-z0-9]+]] = trunc i16 [[USA]] to i15
// UNSIGNED-NEXT: [[USA_SAT_TRUNC:%[a-z0-9]+]] = trunc i16 [[USA_SAT]] to i15
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = call i15 @llvm.uadd.sat.i15(i15 [[USA_TRUNC]], i15 [[USA_SAT_TRUNC]])
// UNSIGNED-NEXT: [[SUM_EXT:%[a-z0-9]+]] = zext i15 [[SUM]] to i16
// UNSIGNED-NEXT: store i16 [[SUM_EXT]], i16* %usa_sat, align 2
usa_sat = usa + usa_sat;
// CHECK: [[UA:%[0-9]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[USA:%[0-9]+]] = load i16, i16* %usa_sat, align 2
// SIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i32
// SIGNED-NEXT: [[USA:%[a-z0-9]+]] = shl i32 [[USA_EXT]], 8
// SIGNED-NEXT: [[SUM:%[0-9]+]] = call i32 @llvm.uadd.sat.i32(i32 [[UA]], i32 [[USA]])
// SIGNED-NEXT: store i32 [[SUM]], i32* %ua_sat, align 4
// UNSIGNED-NEXT: [[UA_TRUNC:%[a-z0-9]+]] = trunc i32 [[UA]] to i31
// UNSIGNED-NEXT: [[USA_EXT:%[a-z0-9]+]] = zext i16 [[USA]] to i31
// UNSIGNED-NEXT: [[USA:%[a-z0-9]+]] = shl i31 [[USA_EXT]], 8
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = call i31 @llvm.uadd.sat.i31(i31 [[UA_TRUNC]], i31 [[USA]])
// UNSIGNED-NEXT: [[SUM_EXT:%[a-z0-9]+]] = zext i31 [[SUM]] to i32
// UNSIGNED-NEXT: store i32 [[SUM_EXT]], i32* %ua_sat, align 4
ua_sat = ua + usa_sat;
// CHECK: [[SA_SAT:%[0-9]+]] = load i16, i16* %sa_sat, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %i, align 4
// CHECK-NEXT: [[SA_SAT_EXT:%[a-z0-9]+]] = sext i16 [[SA_SAT]] to i39
// CHECK-NEXT: [[I_EXT:%[a-z0-9]+]] = sext i32 [[I]] to i39
// CHECK-NEXT: [[I:%[a-z0-9]+]] = shl i39 [[I_EXT]], 7
// CHECK-NEXT: [[SUM:%[0-9]+]] = call i39 @llvm.sadd.sat.i39(i39 [[SA_SAT_EXT]], i39 [[I]])
// CHECK-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i39 [[SUM]], 32767
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = select i1 [[USE_MAX]], i39 32767, i39 [[SUM]]
// CHECK-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i39 [[RES]], -32768
// CHECK-NEXT: [[RES2:%[a-z0-9]+]] = select i1 [[USE_MIN]], i39 -32768, i39 [[RES]]
// CHECK-NEXT: [[RES3:%[a-z0-9]+]] = trunc i39 [[RES2]] to i16
// CHECK-NEXT: store i16 [[RES3]], i16* %sa_sat, align 2
sa_sat = sa_sat + i;
// CHECK: [[SA_SAT:%[0-9]+]] = load i16, i16* %sa_sat, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %ui, align 4
// CHECK-NEXT: [[SA_SAT_EXT:%[a-z0-9]+]] = sext i16 [[SA_SAT]] to i40
// CHECK-NEXT: [[I_EXT:%[a-z0-9]+]] = zext i32 [[I]] to i40
// CHECK-NEXT: [[I:%[a-z0-9]+]] = shl i40 [[I_EXT]], 7
// CHECK-NEXT: [[SUM:%[0-9]+]] = call i40 @llvm.sadd.sat.i40(i40 [[SA_SAT_EXT]], i40 [[I]])
// CHECK-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i40 [[SUM]], 32767
// CHECK-NEXT: [[RES:%[a-z0-9]+]] = select i1 [[USE_MAX]], i40 32767, i40 [[SUM]]
// CHECK-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i40 [[RES]], -32768
// CHECK-NEXT: [[RES2:%[a-z0-9]+]] = select i1 [[USE_MIN]], i40 -32768, i40 [[RES]]
// CHECK-NEXT: [[RES3:%[a-z0-9]+]] = trunc i40 [[RES2]] to i16
// CHECK-NEXT: store i16 [[RES3]], i16* %sa_sat, align 2
sa_sat = sa_sat + ui;
// CHECK: [[UF_SAT:%[0-9]+]] = load i16, i16* %uf_sat, align 2
// CHECK-NEXT: [[UF_SAT2:%[0-9]+]] = load i16, i16* %uf_sat, align 2
// SIGNED-NEXT: [[SUM:%[0-9]+]] = call i16 @llvm.uadd.sat.i16(i16 [[UF_SAT]], i16 [[UF_SAT2]])
// SIGNED-NEXT: store i16 [[SUM]], i16* %uf_sat, align 2
// UNSIGNED-NEXT: [[UF_SAT_TRUNC:%[a-z0-9]+]] = trunc i16 [[UF_SAT]] to i15
// UNSIGNED-NEXT: [[UF_SAT_TRUNC2:%[a-z0-9]+]] = trunc i16 [[UF_SAT2]] to i15
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = call i15 @llvm.uadd.sat.i15(i15 [[UF_SAT_TRUNC]], i15 [[UF_SAT_TRUNC2]])
// UNSIGNED-NEXT: [[SUM_EXT:%[a-z0-9]+]] = zext i15 [[SUM]] to i16
// UNSIGNED-NEXT: store i16 [[SUM_EXT]], i16* %uf_sat, align 2
uf_sat = uf_sat + uf_sat;
// CHECK: [[USA_SAT:%[0-9]+]] = load i16, i16* %usa_sat, align 2
// CHECK-NEXT: [[I:%[0-9]+]] = load i32, i32* %i, align 4
// SIGNED-NEXT: [[USA_SAT_RESIZE:%[a-z0-9]+]] = zext i16 [[USA_SAT]] to i40
// SIGNED-NEXT: [[I_RESIZE:%[a-z0-9]+]] = sext i32 [[I]] to i40
// SIGNED-NEXT: [[I_UPSCALE:%[a-z0-9]+]] = shl i40 [[I_RESIZE]], 8
// SIGNED-NEXT: [[SUM:%[0-9]+]] = call i40 @llvm.uadd.sat.i40(i40 [[USA_SAT_RESIZE]], i40 [[I_UPSCALE]])
// SIGNED-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i40 [[SUM]], 65535
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = select i1 [[USE_MAX]], i40 65535, i40 [[SUM]]
// SIGNED-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i40 [[RESULT]], 0
// SIGNED-NEXT: [[RESULT2:%[a-z0-9]+]] = select i1 [[USE_MIN]], i40 0, i40 [[RESULT]]
// SIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = trunc i40 [[RESULT2]] to i16
// UNSIGNED-NEXT: [[USA_SAT_RESIZE:%[a-z0-9]+]] = zext i16 [[USA_SAT]] to i39
// UNSIGNED-NEXT: [[I_RESIZE:%[a-z0-9]+]] = sext i32 [[I]] to i39
// UNSIGNED-NEXT: [[I_UPSCALE:%[a-z0-9]+]] = shl i39 [[I_RESIZE]], 7
// UNSIGNED-NEXT: [[SUM:%[0-9]+]] = call i39 @llvm.uadd.sat.i39(i39 [[USA_SAT_RESIZE]], i39 [[I_UPSCALE]])
// UNSIGNED-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i39 [[SUM]], 32767
// UNSIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = select i1 [[USE_MAX]], i39 32767, i39 [[SUM]]
// UNSIGNED-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i39 [[RESULT]], 0
// UNSIGNED-NEXT: [[RESULT2:%[a-z0-9]+]] = select i1 [[USE_MIN]], i39 0, i39 [[RESULT]]
// UNSIGNED-NEXT: [[RESULT:%[a-z0-9]+]] = trunc i39 [[RESULT2]] to i16
// CHECK-NEXT: store i16 [[RESULT]], i16* %usa_sat, align 2
usa_sat = usa_sat + i;
}