comparison tests/mini/intrinsics_ovf.d @ 1359:34f2fd925de3

Intrinsics shouldn't see struct padding, so use a special TargetABI for them that removes it. This unbreaks the `llvm_*_with_overflow` intrinsics.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 16 May 2009 13:06:49 +0200
parents
children
comparison
equal deleted inserted replaced
1357:48747003a5de 1359:34f2fd925de3
1 module intrinsics_ovf;
2
3
4 //version = PRINTF;
5
6 version(PRINTF)
7 extern(C) int printf(char*, ...);
8
9
10 import ldc.intrinsics;
11
12
13 int saddo(int a, int b, out bool overflow) {
14 auto Result = llvm_sadd_with_overflow(a, b);
15 overflow = Result.overflow;
16 return Result.result;
17 }
18
19 int uaddo(int a, int b, out bool overflow) {
20 auto Result = llvm_uadd_with_overflow(a, b);
21 overflow = Result.overflow;
22 return Result.result;
23 }
24
25 int smulo(int a, int b, out bool overflow) {
26 auto Result = llvm_smul_with_overflow(a, b);
27 overflow = Result.overflow;
28 return Result.result;
29 }
30
31 /*
32 uint umulo(uint a, uint b, out bool overflow) {
33 auto Result = llvm_umul_with_overflow(a, b);
34 overflow = Result.overflow;
35 return Result.result;
36 }
37 */
38
39 void test(int function(int, int, out bool) fn,
40 int a, int b, int result_e, bool ovf_e) {
41 version(PRINTF)
42 printf("%8x :: %8x :: %8x :: %.*s\n", a, b, result_e,
43 (ovf_e ? "true" : "false"));
44
45 bool ovf;
46 int result = fn(a, b, ovf);
47
48 version(PRINTF)
49 printf("____________________ %8x :: %.*s\n", result,
50 (ovf ? "true" : "false"));
51
52 assert(ovf == ovf_e);
53 assert(result == result_e);
54 }
55
56 void main() {
57 test(&saddo, int.min, int.min, int.min + int.min, true);
58 test(&saddo, int.min, int.max, int.min + int.max, false);
59 test(&saddo, 1, int.max, 1 + int.max, true);
60 test(&saddo, 1, 2, 3, false);
61 test(&saddo, -1, -2, -3, false);
62
63 test(&uaddo, 0, uint.max, 0 + uint.max, false);
64 test(&uaddo, 1, uint.max, 1 + uint.max, true);
65 test(&uaddo, 1, 2, 3, false);
66
67 test(&smulo, int.min, int.min, int.min * int.min, true);
68 test(&smulo, int.min, int.max, int.min * int.max, true);
69 test(&smulo, int.max, int.max, int.max * int.max, true);
70 test(&smulo, 1, int.max, 1 * int.max, false);
71 test(&smulo, 2, int.max/2, 2 * (int.max/2), false);
72 test(&smulo, 2, int.max/2 + 1, 2 * (int.max/2 + 1), true);
73 test(&smulo, 2, int.min/2, 2 * (int.min/2), false);
74 test(&smulo, 2, int.min/2 - 1, 2 * (int.min/2 - 1), true);
75 test(&smulo, 1, 2, 2, false);
76 test(&smulo, -1, -2, 2, false);
77 }