d: Merge upstream dmd 3982604c5, druntime bc58b1e9, phobos 12329adb6.
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / mars1.d
blob30aa9503b09caa8732dc7f18fa62934208c96854
1 /*
2 REQUIRED_ARGS: -mcpu=native
3 PERMUTE_ARGS: -O -inline -release
4 */
6 import core.stdc.stdio;
8 template tuple(A...) { alias tuple = A; }
10 ///////////////////////
12 // https://github.com/dlang/dmd/pull/11441
14 long sdiv1(long l)
16 return l / 2;
19 int sdiv2(int i)
21 return i / 2;
24 void testsdiv2()
26 assert(sdiv1(10) == 5);
27 assert(sdiv1(-10) == -5);
28 assert(sdiv2(10) == 5);
29 assert(sdiv2(-10) == -5);
32 ///////////////////////
34 void testulldiv()
36 __gshared ulong[4][] vectors =
38 [10,3,3,1],
39 [10,1,10,0],
40 [3,10,0,3],
41 [10,10,1,0],
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 ////////////////////////////////////////////////////////////////////////
72 uint udiv10(uint x)
74 return x / 10;
77 uint udiv14(uint x)
79 return x / 14;
82 uint udiv14007(uint x)
84 return x / 14007;
87 uint umod10(uint x)
89 return x % 10;
92 uint umod14(uint x)
94 return x % 14;
97 uint umod14007(uint x)
99 return x % 14007;
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)
121 return x / 10;
124 ulong uldiv14(ulong x)
126 return x / 14;
129 ulong uldiv14007(ulong x)
131 return x / 14007;
134 ulong ulmod10(ulong x)
136 return x % 10;
139 ulong ulmod14(ulong x)
141 return x % 14;
144 ulong ulmod14007(ulong x)
146 return x % 14007;
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);
165 void testfastudiv()
168 static uint x10 = 10;
169 static uint x14 = 14;
170 static uint x14007 = 14007;
172 uint u = 10000;
173 uint r;
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;
189 ulong u = 10000;
190 ulong r;
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); }
222 void testsldiv()
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!(
231 1,2,3,10,300,1000,
232 4_1001_2030_0030,
233 0x7FFF_F8FF_FF3F_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); }
285 void testslmod()
287 static foreach (C; tuple!(
288 1,2,3,10,300,1000,
289 4_1001_2030_0030,
290 0x7FFF_F8FF_FF3F_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)
328 T y = x;
329 y /= C;
330 assert(y == x / C);
331 y = x;
332 y /= -C;
333 assert(y == x / -C);
334 return x / C;
337 T modC(int C, T)(T x)
339 T y = x;
340 y %= C;
341 assert(y == x % C);
342 y = x;
343 y %= -C;
344 assert(y == x % -C);
345 return x % C;
348 T remquoC(int C, T)(T x)
350 return (x / C) | (x % C);
353 void testfastdiv()
355 static int z = 0; // prevent constant folding by optimizer
357 static foreach (T; tuple!(int, long, uint, ulong))
359 T u = 10000;
360 T r;
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 ////////////////////////////////////////////////////////////////////////
373 /* Test the pattern:
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);
385 void testdivdiv()
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 ////////////////////////////////////////////////////////////////////////
401 void testdivcmp()
403 // https://github.com/dlang/dmd/pull/7128
404 static bool foo(uint a, uint b)
406 return cast(bool)(a / b); // convert / to >=
409 assert(!foo(3, 4));
410 assert(foo(4, 4));
411 assert(foo(5, 4));
414 /////////////////////////////////////////////////////
416 void testgoto()
418 int i;
420 i = 3;
421 goto L4;
422 L3: i++;
423 goto L5;
424 L4: goto L3;
425 L5: assert(i == 4);
428 int testswitch()
430 int i;
432 i = 3;
433 switch (i)
435 case 0:
436 case 1:
437 default:
438 assert(0);
439 case 3:
440 break;
442 return 0;
445 void testdo()
447 int x = 0;
451 x++;
452 } while (x < 10);
453 printf("x == %d\n", x);
454 assert(x == 10);
458 void testbreak()
459 { int i, j;
461 Louter:
462 for (i = 0; i < 10; i++)
464 for (j = 0; j < 10; j++)
466 if (j == 3)
467 break Louter;
471 printf("i = %d, j = %d\n", i, j);
472 assert(i == 0);
473 assert(j == 3);
476 ///////////////////////
478 int foo(string s)
480 int i;
482 i = 0;
483 switch (s)
485 case "hello":
486 i = 1;
487 break;
488 case "goodbye":
489 i = 2;
490 break;
491 case "goodb":
492 i = 3;
493 break;
494 default:
495 i = 10;
496 break;
498 return i;
502 void teststringswitch()
503 { int i;
505 i = foo("hello");
506 printf("i = %d\n", i);
507 assert(i == 1);
509 i = foo("goodbye");
510 printf("i = %d\n", i);
511 assert(i == 2);
513 i = foo("goodb");
514 printf("i = %d\n", i);
515 assert(i == 3);
517 i = foo("huzzah");
518 printf("i = %d\n", i);
519 assert(i == 10);
523 ///////////////////////
525 struct Foo
527 int a;
528 char b;
529 long c;
532 Foo test(Foo f)
534 f.a += 1;
535 f.b += 3;
536 f.c += 4;
537 return f;
541 void teststrarg()
543 Foo g;
544 g.a = 1;
545 g.b = 2;
546 g.c = 3;
548 Foo q;
549 q = test(g);
550 assert(q.a == 2);
551 assert(q.b == 5);
552 assert(q.c == 7);
555 ///////////////////////
557 align (1) struct Foo1
559 align (1):
560 int a;
561 char b;
562 long c;
565 struct Foo2
567 int a;
568 char b;
569 long c;
572 struct Foo3
574 int a;
575 align (1) char b;
576 long c;
579 struct Foo4
581 int a;
582 struct { char b; }
583 long c;
586 void testsizes()
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;
615 void test11565()
617 assert(cond11565(true) == size_t.max);
620 ///////////////////////
622 int[3] array1 = [1:1,2,0:3];
624 void testarrayinit()
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) {}
637 ulong k = 0;
639 func(k >= n / 2);
641 if (k >= n / 2)
642 assert(0);
645 ///////////////////////
647 struct U { int a; union { char c; int d; } long b; }
649 U f = { b:3, d:0x22222222, a:1 };
651 void testU()
653 assert(f.b == 3);
654 assert(f.d == 0x22222222);
655 assert(f.c == 0x22);
656 assert(f.a == 1);
657 assert(f.sizeof == 16);
658 assert(U.sizeof == 16);
662 ////////////////////////////////////////////////////////////////////////
664 void vfunc() {}
666 void test12095(int k)
668 int e = 0;
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 )
680 printf("%Lf\n", u );
681 return t && u;
684 bool test3918b( real t, float u )
686 printf("%Lf\n", t );
687 return t && u;
690 void test3918()
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) {
699 l += (thresh < val);
700 return l;
703 T docond2(T)(T l, ubyte thresh, ubyte val) {
704 l -= (thresh >= val);
705 return l;
708 T docond3(T)(T l, ubyte thresh, ubyte val) {
709 l += (thresh >= val);
710 return l;
713 T docond4(T)(T l, ubyte thresh, ubyte val) {
714 l -= (thresh < val);
715 return l;
718 void testdocond()
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 ////////////////////////////////////////////////////////////////////////
764 struct S8658
766 int[16385] a;
769 void foo8658(S8658 s)
771 int x;
774 void test8658()
776 S8658 s;
777 for(int i = 0; i < 1000; i++)
778 foo8658(s);
781 ////////////////////////////////////////////////////////////////////////
783 uint neg(uint i)
785 return ~i + 1;
788 uint com(uint i)
790 return -i - 1;
793 float com(float i)
795 return -i - 1;
798 uint com2(uint i)
800 return -(i + 1);
803 void testnegcom()
805 assert(neg(3) == -3);
806 assert(com(3) == -4);
807 assert(com(3.0f) == -4.0f);
808 assert(com2(3) == -4);
811 ////////////////////////////////////////////////////////////////////////
813 int oror1(char c)
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);
821 int oror2(char c)
823 return ((((((((((c <= 32 || c == 46) || c == 44)
824 || c == 58) || c == 59) || c == 60)
825 || c == 62) || c == 34) || c == 92)
826 || c == 39) != 0);
829 void testoror()
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);
876 void testbt()
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 ////////////////////////////////////////////////////////////////////////
887 void test13383()
889 foreach (k; 32..33)
891 if (1L & (1L << k))
893 assert(0);
898 ////////////////////////////////////////////////////////////////////////
900 int andand1(int c)
902 return (c > 32 && c != 46 && c != 44
903 && c != 58 && c != 59
904 && c != 60 && c != 62
905 && c != 34 && c != 92
906 && c != 39) != 0;
909 bool andand2(long c)
911 return (c > 32 && c != 46 && c != 44
912 && c != 58 && c != 59
913 && c != 60 && c != 62
914 && c != 34 && c != 92
915 && c != 39) != 0;
918 int foox3() { return 1; }
920 int andand3(uint op)
922 if (foox3() &&
923 op != 7 &&
924 op != 3 &&
925 op != 18 &&
926 op != 30 &&
927 foox3())
928 return 3;
929 return 4;
933 void testandand()
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=='.';
978 void testbittest()
980 assert(bittest11508('_'));
983 ////////////////////////////////////////////////////////////////////////
985 uint or1(ubyte x)
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 ////////////////////////////////////////////////////////////////////////
999 int shrshl(int i) {
1000 return ((i+1)>>1)<<1;
1003 void testshrshl()
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
1021 void test10715()
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)
1040 if (*rhsPA == *zis)
1041 return 0;
1042 return ptrdiff_t.min;
1045 struct A12164
1047 int a;
1050 void test12164()
1052 auto a = A12164(3);
1053 auto b = A12164(2);
1054 assert(compare12164(&a, &b));
1057 ////////////////////////////////////////////////////////////////////////
1059 int foo10678(char[5] txt)
1061 return txt[0] + txt[1] + txt[4];
1064 void test10678()
1066 char[5] hello = void;
1067 hello[0] = 8;
1068 hello[1] = 9;
1069 hello[4] = 10;
1070 int i = foo10678(hello);
1071 assert(i == 27);
1074 ////////////////////////////////////////////////////////////////////////
1076 struct S12051
1078 this(char c)
1080 assert(c == 'P' || c == 'M');
1084 void test12051()
1086 auto ip = ["abc"];
1087 foreach (i, s; ip)
1089 S12051(i < ip.length ? 'P' : 'M');
1093 ////////////////////////////////////////////////////////////////////////
1095 void bug7565( double x) { assert(x == 3); }
1097 void test7565()
1099 double y = 3;
1100 bug7565( y++ );
1101 assert(y == 4);
1104 ////////////////////////////////////////////////////////////////////////
1106 int bug8525(int[] devt)
1108 return devt[$ - 1];
1111 ////////////////////////////////////////////////////////////////////////
1113 void func13190(int) {}
1115 struct Struct13190
1117 ulong a;
1118 uint b;
1121 __gshared Struct13190* table13190 =
1123 Struct13190(1, 1),
1124 Struct13190(0, 2)
1127 void test13190()
1129 for (int i = 0; table13190[i].a; i++)
1131 ulong tbl = table13190[i].a;
1132 func13190(i);
1133 if (1 + tbl)
1135 if (tbl == 0x80000)
1136 return;
1141 ////////////////////////////////////////////////////////////////////////
1143 double foo13485(double c, double d)
1145 // This must not be optimized to c += (d + d)
1146 c += d;
1147 c += d;
1148 return c;
1151 void test13485()
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)))
1165 case 1:
1167 break;
1169 default:
1171 assert(0);
1176 void test12833()
1178 test12833a(0x1_0000);
1181 /***********************************************/
1183 struct Point9449
1185 double f = 3.0;
1186 double g = 4.0;
1189 void test9449()
1191 Point9449[1] arr;
1192 if (arr[0].f != 3.0) assert(0);
1193 if (arr[0].g != 4.0) assert(0);
1196 struct Point9449x
1198 float f = 0.0;
1199 double g = 0.0;
1202 void test9449x()
1204 Point9449x[1] arr;
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; }
1214 void test12057()
1216 real fc = f12057(real.init);
1217 if (fc == 0 || fc.prop12057) {}
1221 ////////////////////////////////////////////////////////////////////////
1223 long modulo24 (long ticks)
1225 ticks %= 864000000000;
1226 if (ticks < 0)
1227 ticks += 864000000000;
1228 return ticks;
1231 void test13784()
1233 assert (modulo24(-141600000000) == 722400000000);
1237 ////////////////////////////////////////////////////////////////////////
1239 struct S13969 {
1240 int x, y;
1243 int test13969(const S13969* f) {
1244 return 0 % ((f.y > 0) ? f.x / f.y : f.x / -f.y);
1247 ////////////////////////////////////////////////////////////////////////
1249 int[] arr14436;
1250 void test14436()
1252 assert(arr14436 == null);
1253 arr14436 = [1, 2, 3];
1254 assert(arr14436 != null);
1257 ////////////////////////////////////////////////////////////////////////
1259 void test14220()
1261 auto a = toString(14);
1263 printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1264 return;
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
1279 mValue = div;
1280 } while (mValue);
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
1290 return a;
1293 ////////////////////////////////////////////////////////////////////////
1295 int stripLeft(int str, int dc)
1297 while (true)
1299 int a = str;
1300 int s = a;
1301 str += 1;
1302 if (dc) return s;
1306 void test14829()
1308 if (stripLeft(3, 1) != 3) // fails with -O
1309 assert(0);
1313 ////////////////////////////////////////////////////////////////////////
1315 void test3()
1317 int[6] a;
1318 int[] b;
1319 b = a;
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
1329 void test14782()
1331 static struct Foo
1333 long a = 8;
1334 int b = 7;
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 ////////////////////////////////////////////////////////////////////////
1346 void test14987()
1348 static struct Foo
1350 int b = 7;
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
1367 assert(bc == 1);
1368 return new void[1];
1371 void test15272()
1373 void[] scache = cast(void[])"abc";
1374 size_t count = 1;
1375 void[]* buckets = &scache;
1376 *buckets = calloc15272(count)[0 .. count];
1379 /*****************************************
1380 * https://issues.dlang.org/show_bug.cgi?id=15861
1383 void test15861()
1385 double val = 4286853117.;
1388 assert(val == 4286853117.);
1389 }();
1392 ////////////////////////////////////////////////////////////////////////
1394 // https://issues.dlang.org/show_bug.cgi?id=15629 comment 3
1395 // -O
1397 void test15629()
1399 int[] a = [3];
1400 int value = a[0] >= 0 ? a[0] : -a[0];
1401 assert(a[0] == 3);
1402 writeln(value, a);
1405 void writeln(int v, int[] a)
1409 ////////////////////////////////////////////////////////////////////////
1411 real binPosPow2() { return 1.0L; }
1413 real binPow2()
1415 return 1.0L/binPosPow2();
1418 void test4()
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;
1430 double c = 0.0;
1431 foreach(double x; [1, 1e100, 1, -1e100])
1433 x = multiply(x);
1434 double t = s + x;
1435 if(s.fabs >= x.fabs)
1437 double y = s-t;
1438 c += y+x;
1440 else
1442 double y = x-t;
1443 c += y+s;
1445 s = t;
1447 return s + c;
1450 double multiply(double a) { return a * 10000; }
1452 void test13474()
1454 double r = 20000;
1455 assert(r == sumKBN());
1458 ////////////////////////////////////////////////////////////////////////
1459 // https://issues.dlang.org/show_bug.cgi?id=16699
1461 ulong[1] parseDateRange()
1465 ulong[1] result;
1466 result[0] = 6;
1467 return result;
1469 finally
1474 void test16699()
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() { } }
1486 long[1] f16102()
1488 S16102 a;
1489 return [1];
1492 void test16102()
1494 assert( f16102() == [1] );
1497 ////////////////////////////////////////////////////////////////////////
1499 void test5a(ulong x, ulong y)
1501 int a;
1502 if (x >> 32)
1503 a = 1;
1504 else
1505 a = 2;
1506 assert(a == 1);
1508 if (y >> 32)
1509 a = 1;
1510 else
1511 a = 2;
1512 assert(a == 2);
1515 void test5()
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.
1527 int foo61(int i)
1529 return (i % 2 != 0) ? 4 : 2;
1532 int foo62(int i)
1534 return (i % 2 != 0) ? 2 : 4;
1537 bool bar6(bool b) { return b; }
1539 int foo63(bool b)
1541 return bar6(b) ? 16 : 8;
1544 int foo64(bool b)
1546 return bar6(b) ? 8 : 16;
1549 void test6()
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) {
1564 int ret;
1566 if (b==4)
1567 ret = 3;
1568 else
1569 ret = 5;
1571 if (ret == 4)
1572 return 0;
1573 else
1574 return 1;
1577 void testeqeqranges()
1579 int i = dataflow(4);
1580 if (i != 1)
1581 assert(0);
1584 ////////////////////////////////////////////////////////////////////////
1586 // https://issues.dlang.org/show_bug.cgi?id=16189
1588 void test16189()
1590 ubyte[9][1] data;
1591 uint a = 0;
1592 loop:
1593 data[0] = data[a];
1594 a--;
1595 bool b = false;
1596 if (b) goto loop;
1597 assert(a == -1); // was failing with -O
1601 ////////////////////////////////////////////////////////////////////////
1603 // https://issues.dlang.org/show_bug.cgi?id=16997
1605 void test16997()
1607 /* Exhaustively test all signed and unsigned byte promotions for
1608 * - + and ~
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);
1618 assert(i1 == i2);
1620 i1 = cast(int)(+c);
1621 i2 = cast(int)(+cast(int)c);
1622 assert(i1 == i2);
1624 i1 = cast(int)(-c);
1625 i2 = cast(int)(-cast(int)c);
1626 assert(i1 == i2);
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);
1637 assert(i1 == i2);
1639 i1 = cast(int)(+c);
1640 i2 = cast(int)(+cast(int)c);
1641 assert(i1 == i2);
1643 i1 = cast(int)(-c);
1644 i2 = cast(int)(-cast(int)c);
1645 assert(i1 == i2);
1649 ////////////////////////////////////////////////////////////////////////
1651 void test18315() // https://issues.dlang.org/show_bug.cgi?id=18315
1653 int i = int.min;
1654 bool b = i > 0;
1655 assert(!b);
1656 b = 0 < i;
1657 assert(!b);
1660 ////////////////////////////////////////////////////////////////////////
1662 // https://issues.dlang.org/show_bug.cgi?id=18461
1664 void test18461()
1666 import core.bitop;
1668 size_t test_val = 0b0001_0000;
1670 if (bt(&test_val, 4) == 0)
1671 assert(false);
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);
1710 ulong s = 1;
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. */
1715 version (Posix)
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
1737 ubyte[1024] data;
1738 ushort* ushortPtr = cast(ushort*) data.ptr;
1739 *ushortPtr++ = 0xfe00;
1740 printf("ushortPtr(%p)\n", ushortPtr);
1741 fflush(stdout);
1744 alias Seq(stuff ...) = stuff;
1745 static foreach (T; Seq!(ubyte, ushort, uint, ulong, byte, short, int, long))
1747 T[2] data = 0x2A;
1748 T* q = &data[0];
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"];
1763 int[2] tgt = 0x2a;
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)
1776 int bitIdx = 0;
1777 func18794();
1778 return (*p & (1UL << bitIdx)) != 0;
1781 void func18794() {}
1783 void prep18794()
1785 asm {}
1786 ulong[2] x = -1;
1789 void test18794()
1791 prep18794();
1792 size_t s;
1793 method18794(&s);
1796 ////////////////////////////////////////////////////////////////////////
1798 /* Test the optimization
1799 * (e1+c)-e2 => (e1-e2)+c
1802 void testelmin()
1804 static void foo(int i)
1806 static ubyte[4] bar()
1808 ubyte[4] array;
1809 foreach (i, ref a; array)
1810 a = cast(ubyte)(i + 1);
1811 return array;
1814 static void test(int i, ubyte* p)
1816 foreach (j; 0 .. 4)
1817 assert(p[i * 4 + j] == j + 1);
1820 ubyte[32] data;
1821 data[i*4..(i+1)*4] = bar(); // optimize to single MOV
1823 test(i, data.ptr);
1826 foo(4);
1829 ////////////////////////////////////////////////////////////////////////
1831 const(char)* fastpar(string s)
1833 return s.ptr + s.length;
1836 void testfastpar()
1838 string s = "abcde";
1839 auto p = fastpar(s);
1840 assert(*p == 0);
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;
1852 void test20363()
1854 ulong u = 0b10;
1855 if (foo20363(*cast(double*) &u) == 0)
1856 assert(false);
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;
1872 void testNegConst()
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)
1886 s += 8;
1887 return 3;
1890 int binAdd(int val)
1892 val = val + add8ret3(val);
1893 return val;
1896 void test16317()
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()
1912 ++test20050_g;
1913 test20050_impure_function_1();
1915 return &impure_function_2;
1917 void test20050()
1919 auto f = test20050_get_impure_function();
1920 f();
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
1932 if (y)
1933 return x;
1934 return x + 3;
1937 void testCpStatic()
1939 assert(testCpStatic1(1) == 7);
1940 assert(testCpStatic1(0) == 10);
1943 ////////////////////////////////////////////////////////////////////////
1944 // https://issues.dlang.org/show_bug.cgi?id=20991
1946 int x7;
1948 void bar7(int i)
1950 assert(i == x7);
1951 ++x7;
1954 void test7()
1956 for (int i = 0; i <= 1; ++i)
1957 bar7(i);
1958 assert(x7 == 2);
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));
1971 void testbyteswap()
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
1981 // instructions.
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));
2003 void testrolror()
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
2016 void test20162()
2018 static long f(long a)
2020 assert(a == -1L);
2021 return a;
2024 foreach (i; 1 .. 2)
2026 foreach (j; 0 .. 2)
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
2038 int star1(int i)
2040 return i ? star1(i - 1) : 0;
2043 int star2(int i)
2045 return i == 0 ? 0 : star2(i - 1);
2048 int star3(int i)
2050 if (i == 0)
2051 return 0;
2052 return i == 2 ? star3(i - 2) : star3(i - 1);
2055 int star4(int i)
2057 return (i == 0) ? 0
2058 : i != 2 ? star4(i - 1)
2059 : star4(i - 2);
2062 void test3713()
2064 assert(star1(10) == 0);
2065 assert(star2(10) == 0);
2066 assert(star3(10) == 0);
2067 assert(star4(10) == 0);
2070 ////////////////////////////////////////////////////////////////////////
2072 void testsbbrex()
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;
2101 Void print19846()
2103 //printf("This should print\n");
2104 x19846 = 3;
2105 return VOID;
2108 Void identity19846(Void value, out int i)
2110 i = 7;
2111 return value;
2114 void test19846()
2116 int i;
2117 identity19846(print19846(), i);
2118 //printf("i = %d\n", i);
2119 assert(x19846 == 3);
2120 assert(i == 7);
2123 ////////////////////////////////////////////////////////////////////////
2125 // Some tests for OPmemcpy
2127 enum N = 128;
2129 ubyte[N] def()
2131 ubyte[N] array;
2132 foreach (i, ref a; array)
2133 a = cast(ubyte)(i + 1);
2134 return array;
2138 void ghi(ubyte* p)
2140 foreach (i; 0 .. N)
2141 assert(p[i] == i + 1);
2144 void testmemcpy()
2146 ubyte[N] bits;
2147 ubyte[N] bits2;
2148 bits2[0..N] = bits[0..N] = def();
2149 ghi(bits.ptr);
2150 ghi(bits2.ptr);
2152 __gshared size_t n = N;
2153 ubyte[N] bits3;
2154 ubyte[N] bits4;
2155 bits4[0..n] = bits3[0..n] = def();
2156 ghi(bits3.ptr);
2157 ghi(bits4.ptr);
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()
2170 return x + y;
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);
2179 void testMulLea()
2181 testleax!3(10,11);
2182 testleax!5(10,11);
2183 testleax!6(10,11);
2184 testleax!9(10,11);
2186 testleax!10(10,11);
2187 testleax!12(10,11);
2188 testleax!18(10,11);
2189 testleax!20(10,11);
2190 testleax!24(10,11);
2191 testleax!36(10,11);
2192 testleax!40(10,11);
2193 testleax!72(10,11);
2195 testleax!37(10,11);
2196 testleax!74(10,11);
2197 testleax!13(10,11);
2198 testleax!26(10,11);
2201 ////////////////////////////////////////////////////////////////////////
2203 /* Test *= of register pair
2206 void testMulAssPair()
2208 static ulong pow(ulong x, int m)
2210 ulong v = x;
2211 ulong p = 1;
2212 while (1)
2214 if (m & 1)
2215 p *= v;
2216 m >>= 1;
2217 if (!m)
2218 break;
2219 v *= v;
2221 return p;
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";
2234 void test21038()
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
2250 ulong x = 0uL;
2251 while(true)
2253 const y = f21325(x); // should set y to real.nan
2255 assert(y != y);
2257 if (++x)
2258 return; // good
2262 ////////////////////////////////////////////////////////////////////////
2263 // https://issues.dlang.org/show_bug.cgi?id=16274
2265 extern(C) int pair(short a, ushort b, byte c, ubyte d);
2267 struct S
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);
2274 assert(a == -1);
2275 assert(b == 2);
2276 assert(c == -3);
2277 assert(d == 4);
2281 void test16274()
2283 version (X86_64)
2284 pair(-1, 2, -3, 4);
2285 version (X86)
2286 pair(-1, 2, -3, 4);
2289 ////////////////////////////////////////////////////////////////////////
2290 // https://issues.dlang.org/show_bug.cgi?id=16268
2292 void test16268()
2294 static void f(byte x)
2296 for (byte i = 0; i <= x && i >= 0; ++i)
2298 assert(i >= 0);
2299 assert(i != -1);
2300 //printf("%d\n", i);
2304 f(byte.max);
2307 ////////////////////////////////////////////////////////////////////////
2308 // https://issues.dlang.org/show_bug.cgi?id=11435
2310 void test11435a()
2312 alias T = byte;
2314 static void fun(T c, T b, int v)
2318 static void abc(T[] b)
2320 fun(b[0], b[1], 0);
2323 version(Windows)
2325 import core.sys.windows.windows;
2326 auto p = VirtualAlloc(null, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2328 else
2330 import core.sys.posix.sys.mman;
2331 auto p = mmap(null, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0L);
2333 assert(p);
2334 auto px = (cast(T*)(p + 4096 - 2 * T.sizeof));
2335 abc(px[0..2]);
2338 void test11435b()
2340 import core.sys.windows.windows;
2341 alias T = short;
2343 static void fun(T c, T b, int v)
2347 static void abc(T[] b)
2349 fun(b[0], b[1], 0);
2352 version(Windows)
2354 import core.sys.windows.windows;
2355 auto p = VirtualAlloc(null, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2357 else
2359 import core.sys.posix.sys.mman;
2360 auto p = mmap(null, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0L);
2362 assert(p);
2363 auto px = (cast(T*)(p + 4096 - 2 * T.sizeof));
2364 abc(px[0..2]);
2367 ////////////////////////////////////////////////////////////////////////
2368 // https://issues.dlang.org/show_bug.cgi?id=21513
2370 struct Stuff
2372 size_t c; // declare after items and not crash !
2373 ubyte[1] items;
2376 void grow(ref Stuff stuff)
2378 with (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 !
2386 void test21513()
2388 Stuff stuff;
2389 grow(stuff);
2392 ////////////////////////////////////////////////////////////////////////
2393 // https://issues.dlang.org/show_bug.cgi?id=21526
2395 double f21256(double a, double b) {
2396 double c = a + b;
2397 return c;
2400 void test21256()
2402 union DX
2404 double d;
2405 ulong l;
2408 DX a, b;
2409 a.l = 0x4341c37937e08000;
2410 b.l = 0x4007ffcb923a29c7;
2412 DX r;
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)
2428 return cast(bool)t;
2431 void test21816()
2433 assert(test21816a(float.nan));
2436 ////////////////////////////////////////////////////////////////////////
2437 // https://issues.dlang.org/show_bug.cgi?id=21835
2439 struct Point21835
2441 float f = 3.0;
2442 double d = 4.0;
2443 real r = 5.0;
2446 void test21835y()
2448 Point21835[1] arr;
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);
2454 struct Point21835x
2456 float f = 0.0;
2457 double d = 0.0;
2458 real r = 0.0;
2461 void test21835()
2463 test21835y();
2464 Point21835x[1] arr;
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 ////////////////////////////////////////////////////////////////////////
2472 int main()
2474 // All the various integer divide tests
2475 testsdiv2();
2476 testulldiv();
2477 testfastudiv();
2478 testsldiv();
2479 testslmod();
2480 testfastdiv();
2481 testdivdiv();
2482 testdivcmp();
2484 testgoto();
2485 testswitch();
2486 testdo();
2487 testbreak();
2488 teststringswitch();
2489 teststrarg();
2490 test12164();
2491 testsizes();
2492 testarrayinit();
2493 testU();
2494 testbittest();
2495 test8658();
2496 test3918();
2497 test12051();
2498 testdocond();
2499 testnegcom();
2500 test11565();
2501 testoror();
2502 testbt();
2503 test12095(0);
2504 testandand();
2505 testor_combine();
2506 testshrshl();
2507 test13383();
2508 test13190();
2509 test13485();
2510 test14436();
2511 test10715();
2512 test10678();
2513 test7565();
2514 test13023(0x10_0000_0000);
2515 test12833();
2516 test9449();
2517 test9449x();
2518 test12057();
2519 test13784();
2520 test14220();
2521 test14829();
2522 test3();
2523 test14782();
2524 test14987();
2525 test15272();
2526 test15861();
2527 test15629();
2528 test4();
2529 test13474();
2530 test16699();
2531 test16102();
2532 test5();
2533 test6();
2534 testeqeqranges();
2535 test16189();
2536 test16997();
2537 test18315();
2538 test18461();
2539 test18730();
2540 test19497();
2541 test18794();
2542 testelmin();
2543 testfastpar();
2544 test20363();
2545 testNegConst();
2546 test16317();
2547 test20050();
2548 testCpStatic();
2549 test7();
2550 testbyteswap();
2551 testrolror();
2552 test20162();
2553 test3713();
2554 testsbbrex();
2555 test19846();
2556 testmemcpy();
2557 testMulLea();
2558 testMulAssPair();
2559 test21038();
2560 test21325();
2561 test16274();
2562 test16268();
2563 test11435a();
2564 test11435b();
2565 test21513();
2566 test21256();
2567 test21816();
2568 test21835();
2570 printf("Success\n");
2571 return 0;