forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPInt-C.cpp
490 lines (437 loc) · 13.4 KB
/
APInt-C.cpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// This file is a part of Julia. License is MIT: http://julialang.org/license
#include "llvm-version.h"
#include <llvm/ADT/APInt.h>
#include <llvm/ADT/APFloat.h>
#include <llvm/Support/MathExtras.h>
extern "C" {
#include "APInt-C.h"
DLLEXPORT void jl_error(const char *str);
}
using namespace llvm;
/* create "APInt s" from "integerPart *ps" */
#define CREATE(s) \
APInt s; \
if ((numbits % integerPartWidth) != 0) { \
/* use LLT_ALIGN to round the memory area up to the nearest integerPart-sized chunk */ \
unsigned nbytes = RoundUpToAlignment(numbits, integerPartWidth) / host_char_bit; \
integerPart *data_a64 = (integerPart*)alloca(nbytes); \
/* TODO: this memcpy assumes little-endian,
* for big-endian, need to align the copy to the other end */ \
memcpy(data_a64, p##s, RoundUpToAlignment(numbits, host_char_bit) / host_char_bit); \
s = APInt(numbits, makeArrayRef(data_a64, nbytes / sizeof(integerPart))); \
} \
else { \
s = APInt(numbits, makeArrayRef(p##s, numbits / integerPartWidth)); \
}
/* assign to "integerPart *pr" from "APInt a" */
#define ASSIGN(r, a) \
if (numbits <= 8) \
*(uint8_t*)p##r = a.getZExtValue(); \
else if (numbits <= 16) \
*(uint16_t*)p##r = a.getZExtValue(); \
else if (numbits <= 32) \
*(uint32_t*)p##r = a.getZExtValue(); \
else if (numbits <= 64) \
*(uint64_t*)p##r = a.getZExtValue(); \
else \
memcpy(p##r, a.getRawData(), RoundUpToAlignment(numbits, host_char_bit) / host_char_bit); \
extern "C" DLLEXPORT
void LLVMNeg(unsigned numbits, integerPart *pa, integerPart *pr) {
APInt z(numbits, 0);
CREATE(a)
z -= a;
ASSIGN(r, z)
}
extern "C" DLLEXPORT
void LLVMAdd(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a += b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMSub(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a -= b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMMul(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a *= b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMSDiv(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.sdiv(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMUDiv(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.udiv(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMSRem(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.srem(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMURem(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.urem(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
int LLVMICmpEQ(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.eq(b);
}
extern "C" DLLEXPORT
int LLVMICmpNE(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.ne(b);
}
extern "C" DLLEXPORT
int LLVMICmpSLT(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.slt(b);
}
extern "C" DLLEXPORT
int LLVMICmpULT(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.ult(b);
}
extern "C" DLLEXPORT
int LLVMICmpSLE(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.sle(b);
}
extern "C" DLLEXPORT
int LLVMICmpULE(unsigned numbits, integerPart *pa, integerPart *pb) {
CREATE(a)
CREATE(b)
return a.ule(b);
}
extern "C" DLLEXPORT
void LLVMAnd(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a &= b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMOr(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a |= b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMXor(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a ^= b;
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMShl(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.shl(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMLShr(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.lshr(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMAShr(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
a = a.ashr(b);
ASSIGN(r, a)
}
extern "C" DLLEXPORT
void LLVMFlipAllBits(unsigned numbits, integerPart *pa, integerPart *pr) {
CREATE(a)
a.flipAllBits();
ASSIGN(r, a)
}
extern "C" DLLEXPORT
int LLVMAdd_uov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.uadd_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
int LLVMAdd_sov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.sadd_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
int LLVMSub_uov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.usub_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
int LLVMSub_sov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.ssub_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
int LLVMMul_sov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.smul_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
int LLVMMul_uov(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
bool Overflow;
a = a.umul_ov(b, Overflow);
ASSIGN(r, a)
return Overflow;
}
extern "C" DLLEXPORT
void LLVMByteSwap(unsigned numbits, integerPart *pa, integerPart *pr) {
CREATE(a)
a = a.byteSwap();
ASSIGN(r, a)
}
void LLVMFPtoInt(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr, bool isSigned, bool *isExact) {
double Val;
if (numbits == 32)
Val = *(float*)pa;
else if (numbits == 64)
Val = *(double*)pa;
else
jl_error("FPtoSI: runtime floating point intrinsics are not implemented for bit sizes other than 32 and 64");
unsigned onumbytes = RoundUpToAlignment(onumbits, host_char_bit) / host_char_bit;
if (onumbits <= 64) { // fast-path, if possible
if (isSigned) {
int64_t ia = Val;
memcpy(pr, &ia, onumbytes); // TODO: assumes little-endian
if (isExact) {
// check whether the conversion was lossless
int64_t ia2 = ia < 0 ? -1 : 0;
memcpy(&ia2, pr, onumbytes);
*isExact = (Val == (double)ia2 && ia == ia2);
}
}
else {
uint64_t ia = Val;
memcpy(pr, &ia, onumbytes); // TODO: assumes little-endian
if (isExact) {
// check whether the conversion was lossless
uint64_t ia2 = 0;
memcpy(&ia2, pr, onumbytes);
*isExact = (Val == (double)ia2 && ia == ia2);
}
}
}
else {
APFloat a(Val);
bool isVeryExact;
APFloat::roundingMode rounding_mode = APFloat::rmNearestTiesToEven;
unsigned nbytes = RoundUpToAlignment(onumbits, integerPartWidth) / host_char_bit;
integerPart *parts = (integerPart*)alloca(nbytes);
APFloat::opStatus status = a.convertToInteger(parts, onumbits, isSigned, rounding_mode, &isVeryExact);
memcpy(pr, parts, onumbytes);
if (isExact)
*isExact = (status == APFloat::opOK);
}
}
extern "C" DLLEXPORT
void LLVMFPtoSI(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
LLVMFPtoInt(numbits, pa, onumbits, pr, true, NULL);
}
extern "C" DLLEXPORT
void LLVMFPtoUI(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
LLVMFPtoInt(numbits, pa, onumbits, pr, false, NULL);
}
extern "C" DLLEXPORT
int LLVMFPtoSI_exact(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
bool isExact;
LLVMFPtoInt(numbits, pa, onumbits, pr, true, &isExact);
return isExact;
}
extern "C" DLLEXPORT
int LLVMFPtoUI_exact(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
bool isExact;
LLVMFPtoInt(numbits, pa, onumbits, pr, false, &isExact);
return isExact;
}
extern "C" DLLEXPORT
void LLVMSItoFP(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
CREATE(a)
double val = a.roundToDouble(true);
if (onumbits == 32)
*(float*)pr = val;
else if (onumbits == 64)
*(double*)pr = val;
else
jl_error("SItoFP: runtime floating point intrinsics are not implemented for bit sizes other than 32 and 64");
}
extern "C" DLLEXPORT
void LLVMUItoFP(unsigned numbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
CREATE(a)
double val = a.roundToDouble(false);
if (onumbits == 32)
*(float*)pr = val;
else if (onumbits == 64)
*(double*)pr = val;
else
jl_error("UItoFP: runtime floating point intrinsics are not implemented for bit sizes other than 32 and 64");
}
extern "C" DLLEXPORT
void LLVMSExt(unsigned inumbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
assert(inumbits < onumbits);
unsigned inumbytes = RoundUpToAlignment(inumbits, host_char_bit) / host_char_bit;
unsigned onumbytes = RoundUpToAlignment(onumbits, host_char_bit) / host_char_bit;
int bits = (0 - inumbits) % host_char_bit;
int signbit = (inumbits - 1) % host_char_bit;
int sign = ((unsigned char*)pa)[inumbytes - 1] & (1 << signbit) ? -1 : 0;
// copy over the input bytes
memcpy(pr, pa, inumbytes);
if (bits) {
// sign-extend the partial byte
((signed char*)pr)[inumbytes - 1] = ((signed char*)pa)[inumbytes - 1] << bits >> bits;
}
// sign-extend the rest of the bytes
memset((char*)pr + inumbytes, sign, onumbytes - inumbytes);
}
extern "C" DLLEXPORT
void LLVMZExt(unsigned inumbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
assert(inumbits < onumbits);
unsigned inumbytes = RoundUpToAlignment(inumbits, host_char_bit) / host_char_bit;
unsigned onumbytes = RoundUpToAlignment(onumbits, host_char_bit) / host_char_bit;
int bits = (0 - inumbits) % host_char_bit;
// copy over the input bytes
memcpy(pr, pa, inumbytes);
if (bits) {
// zero the remaining bits of the partial byte
((unsigned char*)pr)[inumbytes - 1] = ((unsigned char*)pa)[inumbytes - 1] << bits >> bits;
}
// zero-extend the rest of the bytes
memset((char*)pr + inumbytes, 0, onumbytes - inumbytes);
}
extern "C" DLLEXPORT
void LLVMTrunc(unsigned inumbits, integerPart *pa, unsigned onumbits, integerPart *pr) {
assert(inumbits > onumbits);
unsigned onumbytes = RoundUpToAlignment(onumbits, host_char_bit) / host_char_bit;
memcpy(pr, pa, onumbytes);
}
extern "C" DLLEXPORT
unsigned countTrailingZeros_8(uint8_t Val) {
#ifdef LLVM35
return countTrailingZeros(Val);
#else
return CountTrailingZeros_32(Val);
#endif
}
extern "C" DLLEXPORT
unsigned countTrailingZeros_16(uint16_t Val) {
#ifdef LLVM35
return countTrailingZeros(Val);
#else
return CountTrailingZeros_32(Val);
#endif
}
extern "C" DLLEXPORT
unsigned countTrailingZeros_32(uint32_t Val) {
#ifdef LLVM35
return countTrailingZeros(Val);
#else
return CountTrailingZeros_32(Val);
#endif
}
extern "C" DLLEXPORT
unsigned countTrailingZeros_64(uint64_t Val) {
#ifdef LLVM35
return countTrailingZeros(Val);
#else
return CountTrailingZeros_64(Val);
#endif
}
extern "C" DLLEXPORT
void jl_LLVMSMod(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
CREATE(a)
CREATE(b)
APInt r = a.srem(b);
if (a.isNegative() != b.isNegative()) {
r = (b + r).srem(b);
}
ASSIGN(r, r)
}
extern "C" DLLEXPORT
void jl_LLVMFlipSign(unsigned numbits, integerPart *pa, integerPart *pb, integerPart *pr) {
unsigned numbytes = RoundUpToAlignment(numbits, host_char_bit) / host_char_bit;
int signbit = (numbits - 1) % host_char_bit;
int sign = ((unsigned char*)pa)[numbytes - 1] & (1 << signbit) ? -1 : 0;
if (sign)
LLVMNeg(numbits, pa, pr);
else
memcpy(pr, pa, numbytes);
}
extern "C" DLLEXPORT
unsigned LLVMCountPopulation(unsigned numbits, integerPart *pa) {
CREATE(a)
return a.countPopulation();
}
extern "C" DLLEXPORT
unsigned LLVMCountTrailingOnes(unsigned numbits, integerPart *pa) {
CREATE(a)
return a.countTrailingOnes();
}
extern "C" DLLEXPORT
unsigned LLVMCountTrailingZeros(unsigned numbits, integerPart *pa) {
CREATE(a)
return a.countTrailingZeros();
}
extern "C" DLLEXPORT
unsigned LLVMCountLeadingOnes(unsigned numbits, integerPart *pa) {
CREATE(a)
return a.countLeadingOnes();
}
extern "C" DLLEXPORT
unsigned LLVMCountLeadingZeros(unsigned numbits, integerPart *pa) {
CREATE(a)
return a.countLeadingZeros();
}