2 REQUIRED_ARGS: -mcpu=native
3 PERMUTE_ARGS: -O -inline -release
6 import core
.stdc
.stdio
;
8 template tuple(A
...) { alias tuple
= A
; }
10 ///////////////////////
12 // https://github.com/dlang/dmd/pull/11441
26 assert(sdiv1(10) == 5);
27 assert(sdiv1(-10) == -5);
28 assert(sdiv2(10) == 5);
29 assert(sdiv2(-10) == -5);
32 ///////////////////////
36 __gshared
ulong[4][] vectors
=
42 [10_000_000_000L, 11_000_000_000L, 0, 10_000_000_000L],
43 [11_000_000_000L, 10_000_000_000L, 1, 1_000_000_000L],
44 [11_000_000_000L, 11_000_000_000L, 1, 0],
45 [10_000_000_000L, 10, 1_000_000_000L, 0],
46 [0x8000_0000_0000_0000, 0x8000_0000_0000_0000, 1, 0],
47 [0x8000_0000_0000_0001, 0x8000_0000_0000_0001, 1, 0],
48 [0x8000_0001_0000_0000, 0x8000_0001_0000_0000, 1, 0],
49 [0x8000_0001_0000_0000, 0x8000_0000_0000_0000, 1, 0x1_0000_0000],
50 [0x8000_0001_0000_0000, 0x8000_0000_8000_0000, 1, 0x8000_0000],
51 [0x8000_0000_0000_0000, 0x7FFF_FFFF_FFFF_FFFF
, 1, 1],
52 [0x8000_0000_0000_0000, 0x8000_0000_0000_0001, 0, 0x8000_0000_0000_0000],
53 [0x8000_0000_0000_0000, 0x8000_0001_0000_0000, 0, 0x8000_0000_0000_0000],
56 for (size_t i
= 0; i
< vectors
.length
; i
++)
58 ulong q
= vectors
[i
][0] / vectors
[i
][1];
59 if (q
!= vectors
[i
][2])
60 printf("[%zd] %lld / %lld = %lld, should be %lld\n",
61 i
, vectors
[i
][0], vectors
[i
][1], q
, vectors
[i
][2]);
63 ulong r
= vectors
[i
][0] % vectors
[i
][1];
64 if (r
!= vectors
[i
][3])
65 printf("[%zd] %lld %% %lld = %lld, should be %lld\n",
66 i
, vectors
[i
][0], vectors
[i
][1], r
, vectors
[i
][3]);
70 ////////////////////////////////////////////////////////////////////////
82 uint udiv14007(uint x
)
97 uint umod14007(uint x
)
102 uint uremquo10(uint x
)
104 return (x
/ 10) |
(x
% 10);
107 uint uremquo14(uint x
)
109 return (x
/ 14) |
(x
% 14);
112 uint uremquo14007(uint x
)
114 return (x
/ 14007) |
(x
% 14007);
119 ulong uldiv10(ulong x
)
124 ulong uldiv14(ulong x
)
129 ulong uldiv14007(ulong x
)
134 ulong ulmod10(ulong x
)
139 ulong ulmod14(ulong x
)
144 ulong ulmod14007(ulong x
)
149 ulong ulremquo10(ulong x
)
151 return (x
/ 10) |
(x
% 10);
154 ulong ulremquo14(ulong x
)
156 return (x
/ 14) |
(x
% 14);
159 ulong ulremquo14007(ulong x
)
161 return (x
/ 14007) |
(x
% 14007);
168 static uint x10
= 10;
169 static uint x14
= 14;
170 static uint x14007
= 14007;
174 r
= udiv10(u
); assert(r
== u
/x10
);
175 r
= udiv14(u
); assert(r
== u
/x14
);
176 r
= udiv14007(u
); assert(r
== u
/x14007
);
177 r
= umod10(u
); assert(r
== u
%x10
);
178 r
= umod14(u
); assert(r
== u
%x14
);
179 r
= umod14007(u
); assert(r
== u
%x14007
);
180 r
= uremquo10(u
); assert(r
== ((u
/10)|
(u
%x10
)));
181 r
= uremquo14(u
); assert(r
== ((u
/14)|
(u
%x14
)));
182 r
= uremquo14007(u
); assert(r
== ((u
/14007)|
(u
%x14007
)));
185 static ulong y10
= 10;
186 static ulong y14
= 14;
187 static ulong y14007
= 14007;
191 r
= uldiv10(u
); assert(r
== u
/y10
);
192 r
= uldiv14(u
); assert(r
== u
/y14
);
193 r
= uldiv14007(u
); assert(r
== u
/y14007
);
194 r
= ulmod10(u
); assert(r
== u
%y10
);
195 r
= ulmod14(u
); assert(r
== u
%y14
);
196 r
= ulmod14007(u
); assert(r
== u
%y14007
);
197 r
= ulremquo10(u
); assert(r
== ((u
/10)|
(u
%y10
)));
198 r
= ulremquo14(u
); assert(r
== ((u
/14)|
(u
%y14
)));
199 r
= ulremquo14007(u
); assert(r
== ((u
/14007)|
(u
%y14007
)));
204 ////////////////////////////////////////////////////////////////////////
206 // https://issues.dlang.org/show_bug.cgi?id=14936
208 long sldiv1 (long x
) { return x
/ (1L << 1); }
209 long sldiv2 (long x
) { return x
/ (1L << 2); }
210 long sldiv3 (long x
) { return x
/ (1L << 3); }
211 long sldiv7 (long x
) { return x
/ (1L << 7); }
212 long sldiv8 (long x
) { return x
/ (1L << 8); }
213 long sldiv9 (long x
) { return x
/ (1L << 9); }
214 long sldiv30(long x
) { return x
/ (1L << 30); }
215 long sldiv31(long x
) { return x
/ (1L << 31); }
216 long sldiv32(long x
) { return x
/ (1L << 32); }
217 long sldiv33(long x
) { return x
/ (1L << 33); }
218 long sldiv34(long x
) { return x
/ (1L << 34); }
219 long sldiv62(long x
) { return x
/ (1L << 62); }
220 long sldiv63(long x
) { return x
/ (1L << 63); }
224 /* Test special div code for signed long divide
225 * by power of 2 for 32 bit targets.
228 // printf("63 = %llx\n", sldiv63(-0x7FFF_F8FF_FF3F_2FFFL));
230 static foreach (C
; tuple
!(
233 0x7FFF_F
8FF_FF
3F
_2FFFL
))
235 /* Check if runtime computation matches compile time
237 assert(sldiv1 ( C
) == C
/ (1L << 1));
238 assert(sldiv1 (-C
) == -C
/ (1L << 1));
239 assert(sldiv2 ( C
) == C
/ (1L << 2));
240 assert(sldiv2 (-C
) == -C
/ (1L << 2));
241 assert(sldiv3 ( C
) == C
/ (1L << 3));
242 assert(sldiv3 (-C
) == -C
/ (1L << 3));
243 assert(sldiv7 ( C
) == C
/ (1L << 7));
244 assert(sldiv7 (-C
) == -C
/ (1L << 7));
245 assert(sldiv8 ( C
) == C
/ (1L << 8));
246 assert(sldiv8 (-C
) == -C
/ (1L << 8));
247 assert(sldiv9 ( C
) == C
/ (1L << 9));
248 assert(sldiv9 (-C
) == -C
/ (1L << 9));
250 assert(sldiv30( C
) == C
/ (1L << 30));
251 assert(sldiv30(-C
) == -C
/ (1L << 30));
252 assert(sldiv31( C
) == C
/ (1L << 31));
253 assert(sldiv31(-C
) == -C
/ (1L << 31));
254 assert(sldiv32( C
) == C
/ (1L << 32));
255 assert(sldiv32(-C
) == -C
/ (1L << 32));
256 assert(sldiv33( C
) == C
/ (1L << 33));
257 assert(sldiv33(-C
) == -C
/ (1L << 33));
258 assert(sldiv34( C
) == C
/ (1L << 34));
259 assert(sldiv34(-C
) == -C
/ (1L << 34));
260 assert(sldiv62( C
) == C
/ (1L << 62));
261 assert(sldiv62(-C
) == -C
/ (1L << 62));
262 assert(sldiv63( C
) == C
/ (1L << 63));
263 assert(sldiv63(-C
) == -C
/ (1L << 63));
267 ////////////////////////////////////////////////////////////////////////
269 // https://issues.dlang.org/show_bug.cgi?id=14936
271 long slmod1 (long x
) { return x
% (1L << 1); }
272 long slmod2 (long x
) { return x
% (1L << 2); }
273 long slmod3 (long x
) { return x
% (1L << 3); }
274 long slmod7 (long x
) { return x
% (1L << 7); }
275 long slmod8 (long x
) { return x
% (1L << 8); }
276 long slmod9 (long x
) { return x
% (1L << 9); }
277 long slmod30(long x
) { return x
% (1L << 30); }
278 long slmod31(long x
) { return x
% (1L << 31); }
279 long slmod32(long x
) { return x
% (1L << 32); }
280 long slmod33(long x
) { return x
% (1L << 33); }
281 long slmod34(long x
) { return x
% (1L << 34); }
282 long slmod62(long x
) { return x
% (1L << 62); }
283 long slmod63(long x
) { return x
% (1L << 63); }
287 static foreach (C
; tuple
!(
290 0x7FFF_F
8FF_FF
3F
_2FFFL
))
292 /* Check if runtime computation matches compile time
294 assert(slmod1 ( C
) == C
% (1L << 1));
295 assert(slmod1 (-C
) == -C
% (1L << 1));
296 assert(slmod2 ( C
) == C
% (1L << 2));
297 assert(slmod2 (-C
) == -C
% (1L << 2));
298 assert(slmod3 ( C
) == C
% (1L << 3));
299 assert(slmod3 (-C
) == -C
% (1L << 3));
300 assert(slmod7 ( C
) == C
% (1L << 7));
301 assert(slmod7 (-C
) == -C
% (1L << 7));
302 assert(slmod8 ( C
) == C
% (1L << 8));
303 assert(slmod8 (-C
) == -C
% (1L << 8));
304 assert(slmod9 ( C
) == C
% (1L << 9));
305 assert(slmod9 (-C
) == -C
% (1L << 9));
307 assert(slmod30( C
) == C
% (1L << 30));
308 assert(slmod30(-C
) == -C
% (1L << 30));
309 assert(slmod31( C
) == C
% (1L << 31));
310 assert(slmod31(-C
) == -C
% (1L << 31));
311 assert(slmod32( C
) == C
% (1L << 32));
312 assert(slmod32(-C
) == -C
% (1L << 32));
313 assert(slmod33( C
) == C
% (1L << 33));
314 assert(slmod33(-C
) == -C
% (1L << 33));
315 assert(slmod34( C
) == C
% (1L << 34));
316 assert(slmod34(-C
) == -C
% (1L << 34));
317 assert(slmod62( C
) == C
% (1L << 62));
318 assert(slmod62(-C
) == -C
% (1L << 62));
319 assert(slmod63( C
) == C
% (1L << 63));
320 assert(slmod63(-C
) == -C
% (1L << 63));
324 ////////////////////////////////////////////////////////////////////////
326 T
divC(int C
, T
)(T x
)
337 T
modC(int C
, T
)(T x
)
348 T
remquoC(int C
, T
)(T x
)
350 return (x
/ C
) |
(x
% C
);
355 static int z
= 0; // prevent constant folding by optimizer
357 static foreach (T
; tuple
!(int, long, uint, ulong))
361 static foreach (C
; tuple
!(10, 14, 14007, -10, -14, -14007))
363 r
= divC
!C(u
); assert(r
== u
/ (z
+ C
));
364 r
= modC
!C(u
); assert(r
== u
% (z
+ C
));
365 r
= remquoC
!C(u
); assert(r
== ((u
/ (z
+ C
) |
(u
% (z
+ C
)))));
370 ////////////////////////////////////////////////////////////////////////
374 * replace ((i / C1) / C2) with (i / (C1 * C2))
375 * when e1 is 0 or 1 and (i2-i1) is a power of 2.
378 void divdiv(T
, T C1
, T C2
)(T i
)
380 auto a
= (i
/ C1
) / C2
;
381 auto b
= i
/ (C1
* C2
);
382 if (a
!= b
) assert(0);
387 divdiv
!(int,10,20)(30);
388 divdiv
!(uint,10,20)(30);
389 divdiv
!(long,10,20)(30);
390 divdiv
!(ulong,10,20)(30);
392 divdiv
!(int,-10,20)(30);
393 divdiv
!(long,-10,20)(30);
395 divdiv
!(int,-10,-20)(-30);
396 divdiv
!(long,-10,-20)(-30);
399 ////////////////////////////////////////////////////////////////////////
403 // https://github.com/dlang/dmd/pull/7128
404 static bool foo(uint a
, uint b
)
406 return cast(bool)(a
/ b
); // convert / to >=
414 /////////////////////////////////////////////////////
453 printf("x == %d\n", x
);
462 for (i
= 0; i
< 10; i
++)
464 for (j
= 0; j
< 10; j
++)
471 printf("i = %d, j = %d\n", i
, j
);
476 ///////////////////////
502 void teststringswitch()
506 printf("i = %d\n", i
);
510 printf("i = %d\n", i
);
514 printf("i = %d\n", i
);
518 printf("i = %d\n", i
);
523 ///////////////////////
555 ///////////////////////
557 align (1) struct Foo1
588 printf("%zd\n", Foo1
.sizeof
);
589 assert(Foo1
.a
.offsetof
== 0);
590 assert(Foo1
.b
.offsetof
== 4);
591 assert(Foo1
.c
.offsetof
== 5);
592 assert(Foo1
.sizeof
== 13);
594 assert(Foo2
.a
.offsetof
== 0);
595 assert(Foo2
.b
.offsetof
== 4);
596 assert(Foo2
.c
.offsetof
== 8);
597 assert(Foo2
.sizeof
== 16);
599 assert(Foo3
.a
.offsetof
== 0);
600 assert(Foo3
.b
.offsetof
== 4);
601 assert(Foo3
.c
.offsetof
== 8);
602 assert(Foo3
.b
.sizeof
== 1);
603 assert(Foo3
.sizeof
== 16);
605 assert(Foo4
.sizeof
== 16);
608 ///////////////////////
610 size_t
cond11565(size_t val
)
612 return val ? size_t
.max
: 0;
617 assert(cond11565(true) == size_t
.max
);
620 ///////////////////////
622 int[3] array1
= [1:1,2,0:3];
626 assert(array1
[0] == 3);
627 assert(array1
[1] == 1);
628 assert(array1
[2] == 2);
631 ///////////////////////
633 void test13023(ulong n
)
635 static void func(bool b
) {}
645 ///////////////////////
647 struct U
{ int a
; union { char c
; int d
; } long b
; }
649 U f
= { b
:3, d
:0x22222222, a
:1 };
654 assert(f
.d
== 0x22222222);
657 assert(f
.sizeof
== 16);
658 assert(U
.sizeof
== 16);
662 ////////////////////////////////////////////////////////////////////////
666 void test12095(int k
)
669 e ? k ||
assert(0) : !e ||
vfunc();
670 e ? k ||
assert(0) : e
&& vfunc();
671 !e ?
!e ||
vfunc() : k ||
assert(0);
675 ////////////////////////////////////////////////////////////////////////
678 bool test3918a( float t
, real u
)
684 bool test3918b( real t
, float u
)
692 assert(test3918a(float.nan
, real.nan
));
693 assert(test3918b(real.nan
, float.nan
));
696 ////////////////////////////////////////////////////////////////////////
698 T
docond1(T
)(T l
, ubyte thresh
, ubyte val
) {
703 T
docond2(T
)(T l
, ubyte thresh
, ubyte val
) {
704 l
-= (thresh
>= val
);
708 T
docond3(T
)(T l
, ubyte thresh
, ubyte val
) {
709 l
+= (thresh
>= val
);
713 T
docond4(T
)(T l
, ubyte thresh
, ubyte val
) {
720 assert(docond1
!ubyte(10,3,5) == 11);
721 assert(docond1
!ushort(10,3,5) == 11);
722 assert(docond1
!uint(10,3,5) == 11);
723 assert(docond1
!ulong(10,3,5) == 11);
725 assert(docond2
!ubyte(10,3,5) == 10);
726 assert(docond2
!ushort(10,3,5) == 10);
727 assert(docond2
!uint(10,3,5) == 10);
728 assert(docond2
!ulong(10,3,5) == 10);
730 assert(docond3
!ubyte(10,3,5) == 10);
731 assert(docond3
!ushort(10,3,5) == 10);
732 assert(docond3
!uint(10,3,5) == 10);
733 assert(docond3
!ulong(10,3,5) == 10);
735 assert(docond4
!ubyte(10,3,5) == 9);
736 assert(docond4
!ushort(10,3,5) == 9);
737 assert(docond4
!uint(10,3,5) == 9);
738 assert(docond4
!ulong(10,3,5) == 9);
741 assert(docond1
!ubyte(10,5,3) == 10);
742 assert(docond1
!ushort(10,5,3) == 10);
743 assert(docond1
!uint(10,5,3) == 10);
744 assert(docond1
!ulong(10,5,3) == 10);
746 assert(docond2
!ubyte(10,5,3) == 9);
747 assert(docond2
!ushort(10,5,3) == 9);
748 assert(docond2
!uint(10,5,3) == 9);
749 assert(docond2
!ulong(10,5,3) == 9);
751 assert(docond3
!ubyte(10,5,3) == 11);
752 assert(docond3
!ushort(10,5,3) == 11);
753 assert(docond3
!uint(10,5,3) == 11);
754 assert(docond3
!ulong(10,5,3) == 11);
756 assert(docond4
!ubyte(10,5,3) == 10);
757 assert(docond4
!ushort(10,5,3) == 10);
758 assert(docond4
!uint(10,5,3) == 10);
759 assert(docond4
!ulong(10,5,3) == 10);
762 ////////////////////////////////////////////////////////////////////////
769 void foo8658(S8658 s
)
777 for(int i
= 0; i
< 1000; i
++)
781 ////////////////////////////////////////////////////////////////////////
805 assert(neg(3) == -3);
806 assert(com(3) == -4);
807 assert(com(3.0f) == -4.0f);
808 assert(com2(3) == -4);
811 ////////////////////////////////////////////////////////////////////////
815 return ((((((((((cast(int) c
<= 32 ||
cast(int) c
== 46) ||
cast(int) c
== 44)
816 ||
cast(int) c
== 58) ||
cast(int) c
== 59) ||
cast(int) c
== 60)
817 ||
cast(int) c
== 62) ||
cast(int) c
== 34) ||
cast(int) c
== 92)
818 ||
cast(int) c
== 39) != 0);
823 return ((((((((((c
<= 32 || c
== 46) || c
== 44)
824 || c
== 58) || c
== 59) || c
== 60)
825 || c
== 62) || c
== 34) || c
== 92)
831 assert(oror1(0) == 1);
832 assert(oror1(32) == 1);
833 assert(oror1(46) == 1);
834 assert(oror1(44) == 1);
835 assert(oror1(58) == 1);
836 assert(oror1(59) == 1);
837 assert(oror1(60) == 1);
838 assert(oror1(62) == 1);
839 assert(oror1(34) == 1);
840 assert(oror1(92) == 1);
841 assert(oror1(39) == 1);
842 assert(oror1(33) == 0);
843 assert(oror1(61) == 0);
844 assert(oror1(93) == 0);
845 assert(oror1(255) == 0);
847 assert(oror2(0) == 1);
848 assert(oror2(32) == 1);
849 assert(oror2(46) == 1);
850 assert(oror2(44) == 1);
851 assert(oror2(58) == 1);
852 assert(oror2(59) == 1);
853 assert(oror2(60) == 1);
854 assert(oror2(62) == 1);
855 assert(oror2(34) == 1);
856 assert(oror2(92) == 1);
857 assert(oror2(39) == 1);
858 assert(oror2(33) == 0);
859 assert(oror2(61) == 0);
860 assert(oror2(93) == 0);
861 assert(oror2(255) == 0);
864 ////////////////////////////////////////////////////////////////////////
866 bool bt1(int p
, int a
, int b
)
868 return p
&& ((1 << b
) & a
);
871 bool bt2(int p
, long a
, long b
)
873 return p
&& ((1L << b
) & a
);
878 assert(bt1(1,7,2) == 1);
879 assert(bt1(1,7,3) == 0);
881 assert(bt2(1,0x7_0000_0000,2+32) == 1);
882 assert(bt2(1,0x7_0000_0000,3+32) == 0);
885 ////////////////////////////////////////////////////////////////////////
898 ////////////////////////////////////////////////////////////////////////
902 return (c
> 32 && c
!= 46 && c
!= 44
903 && c
!= 58 && c
!= 59
904 && c
!= 60 && c
!= 62
905 && c
!= 34 && c
!= 92
911 return (c
> 32 && c
!= 46 && c
!= 44
912 && c
!= 58 && c
!= 59
913 && c
!= 60 && c
!= 62
914 && c
!= 34 && c
!= 92
918 int foox3() { return 1; }
935 assert(andand1(0) == 0);
936 assert(andand1(32) == 0);
937 assert(andand1(46) == 0);
938 assert(andand1(44) == 0);
939 assert(andand1(58) == 0);
940 assert(andand1(59) == 0);
941 assert(andand1(60) == 0);
942 assert(andand1(62) == 0);
943 assert(andand1(34) == 0);
944 assert(andand1(92) == 0);
945 assert(andand1(39) == 0);
946 assert(andand1(33) == 1);
947 assert(andand1(61) == 1);
948 assert(andand1(93) == 1);
949 assert(andand1(255) == 1);
951 assert(andand2(0) == false);
952 assert(andand2(32) == false);
953 assert(andand2(46) == false);
954 assert(andand2(44) == false);
955 assert(andand2(58) == false);
956 assert(andand2(59) == false);
957 assert(andand2(60) == false);
958 assert(andand2(62) == false);
959 assert(andand2(34) == false);
960 assert(andand2(92) == false);
961 assert(andand2(39) == false);
962 assert(andand2(33) == true);
963 assert(andand2(61) == true);
964 assert(andand2(93) == true);
965 assert(andand2(255) == true);
967 assert(andand3(6) == 3);
968 assert(andand3(30) == 4);
971 ////////////////////////////////////////////////////////////////////////
973 bool bittest11508(char c
)
975 return c
=='_' || c
=='-' || c
=='+' || c
=='.';
980 assert(bittest11508('_'));
983 ////////////////////////////////////////////////////////////////////////
987 return x |
(x
<<8) |
(x
<<16) |
(x
<<24) |
(x
* 3);
990 void testor_combine()
992 printf("%x\n", or1(1));
993 assert(or1(5) == 5 * (0x1010101 |
3));
996 ////////////////////////////////////////////////////////////////////////
1000 return ((i
+1)>>1)<<1;
1005 assert(shrshl(6) == 6);
1006 assert(shrshl(7) == 8);
1009 ////////////////////////////////////////////////////////////////////////
1011 bool bt10715(in uint[] ary
, size_t bitnum
)
1013 return !!(ary
[bitnum
>> 5] & 1 << (bitnum
& 31)); // uses bt
1016 bool neg_bt10715(in uint[] ary
, size_t bitnum
)
1018 return !(ary
[bitnum
>> 5] & 1 << (bitnum
& 31)); // does not use bt
1023 static uint[2] a1
= [0x1001_1100, 0x0220_0012];
1025 if ( bt10715(a1
,30)) assert(0);
1026 if (!bt10715(a1
,8)) assert(0);
1027 if ( bt10715(a1
,30+32)) assert(0);
1028 if (!bt10715(a1
,1+32)) assert(0);
1030 if (!neg_bt10715(a1
,30)) assert(0);
1031 if ( neg_bt10715(a1
,8)) assert(0);
1032 if (!neg_bt10715(a1
,30+32)) assert(0);
1033 if ( neg_bt10715(a1
,1+32)) assert(0);
1036 ////////////////////////////////////////////////////////////////////////
1038 ptrdiff_t
compare12164(A12164
* rhsPA
, A12164
* zis
)
1042 return ptrdiff_t
.min
;
1054 assert(compare12164(&a
, &b
));
1057 ////////////////////////////////////////////////////////////////////////
1059 int foo10678(char[5] txt
)
1061 return txt
[0] + txt
[1] + txt
[4];
1066 char[5] hello
= void;
1070 int i
= foo10678(hello
);
1074 ////////////////////////////////////////////////////////////////////////
1080 assert(c
== 'P' || c
== 'M');
1089 S12051(i
< ip
.length ?
'P' : 'M');
1093 ////////////////////////////////////////////////////////////////////////
1095 void bug7565( double x
) { assert(x
== 3); }
1104 ////////////////////////////////////////////////////////////////////////
1106 int bug8525(int[] devt
)
1111 ////////////////////////////////////////////////////////////////////////
1113 void func13190(int) {}
1121 __gshared Struct13190
* table13190
=
1129 for (int i
= 0; table13190
[i
].a
; i
++)
1131 ulong tbl
= table13190
[i
].a
;
1141 ////////////////////////////////////////////////////////////////////////
1143 double foo13485(double c
, double d
)
1145 // This must not be optimized to c += (d + d)
1153 enum double d
= 0X1P
+1023;
1154 assert(foo13485(-d
, d
) == d
);
1157 ////////////////////////////////////////////////////////////////////////
1159 void test12833a(int a
)
1161 long x
= cast(long)a
;
1163 switch (cast(int)(cast(ushort)(x
>> 16 & 65535L)))
1178 test12833a(0x1_0000);
1181 /***********************************************/
1192 if (arr
[0].f
!= 3.0) assert(0);
1193 if (arr
[0].g
!= 4.0) assert(0);
1205 if (arr
[0].f
!= 0.0) assert(0);
1206 if (arr
[0].g
!= 0.0) assert(0);
1209 ////////////////////////////////////////////////////////////////////////
1210 // https://issues.dlang.org/show_bug.cgi?id=12057
1212 bool prop12057(real x
) { return false; }
1213 double f12057(real) { return double.init
; }
1216 real fc
= f12057(real.init
);
1217 if (fc
== 0 || fc
.prop12057
) {}
1221 ////////////////////////////////////////////////////////////////////////
1223 long modulo24 (long ticks
)
1225 ticks
%= 864000000000;
1227 ticks
+= 864000000000;
1233 assert (modulo24(-141600000000) == 722400000000);
1237 ////////////////////////////////////////////////////////////////////////
1243 int test13969(const S13969
* f
) {
1244 return 0 % ((f
.y
> 0) ? f
.x
/ f
.y
: f
.x
/ -f
.y
);
1247 ////////////////////////////////////////////////////////////////////////
1252 assert(arr14436
== null);
1253 arr14436
= [1, 2, 3];
1254 assert(arr14436
!= null);
1257 ////////////////////////////////////////////////////////////////////////
1261 auto a
= toString(14);
1263 printf("a.ptr = %p, a.length = %d\n", a
.ptr
, cast(int)a
.length
);
1267 auto toString(int value
)
1269 uint mValue
= value
;
1271 char[int.sizeof
* 3] buffer
= void;
1272 size_t index
= buffer
.length
;
1276 uint div = cast(int)(mValue
/ 10);
1277 char mod
= mValue
% 10 + '0';
1278 buffer
[--index
] = mod
; // Line 22
1282 //printf("buffer.ptr = %p, index = %d\n", buffer.ptr, cast(int)index);
1283 return dup(buffer
[index
.. $]);
1286 char[] dup(char[] a
)
1288 //printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1289 a
[0] = 1; // segfault
1293 ////////////////////////////////////////////////////////////////////////
1295 int stripLeft(int str, int dc
)
1308 if (stripLeft(3, 1) != 3) // fails with -O
1313 ////////////////////////////////////////////////////////////////////////
1320 b
= (b
.ptr
+ b
.length
- 5)[0 .. b
.ptr
+ b
.length
- 1 - a
.ptr
];
1321 assert(b
.ptr
== a
.ptr
+ 1);
1322 assert(b
.length
== 5);
1325 ////////////////////////////////////////////////////////////////////////
1326 // https://issues.dlang.org/show_bug.cgi?id=14782
1337 static Foo
[1] fun() { Foo
[1] a
; return a
; }
1339 auto result
= fun();
1340 assert(result
[0].a
== 8);
1341 assert(result
[0].b
== 7);
1344 ////////////////////////////////////////////////////////////////////////
1352 static assert((Foo
[4]).sizeof
== 16);
1354 static Foo
[4] fun() { Foo
[4] a
; return a
; }
1356 auto result
= fun();
1357 assert(result
[0].b
== 7);
1358 assert(result
[1].b
== 7);
1359 assert(result
[2].b
== 7);
1360 assert(result
[3].b
== 7);
1363 ////////////////////////////////////////////////////////////////////////
1365 void[] calloc15272(size_t bc
) nothrow pure
1373 void[] scache
= cast(void[])"abc";
1375 void[]* buckets
= &scache
;
1376 *buckets
= calloc15272(count
)[0 .. count
];
1379 /*****************************************
1380 * https://issues.dlang.org/show_bug.cgi?id=15861
1385 double val
= 4286853117.;
1388 assert(val
== 4286853117.);
1392 ////////////////////////////////////////////////////////////////////////
1394 // https://issues.dlang.org/show_bug.cgi?id=15629 comment 3
1400 int value
= a
[0] >= 0 ? a
[0] : -a
[0];
1405 void writeln(int v
, int[] a
)
1409 ////////////////////////////////////////////////////////////////////////
1411 real binPosPow2() { return 1.0L; }
1415 return 1.0L/binPosPow2();
1420 assert(binPow2() == 1.0L);
1423 ////////////////////////////////////////////////////////////////////////
1424 // https://issues.dlang.org/show_bug.cgi?id=13474
1427 double sumKBN(double s
= 0.0)
1429 import core
.math
: fabs;
1431 foreach(double x
; [1, 1e100
, 1, -1e100
])
1435 if(s
.fabs >= x
.fabs)
1450 double multiply(double a
) { return a
* 10000; }
1455 assert(r
== sumKBN());
1458 ////////////////////////////////////////////////////////////////////////
1459 // https://issues.dlang.org/show_bug.cgi?id=16699
1461 ulong[1] parseDateRange()
1476 ulong[1] range
= parseDateRange();
1477 assert(range
[0] == 6);
1480 ////////////////////////////////////////////////////////////////////////
1482 // https://issues.dlang.org/show_bug.cgi?id=16102
1484 struct S16102
{ ~this() { } }
1494 assert( f16102() == [1] );
1497 ////////////////////////////////////////////////////////////////////////
1499 void test5a(ulong x
, ulong y
)
1517 test5a(uint.max
+ 1L, uint.max
);
1520 ////////////////////////////////////////////////////////////////////////
1522 /* Test the pattern:
1523 * replace (e ? i1 : i2) with (i1 + e * (i2 - i1))
1524 * when e1 is 0 or 1 and (i2-i1) is a power of 2.
1529 return (i
% 2 != 0) ?
4 : 2;
1534 return (i
% 2 != 0) ?
2 : 4;
1537 bool bar6(bool b
) { return b
; }
1541 return bar6(b
) ?
16 : 8;
1546 return bar6(b
) ?
8 : 16;
1551 if (foo61(0) != 2) assert(0);
1552 if (foo61(1) != 4) assert(0);
1553 if (foo62(0) != 4) assert(0);
1554 if (foo62(1) != 2) assert(0);
1555 if (foo63(0) != 8) assert(0);
1556 if (foo63(1) != 16) assert(0);
1557 if (foo64(0) != 16) assert(0);
1558 if (foo64(1) != 8) assert(0);
1561 ////////////////////////////////////////////////////////////////////////
1563 int dataflow(int b
) {
1577 void testeqeqranges()
1579 int i
= dataflow(4);
1584 ////////////////////////////////////////////////////////////////////////
1586 // https://issues.dlang.org/show_bug.cgi?id=16189
1597 assert(a
== -1); // was failing with -O
1601 ////////////////////////////////////////////////////////////////////////
1603 // https://issues.dlang.org/show_bug.cgi?id=16997
1607 /* Exhaustively test all signed and unsigned byte promotions for
1610 for (int i
= 0; i
< 256; ++i
)
1612 ubyte c
= cast(ubyte)i
;
1614 int i1
= cast(int)(~c
);
1615 int i2
= cast(int)(~cast(int)c
);
1617 //printf("%d, %d\n", i1, i2);
1621 i2
= cast(int)(+cast(int)c
);
1625 i2
= cast(int)(-cast(int)c
);
1629 for (int i
= 0; i
< 256; ++i
)
1631 byte c
= cast(byte)i
;
1633 int i1
= cast(int)(~c
);
1634 int i2
= cast(int)(~cast(int)c
);
1636 //printf("%d, %d\n", i1, i2);
1640 i2
= cast(int)(+cast(int)c
);
1644 i2
= cast(int)(-cast(int)c
);
1649 ////////////////////////////////////////////////////////////////////////
1651 void test18315() // https://issues.dlang.org/show_bug.cgi?id=18315
1660 ////////////////////////////////////////////////////////////////////////
1662 // https://issues.dlang.org/show_bug.cgi?id=18461
1668 size_t test_val
= 0b0001_0000;
1670 if (bt(&test_val
, 4) == 0)
1674 ////////////////////////////////////////////////////////////////////////
1676 void test18730() // https://issues.dlang.org/show_bug.cgi?id=18730
1678 static if (size_t
.sizeof
== 8)
1680 static int bt18730_64_64(in ulong* p
, ulong bitnum
) pure @system
1682 return ((p
[bitnum
>> 6] & (1L << (bitnum
& 63)))) != 0;
1684 static int bt18730_64_32(in ulong* p
, uint bitnum
) pure @system
1686 return ((p
[bitnum
>> 6] & (1L << (bitnum
& 63)))) != 0;
1688 static int bt18730_32_64(in uint* p
, ulong bitnum
) pure @system
1690 return ((p
[bitnum
>> 5] & (1 << (bitnum
& 31)))) != 0;
1693 // Check that bt_64_64 uses a 64-bit register for the offset.
1695 enum bitIndex
= int.max
+ 1L;
1696 auto a
= new ulong[](bitIndex
/ 64 + 1);
1697 a
[bitIndex
/ 64] = 1;
1698 assert(bt18730_64_64(a
.ptr
, bitIndex
));
1699 assert(!bt18730_64_64(a
.ptr
, bitIndex
+ 1));
1700 assert(!bt18730_64_64(a
.ptr
, bitIndex
- 1));
1702 // Check that bt_64_32 uses a 32-bit register for the offset.
1704 static int f(ulong* p
, ulong bitnum
)
1706 return bt18730_64_32(p
, cast(uint) bitnum
);
1708 enum bitIndex
= uint.max
+ 1L;
1709 assert(cast(uint) bitIndex
== 0);
1711 assert(f(&s
, bitIndex
));
1713 /* Check that bt_32_64 does not become a 64-bit bt instruction. Would lead
1714 to a segfault when trying to load 8 bytes while only 4 are accessible. */
1717 import core
.sys
.posix
.sys
.mman
;
1718 import core
.sys
.posix
.unistd
;
1719 // Allocate two pages.
1720 immutable sz
= 2 * sysconf(_SC_PAGESIZE
);
1721 auto m
= mmap(null, sz
, PROT_READ
, MAP_PRIVATE | MAP_ANON
, -1, 0);
1722 // Discard the higher page. It becomes unreadable.
1723 munmap(m
+ sz
/ 2, sz
/ 2);
1724 // Try looking at the last 4 bytes of the readable page.
1725 uint* p
= cast(uint*) (m
+ sz
/ 2 - uint.sizeof
);
1726 bt18730_32_64(p
, 0);
1727 munmap(m
, sz
/ 2); // Free the readable page.
1732 ////////////////////////////////////////////////////////////////////////
1734 void test19497() // https://issues.dlang.org/show_bug.cgi?id=19497
1738 ushort* ushortPtr
= cast(ushort*) data
.ptr
;
1739 *ushortPtr
++ = 0xfe00;
1740 printf("ushortPtr(%p)\n", ushortPtr
);
1744 alias Seq(stuff
...) = stuff
;
1745 static foreach (T
; Seq
!(ubyte, ushort, uint, ulong, byte, short, int, long))
1749 *q
++ = cast(T
) 0x1122334455667788;
1750 if (*q
!= 0x2A) assert(false);
1754 static int toStringz(string s
) { return s
.length
> 0 ? s
[0] : 0; }
1755 static void toAStringz(in string
[] a
, int* az
)
1757 foreach (string s
; a
)
1759 *az
++ = toStringz(s
);
1762 string
[1] sa
= ["abc"];
1764 toAStringz(sa
[], tgt
.ptr
);
1765 if (tgt
[0] != 'a') assert(false);
1766 if (tgt
[1] != 0x2a) assert(false);
1770 ////////////////////////////////////////////////////////////////////////
1772 // https://issues.dlang.org/show_bug.cgi?id=18794
1774 bool method18794(size_t
* p
)
1778 return (*p
& (1UL << bitIdx
)) != 0;
1796 ////////////////////////////////////////////////////////////////////////
1798 /* Test the optimization
1799 * (e1+c)-e2 => (e1-e2)+c
1804 static void foo(int i
)
1806 static ubyte[4] bar()
1809 foreach (i
, ref a
; array
)
1810 a
= cast(ubyte)(i
+ 1);
1814 static void test(int i
, ubyte* p
)
1817 assert(p
[i
* 4 + j
] == j
+ 1);
1821 data
[i
*4..(i
+1)*4] = bar(); // optimize to single MOV
1829 ////////////////////////////////////////////////////////////////////////
1831 const(char)* fastpar(string s
)
1833 return s
.ptr
+ s
.length
;
1839 auto p
= fastpar(s
);
1843 ////////////////////////////////////////////////////////////////////////
1844 // https://issues.dlang.org/show_bug.cgi?id=20363
1846 ulong foo20363(double d
)
1848 ulong u
= * cast(ulong*) &d
;
1849 return (u
>> 1) & 1;
1855 if (foo20363(*cast(double*) &u
) == 0)
1859 ////////////////////////////////////////////////////////////////////////
1862 T
testfooa(T
)(T value
)
1864 return 10 - (value
* 57); // gets rewritten into (value*-57)+10
1867 T
testfoob(T
)(T value
)
1869 return (value
* -57) + 10;
1874 assert(testfooa(1) == -47);
1875 assert(testfoob(1) == -47);
1876 assert(testfooa(1.0) == -47);
1877 assert(testfoob(1.0) == -47);
1880 ////////////////////////////////////////////////////////////////////////
1882 // https://issues.dlang.org/show_bug.cgi?id=16317
1884 int add8ret3(ref int s
)
1892 val
= val
+ add8ret3(val
);
1898 assert(binAdd(1) == (1 + 3));
1899 static assert(binAdd(1) == (1 + 3));
1902 ////////////////////////////////////////////////////////////////////////
1904 // https://issues.dlang.org/show_bug.cgi?id=20050
1906 int test20050_g
= 0;
1907 void test20050_impure_function_1() { ++test20050_g
; }
1908 void function() test20050_get_impure_function() pure
1910 static void impure_function_2()
1913 test20050_impure_function_1();
1915 return &impure_function_2
;
1919 auto f
= test20050_get_impure_function();
1921 assert(test20050_g
== 2);
1924 ////////////////////////////////////////////////////////////////////////
1926 // http://github.com/dlang/dmd/pull/11238
1928 int testCpStatic1(int y
)
1930 __gshared
int yyy
= 7;
1931 auto x
= yyy
; // no copy-propagation
1939 assert(testCpStatic1(1) == 7);
1940 assert(testCpStatic1(0) == 10);
1943 ////////////////////////////////////////////////////////////////////////
1944 // https://issues.dlang.org/show_bug.cgi?id=20991
1956 for (int i
= 0; i
<= 1; ++i
)
1961 ////////////////////////////////////////////////////////////////////////
1963 // http://github.com/dlang/dmd/pull/11388
1965 ushort byteswap(ushort x
) pure
1967 // Should be detected and XCHG instruction generated
1968 return cast(ushort) (((x
>> 8) & 0xFF) |
((x
<< 8) & 0xFF00u
));
1973 assert(byteswap(0xF234) == 0x34F2);
1974 static ushort xx
= 0xF234;
1975 assert(byteswap(xx
) == 0x34F2);
1978 ////////////////////////////////////////////////////////////////////////
1980 // These should all be recognized by the compiler and generate ROL or ROR
1983 uint rol32(uint x
, uint n
)
1985 return (x
<< n
) |
(x
>> (32 - n
));
1988 uint ror32(uint x
, uint n
)
1990 return (x
>> n
) |
(x
<< (32 - n
));
1993 ulong rol64(ulong x
, uint n
)
1995 return (x
<< n
) |
(x
>> (64 - n
));
1998 ulong ror64(ulong x
, uint n
)
2000 return (x
>> n
) |
(x
<< (64 - n
));
2005 assert(ror32(0x0123_4567u, 4) == 0x7012_3456);
2006 assert(rol32(0x7012_3456u, 4) == 0x0123_4567);
2008 assert(ror64(0x0123_4567_89AB_CDEFuL
, 4) == 0xF012_3456_789A_BCDE
);
2009 assert(rol64(0xF012_3456_789A_BCDEuL
, 4) == 0x0123_4567_89AB_CDEF
);
2012 ////////////////////////////////////////////////////////////////////////
2014 // https://issues.dlang.org/show_bug.cgi?id=20162
2018 static long f(long a
)
2028 printf("%d %d %llx\n", i
,
2029 ((i
!= 0) ?
-1 : +1),
2030 f((i
!= 0) ?
-1 : +1));
2035 ////////////////////////////////////////////////////////////////////////
2036 // https://issues.dlang.org/show_bug.cgi?id=3713
2040 return i ?
star1(i
- 1) : 0;
2045 return i
== 0 ?
0 : star2(i
- 1);
2052 return i
== 2 ?
star3(i
- 2) : star3(i
- 1);
2058 : i
!= 2 ?
star4(i
- 1)
2064 assert(star1(10) == 0);
2065 assert(star2(10) == 0);
2066 assert(star3(10) == 0);
2067 assert(star4(10) == 0);
2070 ////////////////////////////////////////////////////////////////////////
2074 // special code is generated for these two cases
2075 static long foolt(dchar c
)
2077 return c
< 0x10000 ?
1 : 2;
2080 static long fooge(uint c
)
2082 return c
>= 0x10000 ?
1L : 2L;
2085 assert(foolt(0) == 1);
2086 assert(foolt(0x10000) == 2);
2087 assert(fooge(0) == 2);
2088 assert(fooge(0x10000) == 1);
2092 ////////////////////////////////////////////////////////////////////////
2094 // https://issues.dlang.org/show_bug.cgi?id=19846
2096 alias Void
= byte[0];
2097 static immutable Void VOID
; // = [];
2099 __gshared
int x19846
;
2103 //printf("This should print\n");
2108 Void
identity19846(Void value
, out int i
)
2117 identity19846(print19846(), i
);
2118 //printf("i = %d\n", i);
2119 assert(x19846
== 3);
2123 ////////////////////////////////////////////////////////////////////////
2125 // Some tests for OPmemcpy
2132 foreach (i
, ref a
; array
)
2133 a
= cast(ubyte)(i
+ 1);
2141 assert(p
[i
] == i
+ 1);
2148 bits2
[0..N
] = bits
[0..N
] = def();
2152 __gshared size_t n
= N
;
2155 bits4
[0..n
] = bits3
[0..n
] = def();
2160 ////////////////////////////////////////////////////////////////////////
2163 /* Test all the cases of uses of LEA for multiplication by a constant
2166 T
testlea(uint C
, T
)(T x
, T y
)
2168 y
= y
* C
; // cdmul()
2169 x
*= C
; // cdmulass()
2173 void testleax(uint C
)(uint X
, uint Y
)
2175 assert(testlea
!C(X
,Y
) == C
* (X
+ Y
));
2176 assert(testlea
!C(cast(long)X
,cast(long)Y
) == cast(long)C
*X
+ cast(long)C
*Y
);
2201 ////////////////////////////////////////////////////////////////////////
2203 /* Test *= of register pair
2206 void testMulAssPair()
2208 static ulong pow(ulong x
, int m
)
2224 enum ulong e_10_pow_19
= 10uL^^
19;
2225 assert(e_10_pow_19
== pow(10uL, 19));
2228 ////////////////////////////////////////////////////////////////////////
2229 // https://issues.dlang.org/show_bug.cgi?id=21038
2231 const(wchar)* x21038
= "xz";
2232 const(dchar)* name21038
= "abcd";
2236 assert((cast(size_t
) x21038
) % wchar.sizeof
== 0);
2237 assert((cast(size_t
) name21038
) % dchar.sizeof
== 0);
2240 ////////////////////////////////////////////////////////////////////////
2241 // https://issues.dlang.org/show_bug.cgi?id=21325
2243 real f21325(const real x
) pure @safe nothrow @nogc
2245 return (x
!= 0.0L) ? x
: real.nan
;
2248 void test21325() @safe
2253 const y
= f21325(x
); // should set y to real.nan
2262 ////////////////////////////////////////////////////////////////////////
2263 // https://issues.dlang.org/show_bug.cgi?id=16274
2265 extern(C
) int pair(short a
, ushort b
, byte c
, ubyte d
);
2269 // provide alternate implementation of .pair()
2270 pragma(mangle
, "pair")
2271 extern(C
) static void pair(int a
, int b
, int c
, int d
)
2273 //printf("%d %d %d %d\n", a, b, c, d);
2289 ////////////////////////////////////////////////////////////////////////
2290 // https://issues.dlang.org/show_bug.cgi?id=16268
2294 static void f(byte x
)
2296 for (byte i
= 0; i
<= x
&& i
>= 0; ++i
)
2300 //printf("%d\n", i);
2307 ////////////////////////////////////////////////////////////////////////
2308 // https://issues.dlang.org/show_bug.cgi?id=11435
2314 static void fun(T c
, T b
, int v
)
2318 static void abc(T
[] b
)
2325 import core
.sys
.windows
.windows
;
2326 auto p
= VirtualAlloc(null, 4096, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
2330 import core
.sys
.posix
.sys
.mman
;
2331 auto p
= mmap(null, 4096, PROT_READ|PROT_WRITE
, MAP_PRIVATE|MAP_ANON
, -1, 0L);
2334 auto px
= (cast(T
*)(p
+ 4096 - 2 * T
.sizeof
));
2340 import core
.sys
.windows
.windows
;
2343 static void fun(T c
, T b
, int v
)
2347 static void abc(T
[] b
)
2354 import core
.sys
.windows
.windows
;
2355 auto p
= VirtualAlloc(null, 4096, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
2359 import core
.sys
.posix
.sys
.mman
;
2360 auto p
= mmap(null, 4096, PROT_READ|PROT_WRITE
, MAP_PRIVATE|MAP_ANON
, -1, 0L);
2363 auto px
= (cast(T
*)(p
+ 4096 - 2 * T
.sizeof
));
2367 ////////////////////////////////////////////////////////////////////////
2368 // https://issues.dlang.org/show_bug.cgi?id=21513
2372 size_t c
; // declare after items and not crash !
2376 void grow(ref Stuff stuff
)
2380 const oldCapacity
= c
;
2381 items
.ptr
[0..oldCapacity
] = items
.ptr
[0..0]; // use literal 0 instead of
2382 items
.ptr
[0] = 0; // oldcapacity and no crash !
2392 ////////////////////////////////////////////////////////////////////////
2393 // https://issues.dlang.org/show_bug.cgi?id=21526
2395 double f21256(double a
, double b
) {
2409 a
.l
= 0x4341c37937e08000;
2410 b
.l
= 0x4007ffcb923a29c7;
2413 r
.d
= f21256(a
.d
, b
.d
);
2414 //if (r.d != 0x1.1c37937e08001p+53)
2415 //printf("r = %A should be 0x1.1c37937e08001p+53 %A\n", r.d, 0x1.1c37937e08001p+53);
2416 //assert(r == 0x1.1c37937e08001p+53);
2418 // cannot seem to get the two to produce the same value
2419 assert(r
.l
== 0x4341c37937e08001 ||
// value using XMM
2420 r
.l
== 0x4341c37937e08002); // value using x87
2423 ////////////////////////////////////////////////////////////////////////
2424 // https://issues.dlang.org/show_bug.cgi?id=21816
2426 bool test21816a(float t
)
2433 assert(test21816a(float.nan
));
2436 ////////////////////////////////////////////////////////////////////////
2437 // https://issues.dlang.org/show_bug.cgi?id=21835
2449 if (arr
[0].f
!= 3.0) assert(0);
2450 if (arr
[0].d
!= 4.0) assert(0);
2451 if (arr
[0].r
!= 5.0) assert(0);
2465 if (arr
[0].f
!= 0.0) assert(0);
2466 if (arr
[0].d
!= 0.0) assert(0);
2467 if (arr
[0].r
!= 0.0) assert(0);
2470 ////////////////////////////////////////////////////////////////////////
2474 // All the various integer divide tests
2514 test13023(0x10_0000_0000);
2570 printf("Success\n");