Debian Codegraph @

gcc/testsuite/gdc.test/runnable/mars1.d

raw
1 /*
2 REQUIRED_ARGS: -mcpu=native
3 PERMUTE_ARGS: -O -inline -release
4 */
5
6 import core.stdc.stdio;
7
8 template tuple(A...) { alias tuple = A; }
9
10 ///////////////////////
11
12 // https://github.com/dlang/dmd/pull/11441
13
14 long sdiv1(long l)
15 {
16 return l / 2;
17 }
18
19 int sdiv2(int i)
20 {
21 return i / 2;
22 }
23
24 void testsdiv2()
25 {
26 assert(sdiv1(10) == 5);
27 assert(sdiv1(-10) == -5);
28 assert(sdiv2(10) == 5);
29 assert(sdiv2(-10) == -5);
30 }
31
32 ///////////////////////
33
34 void testulldiv()
35 {
36 __gshared ulong[4][] vectors =
37 [
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],
54 ];
55
56 for (size_t i = 0; i < vectors.length; i++)
57 {
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]);
62
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]);
67 }
68 }
69
70 ////////////////////////////////////////////////////////////////////////
71
72 uint udiv10(uint x)
73 {
74 return x / 10;
75 }
76
77 uint udiv14(uint x)
78 {
79 return x / 14;
80 }
81
82 uint udiv14007(uint x)
83 {
84 return x / 14007;
85 }
86
87 uint umod10(uint x)
88 {
89 return x % 10;
90 }
91
92 uint umod14(uint x)
93 {
94 return x % 14;
95 }
96
97 uint umod14007(uint x)
98 {
99 return x % 14007;
100 }
101
102 uint uremquo10(uint x)
103 {
104 return (x / 10) | (x % 10);
105 }
106
107 uint uremquo14(uint x)
108 {
109 return (x / 14) | (x % 14);
110 }
111
112 uint uremquo14007(uint x)
113 {
114 return (x / 14007) | (x % 14007);
115 }
116
117
118
119 ulong uldiv10(ulong x)
120 {
121 return x / 10;
122 }
123
124 ulong uldiv14(ulong x)
125 {
126 return x / 14;
127 }
128
129 ulong uldiv14007(ulong x)
130 {
131 return x / 14007;
132 }
133
134 ulong ulmod10(ulong x)
135 {
136 return x % 10;
137 }
138
139 ulong ulmod14(ulong x)
140 {
141 return x % 14;
142 }
143
144 ulong ulmod14007(ulong x)
145 {
146 return x % 14007;
147 }
148
149 ulong ulremquo10(ulong x)
150 {
151 return (x / 10) | (x % 10);
152 }
153
154 ulong ulremquo14(ulong x)
155 {
156 return (x / 14) | (x % 14);
157 }
158
159 ulong ulremquo14007(ulong x)
160 {
161 return (x / 14007) | (x % 14007);
162 }
163
164
165 void testfastudiv()
166 {
167 {
168 static uint x10 = 10;
169 static uint x14 = 14;
170 static uint x14007 = 14007;
171
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)));
183 }
184 {
185 static ulong y10 = 10;
186 static ulong y14 = 14;
187 static ulong y14007 = 14007;
188
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)));
200 }
201 }
202
203
204 ////////////////////////////////////////////////////////////////////////
205
206 // https://issues.dlang.org/show_bug.cgi?id=14936
207
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); }
221
222 void testsldiv()
223 {
224 /* Test special div code for signed long divide
225 * by power of 2 for 32 bit targets.
226 */
227
228 // printf("63 = %llx\n", sldiv63(-0x7FFF_F8FF_FF3F_2FFFL));
229
230 static foreach (C; tuple!(
231 1,2,3,10,300,1000,
232 4_1001_2030_0030,
233 0x7FFF_F8FF_FF3F_2FFFL))
234 {
235 /* Check if runtime computation matches compile time
236 */
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));
249
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));
264 }
265 }
266
267 ////////////////////////////////////////////////////////////////////////
268
269 // https://issues.dlang.org/show_bug.cgi?id=14936
270
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); }
284
285 void testslmod()
286 {
287 static foreach (C; tuple!(
288 1,2,3,10,300,1000,
289 4_1001_2030_0030,
290 0x7FFF_F8FF_FF3F_2FFFL))
291 {
292 /* Check if runtime computation matches compile time
293 */
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));
306
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));
321 }
322 }
323
324 ////////////////////////////////////////////////////////////////////////
325
326 T divC(int C, T)(T x)
327 {
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;
335 }
336
337 T modC(int C, T)(T x)
338 {
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;
346 }
347
348 T remquoC(int C, T)(T x)
349 {
350 return (x / C) | (x % C);
351 }
352
353 void testfastdiv()
354 {
355 static int z = 0; // prevent constant folding by optimizer
356
357 static foreach (T; tuple!(int, long, uint, ulong))
358 {{
359 T u = 10000;
360 T r;
361 static foreach (C; tuple!(10, 14, 14007, -10, -14, -14007))
362 {
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)))));
366 }
367 }}
368 }
369
370 ////////////////////////////////////////////////////////////////////////
371
372
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.
376 */
377
378 void divdiv(T, T C1, T C2)(T i)
379 {
380 auto a = (i / C1) / C2;
381 auto b = i / (C1 * C2);
382 if (a != b) assert(0);
383 }
384
385 void testdivdiv()
386 {
387 divdiv!(int,10,20)(30);
388 divdiv!(uint,10,20)(30);
389 divdiv!(long,10,20)(30);
390 divdiv!(ulong,10,20)(30);
391
392 divdiv!(int,-10,20)(30);
393 divdiv!(long,-10,20)(30);
394
395 divdiv!(int,-10,-20)(-30);
396 divdiv!(long,-10,-20)(-30);
397 }
398
399 ////////////////////////////////////////////////////////////////////////
400
401 void testdivcmp()
402 {
403 // https://github.com/dlang/dmd/pull/7128
404 static bool foo(uint a, uint b)
405 {
406 return cast(bool)(a / b); // convert / to >=
407 }
408
409 assert(!foo(3, 4));
410 assert(foo(4, 4));
411 assert(foo(5, 4));
412 }
413
414 /////////////////////////////////////////////////////
415
416 void testgoto()
417 {
418 int i;
419
420 i = 3;
421 goto L4;
422 L3: i++;
423 goto L5;
424 L4: goto L3;
425 L5: assert(i == 4);
426 }
427
428 int testswitch()
429 {
430 int i;
431
432 i = 3;
433 switch (i)
434 {
435 case 0:
436 case 1:
437 default:
438 assert(0);
439 case 3:
440 break;
441 }
442 return 0;
443 }
444
445 void testdo()
446 {
447 int x = 0;
448
449 do
450 {
451 x++;
452 } while (x < 10);
453 printf("x == %d\n", x);
454 assert(x == 10);
455 }
456
457
458 void testbreak()
459 { int i, j;
460
461 Louter:
462 for (i = 0; i < 10; i++)
463 {
464 for (j = 0; j < 10; j++)
465 {
466 if (j == 3)
467 break Louter;
468 }
469 }
470
471 printf("i = %d, j = %d\n", i, j);
472 assert(i == 0);
473 assert(j == 3);
474 }
475
476 ///////////////////////
477
478 int foo(string s)
479 {
480 int i;
481
482 i = 0;
483 switch (s)
484 {
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;
497 }
498 return i;
499 }
500
501
502 void teststringswitch()
503 { int i;
504
505 i = foo("hello");
506 printf("i = %d\n", i);
507 assert(i == 1);
508
509 i = foo("goodbye");
510 printf("i = %d\n", i);
511 assert(i == 2);
512
513 i = foo("goodb");
514 printf("i = %d\n", i);
515 assert(i == 3);
516
517 i = foo("huzzah");
518 printf("i = %d\n", i);
519 assert(i == 10);
520 }
521
522
523 ///////////////////////
524
525 struct Foo
526 {
527 int a;
528 char b;
529 long c;
530 }
531
532 Foo test(Foo f)
533 {
534 f.a += 1;
535 f.b += 3;
536 f.c += 4;
537 return f;
538 }
539
540
541 void teststrarg()
542 {
543 Foo g;
544 g.a = 1;
545 g.b = 2;
546 g.c = 3;
547
548 Foo q;
549 q = test(g);
550 assert(q.a == 2);
551 assert(q.b == 5);
552 assert(q.c == 7);
553 }
554
555 ///////////////////////
556
557 align (1) struct Foo1
558 {
559 align (1):
560 int a;
561 char b;
562 long c;
563 }
564
565 struct Foo2
566 {
567 int a;
568 char b;
569 long c;
570 }
571
572 struct Foo3
573 {
574 int a;
575 align (1) char b;
576 long c;
577 }
578
579 struct Foo4
580 {
581 int a;
582 struct { char b; }
583 long c;
584 }
585
586 void testsizes()
587 {
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);
593
594 assert(Foo2.a.offsetof == 0);
595 assert(Foo2.b.offsetof == 4);
596 assert(Foo2.c.offsetof == 8);
597 assert(Foo2.sizeof == 16);
598
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);
604
605 assert(Foo4.sizeof == 16);
606 }
607
608 ///////////////////////
609
610 size_t cond11565(size_t val)
611 {
612 return val ? size_t.max : 0;
613 }
614
615 void test11565()
616 {
617 assert(cond11565(true) == size_t.max);
618 }
619
620 // https://issues.dlang.org/show_bug.cgi?id=23743
621 void test23743()
622 {
623 ubyte[] a = [1];
624 foreach (x; a)
625 {
626 ubyte v = x >= 1 ? 255 : 0;
627 assert(v == 255);
628 }
629 }
630
631 ///////////////////////
632
633 int[3] array1 = [1:1,2,0:3];
634
635 void testarrayinit()
636 {
637 assert(array1[0] == 3);
638 assert(array1[1] == 1);
639 assert(array1[2] == 2);
640 }
641
642 ///////////////////////
643
644 void test13023(ulong n)
645 {
646 static void func(bool b) {}
647
648 ulong k = 0;
649
650 func(k >= n / 2);
651
652 if (k >= n / 2)
653 assert(0);
654 }
655
656 ///////////////////////
657
658 struct U { int a; union { char c; int d; } long b; }
659
660 U f = { b:3, d:0x22222222, a:1 };
661
662 void testU()
663 {
664 assert(f.b == 3);
665 assert(f.d == 0x22222222);
666 assert(f.c == 0x22);
667 assert(f.a == 1);
668 assert(f.sizeof == 16);
669 assert(U.sizeof == 16);
670 }
671
672
673 ////////////////////////////////////////////////////////////////////////
674
675 void vfunc() {}
676
677 void test12095(int k)
678 {
679 int e = 0;
680 e ? k || assert(0) : !e || vfunc();
681 e ? k || assert(0) : e && vfunc();
682 !e ? !e || vfunc() : k || assert(0);
683 }
684
685
686 ////////////////////////////////////////////////////////////////////////
687
688
689 bool test3918a( float t, real u )
690 {
691 printf("%Lf\n", u );
692 return t && u;
693 }
694
695 bool test3918b( real t, float u )
696 {
697 printf("%Lf\n", t );
698 return t && u;
699 }
700
701 void test3918()
702 {
703 assert(test3918a(float.nan, real.nan));
704 assert(test3918b(real.nan, float.nan));
705 }
706
707 ////////////////////////////////////////////////////////////////////////
708
709 T docond1(T)(T l, ubyte thresh, ubyte val) {
710 l += (thresh < val);
711 return l;
712 }
713
714 T docond2(T)(T l, ubyte thresh, ubyte val) {
715 l -= (thresh >= val);
716 return l;
717 }
718
719 T docond3(T)(T l, ubyte thresh, ubyte val) {
720 l += (thresh >= val);
721 return l;
722 }
723
724 T docond4(T)(T l, ubyte thresh, ubyte val) {
725 l -= (thresh < val);
726 return l;
727 }
728
729 void testdocond()
730 {
731 assert(docond1!ubyte(10,3,5) == 11);
732 assert(docond1!ushort(10,3,5) == 11);
733 assert(docond1!uint(10,3,5) == 11);
734 assert(docond1!ulong(10,3,5) == 11);
735
736 assert(docond2!ubyte(10,3,5) == 10);
737 assert(docond2!ushort(10,3,5) == 10);
738 assert(docond2!uint(10,3,5) == 10);
739 assert(docond2!ulong(10,3,5) == 10);
740
741 assert(docond3!ubyte(10,3,5) == 10);
742 assert(docond3!ushort(10,3,5) == 10);
743 assert(docond3!uint(10,3,5) == 10);
744 assert(docond3!ulong(10,3,5) == 10);
745
746 assert(docond4!ubyte(10,3,5) == 9);
747 assert(docond4!ushort(10,3,5) == 9);
748 assert(docond4!uint(10,3,5) == 9);
749 assert(docond4!ulong(10,3,5) == 9);
750
751
752 assert(docond1!ubyte(10,5,3) == 10);
753 assert(docond1!ushort(10,5,3) == 10);
754 assert(docond1!uint(10,5,3) == 10);
755 assert(docond1!ulong(10,5,3) == 10);
756
757 assert(docond2!ubyte(10,5,3) == 9);
758 assert(docond2!ushort(10,5,3) == 9);
759 assert(docond2!uint(10,5,3) == 9);
760 assert(docond2!ulong(10,5,3) == 9);
761
762 assert(docond3!ubyte(10,5,3) == 11);
763 assert(docond3!ushort(10,5,3) == 11);
764 assert(docond3!uint(10,5,3) == 11);
765 assert(docond3!ulong(10,5,3) == 11);
766
767 assert(docond4!ubyte(10,5,3) == 10);
768 assert(docond4!ushort(10,5,3) == 10);
769 assert(docond4!uint(10,5,3) == 10);
770 assert(docond4!ulong(10,5,3) == 10);
771 }
772
773 ////////////////////////////////////////////////////////////////////////
774
775 struct S8658
776 {
777 int[16385] a;
778 }
779
780 void foo8658(S8658 s)
781 {
782 int x;
783 }
784
785 void test8658()
786 {
787 S8658 s;
788 for(int i = 0; i < 1000; i++)
789 foo8658(s);
790 }
791
792 ////////////////////////////////////////////////////////////////////////
793
794 uint neg(uint i)
795 {
796 return ~i + 1;
797 }
798
799 uint com(uint i)
800 {
801 return -i - 1;
802 }
803
804 float com(float i)
805 {
806 return -i - 1;
807 }
808
809 uint com2(uint i)
810 {
811 return -(i + 1);
812 }
813
814 void testnegcom()
815 {
816 assert(neg(3) == -3);
817 assert(com(3) == -4);
818 assert(com(3.0f) == -4.0f);
819 assert(com2(3) == -4);
820 }
821
822 ////////////////////////////////////////////////////////////////////////
823
824 int oror1(char c)
825 {
826 return ((((((((((cast(int) c <= 32 || cast(int) c == 46) || cast(int) c == 44)
827 || cast(int) c == 58) || cast(int) c == 59) || cast(int) c == 60)
828 || cast(int) c == 62) || cast(int) c == 34) || cast(int) c == 92)
829 || cast(int) c == 39) != 0);
830 }
831
832 int oror2(char c)
833 {
834 return ((((((((((c <= 32 || c == 46) || c == 44)
835 || c == 58) || c == 59) || c == 60)
836 || c == 62) || c == 34) || c == 92)
837 || c == 39) != 0);
838 }
839
840 void testoror()
841 {
842 assert(oror1(0) == 1);
843 assert(oror1(32) == 1);
844 assert(oror1(46) == 1);
845 assert(oror1(44) == 1);
846 assert(oror1(58) == 1);
847 assert(oror1(59) == 1);
848 assert(oror1(60) == 1);
849 assert(oror1(62) == 1);
850 assert(oror1(34) == 1);
851 assert(oror1(92) == 1);
852 assert(oror1(39) == 1);
853 assert(oror1(33) == 0);
854 assert(oror1(61) == 0);
855 assert(oror1(93) == 0);
856 assert(oror1(255) == 0);
857
858 assert(oror2(0) == 1);
859 assert(oror2(32) == 1);
860 assert(oror2(46) == 1);
861 assert(oror2(44) == 1);
862 assert(oror2(58) == 1);
863 assert(oror2(59) == 1);
864 assert(oror2(60) == 1);
865 assert(oror2(62) == 1);
866 assert(oror2(34) == 1);
867 assert(oror2(92) == 1);
868 assert(oror2(39) == 1);
869 assert(oror2(33) == 0);
870 assert(oror2(61) == 0);
871 assert(oror2(93) == 0);
872 assert(oror2(255) == 0);
873 }
874
875 ////////////////////////////////////////////////////////////////////////
876
877 bool bt1(int p, int a, int b)
878 {
879 return p && ((1 << b) & a);
880 }
881
882 bool bt2(int p, long a, long b)
883 {
884 return p && ((1L << b) & a);
885 }
886
887 void testbt()
888 {
889 assert(bt1(1,7,2) == 1);
890 assert(bt1(1,7,3) == 0);
891
892 assert(bt2(1,0x7_0000_0000,2+32) == 1);
893 assert(bt2(1,0x7_0000_0000,3+32) == 0);
894 }
895
896 ////////////////////////////////////////////////////////////////////////
897
898 void test13383()
899 {
900 foreach (k; 32..33)
901 {
902 if (1L & (1L << k))
903 {
904 assert(0);
905 }
906 }
907 }
908
909 ////////////////////////////////////////////////////////////////////////
910
911 int andand1(int c)
912 {
913 return (c > 32 && c != 46 && c != 44
914 && c != 58 && c != 59
915 && c != 60 && c != 62
916 && c != 34 && c != 92
917 && c != 39) != 0;
918 }
919
920 bool andand2(long c)
921 {
922 return (c > 32 && c != 46 && c != 44
923 && c != 58 && c != 59
924 && c != 60 && c != 62
925 && c != 34 && c != 92
926 && c != 39) != 0;
927 }
928
929 int foox3() { return 1; }
930
931 int andand3(uint op)
932 {
933 if (foox3() &&
934 op != 7 &&
935 op != 3 &&
936 op != 18 &&
937 op != 30 &&
938 foox3())
939 return 3;
940 return 4;
941 }
942
943
944 void testandand()
945 {
946 assert(andand1(0) == 0);
947 assert(andand1(32) == 0);
948 assert(andand1(46) == 0);
949 assert(andand1(44) == 0);
950 assert(andand1(58) == 0);
951 assert(andand1(59) == 0);
952 assert(andand1(60) == 0);
953 assert(andand1(62) == 0);
954 assert(andand1(34) == 0);
955 assert(andand1(92) == 0);
956 assert(andand1(39) == 0);
957 assert(andand1(33) == 1);
958 assert(andand1(61) == 1);
959 assert(andand1(93) == 1);
960 assert(andand1(255) == 1);
961
962 assert(andand2(0) == false);
963 assert(andand2(32) == false);
964 assert(andand2(46) == false);
965 assert(andand2(44) == false);
966 assert(andand2(58) == false);
967 assert(andand2(59) == false);
968 assert(andand2(60) == false);
969 assert(andand2(62) == false);
970 assert(andand2(34) == false);
971 assert(andand2(92) == false);
972 assert(andand2(39) == false);
973 assert(andand2(33) == true);
974 assert(andand2(61) == true);
975 assert(andand2(93) == true);
976 assert(andand2(255) == true);
977
978 assert(andand3(6) == 3);
979 assert(andand3(30) == 4);
980 }
981
982 ////////////////////////////////////////////////////////////////////////
983
984 bool bittest11508(char c)
985 {
986 return c=='_' || c=='-' || c=='+' || c=='.';
987 }
988
989 void testbittest()
990 {
991 assert(bittest11508('_'));
992 }
993
994 ////////////////////////////////////////////////////////////////////////
995
996 uint or1(ubyte x)
997 {
998 return x | (x<<8) | (x<<16) | (x<<24) | (x * 3);
999 }
1000
1001 void testor_combine()
1002 {
1003 printf("%x\n", or1(1));
1004 assert(or1(5) == 5 * (0x1010101 | 3));
1005 }
1006
1007 ////////////////////////////////////////////////////////////////////////
1008
1009
1010 int shrshl(int i) {
1011 return ((i+1)>>1)<<1;
1012 }
1013
1014 void testshrshl()
1015 {
1016 assert(shrshl(6) == 6);
1017 assert(shrshl(7) == 8);
1018 }
1019
1020 ////////////////////////////////////////////////////////////////////////
1021
1022 bool bt10715(in uint[] ary, size_t bitnum)
1023 {
1024 return !!(ary[bitnum >> 5] & 1 << (bitnum & 31)); // uses bt
1025 }
1026
1027 bool neg_bt10715(in uint[] ary, size_t bitnum)
1028 {
1029 return !(ary[bitnum >> 5] & 1 << (bitnum & 31)); // does not use bt
1030 }
1031
1032 void test10715()
1033 {
1034 static uint[2] a1 = [0x1001_1100, 0x0220_0012];
1035
1036 if ( bt10715(a1,30)) assert(0);
1037 if (!bt10715(a1,8)) assert(0);
1038 if ( bt10715(a1,30+32)) assert(0);
1039 if (!bt10715(a1,1+32)) assert(0);
1040
1041 if (!neg_bt10715(a1,30)) assert(0);
1042 if ( neg_bt10715(a1,8)) assert(0);
1043 if (!neg_bt10715(a1,30+32)) assert(0);
1044 if ( neg_bt10715(a1,1+32)) assert(0);
1045 }
1046
1047 ////////////////////////////////////////////////////////////////////////
1048
1049 ptrdiff_t compare12164(A12164* rhsPA, A12164* zis)
1050 {
1051 if (*rhsPA == *zis)
1052 return 0;
1053 return ptrdiff_t.min;
1054 }
1055
1056 struct A12164
1057 {
1058 int a;
1059 }
1060
1061 void test12164()
1062 {
1063 auto a = A12164(3);
1064 auto b = A12164(2);
1065 assert(compare12164(&a, &b));
1066 }
1067
1068 ////////////////////////////////////////////////////////////////////////
1069
1070 int foo10678(char[5] txt)
1071 {
1072 return txt[0] + txt[1] + txt[4];
1073 }
1074
1075 void test10678()
1076 {
1077 char[5] hello = void;
1078 hello[0] = 8;
1079 hello[1] = 9;
1080 hello[4] = 10;
1081 int i = foo10678(hello);
1082 assert(i == 27);
1083 }
1084
1085 ////////////////////////////////////////////////////////////////////////
1086
1087 struct S12051
1088 {
1089 this(char c)
1090 {
1091 assert(c == 'P' || c == 'M');
1092 }
1093 }
1094
1095 void test12051()
1096 {
1097 auto ip = ["abc"];
1098 foreach (i, s; ip)
1099 {
1100 S12051(i < ip.length ? 'P' : 'M');
1101 }
1102 }
1103
1104 ////////////////////////////////////////////////////////////////////////
1105
1106 void bug7565( double x) { assert(x == 3); }
1107
1108 void test7565()
1109 {
1110 double y = 3;
1111 bug7565( y++ );
1112 assert(y == 4);
1113 }
1114
1115 ////////////////////////////////////////////////////////////////////////
1116
1117 int bug8525(int[] devt)
1118 {
1119 return devt[$ - 1];
1120 }
1121
1122 ////////////////////////////////////////////////////////////////////////
1123
1124 void func13190(int) {}
1125
1126 struct Struct13190
1127 {
1128 ulong a;
1129 uint b;
1130 };
1131
1132 __gshared Struct13190* table13190 =
1133 [
1134 Struct13190(1, 1),
1135 Struct13190(0, 2)
1136 ];
1137
1138 void test13190()
1139 {
1140 for (int i = 0; table13190[i].a; i++)
1141 {
1142 ulong tbl = table13190[i].a;
1143 func13190(i);
1144 if (1 + tbl)
1145 {
1146 if (tbl == 0x80000)
1147 return;
1148 }
1149 }
1150 }
1151
1152 ////////////////////////////////////////////////////////////////////////
1153
1154 double foo13485(double c, double d)
1155 {
1156 // This must not be optimized to c += (d + d)
1157 c += d;
1158 c += d;
1159 return c;
1160 }
1161
1162 void test13485()
1163 {
1164 enum double d = 0X1P+1023;
1165 assert(foo13485(-d, d) == d);
1166 }
1167
1168 ////////////////////////////////////////////////////////////////////////
1169
1170 void test12833a(int a)
1171 {
1172 long x = cast(long)a;
1173
1174 switch (cast(int)(cast(ushort)(x >> 16 & 65535L)))
1175 {
1176 case 1:
1177 {
1178 break;
1179 }
1180 default:
1181 {
1182 assert(0);
1183 }
1184 }
1185 }
1186
1187 void test12833()
1188 {
1189 test12833a(0x1_0000);
1190 }
1191
1192 /***********************************************/
1193
1194 struct Point9449
1195 {
1196 double f = 3.0;
1197 double g = 4.0;
1198 }
1199
1200 void test9449()
1201 {
1202 Point9449[1] arr;
1203 if (arr[0].f != 3.0) assert(0);
1204 if (arr[0].g != 4.0) assert(0);
1205 }
1206
1207 struct Point9449x
1208 {
1209 float f = 0.0;
1210 double g = 0.0;
1211 }
1212
1213 void test9449x()
1214 {
1215 Point9449x[1] arr;
1216 if (arr[0].f != 0.0) assert(0);
1217 if (arr[0].g != 0.0) assert(0);
1218 }
1219
1220 ////////////////////////////////////////////////////////////////////////
1221 // https://issues.dlang.org/show_bug.cgi?id=12057
1222
1223 bool prop12057(real x) { return false; }
1224 double f12057(real) { return double.init; }
1225 void test12057()
1226 {
1227 real fc = f12057(real.init);
1228 if (fc == 0 || fc.prop12057) {}
1229 }
1230
1231
1232 ////////////////////////////////////////////////////////////////////////
1233
1234 long modulo24 (long ticks)
1235 {
1236 ticks %= 864000000000;
1237 if (ticks < 0)
1238 ticks += 864000000000;
1239 return ticks;
1240 }
1241
1242 void test13784()
1243 {
1244 assert (modulo24(-141600000000) == 722400000000);
1245 }
1246
1247
1248 ////////////////////////////////////////////////////////////////////////
1249
1250 struct S13969 {
1251 int x, y;
1252 }
1253
1254 int test13969(const S13969* f) {
1255 return 0 % ((f.y > 0) ? f.x / f.y : f.x / -f.y);
1256 }
1257
1258 ////////////////////////////////////////////////////////////////////////
1259
1260 int[] arr14436;
1261 void test14436()
1262 {
1263 assert(arr14436 == null);
1264 arr14436 = [1, 2, 3];
1265 assert(arr14436 != null);
1266 }
1267
1268 ////////////////////////////////////////////////////////////////////////
1269
1270 void test14220()
1271 {
1272 auto a = toString(14);
1273
1274 printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1275 return;
1276 }
1277
1278 auto toString(int value)
1279 {
1280 uint mValue = value;
1281
1282 char[int.sizeof * 3] buffer = void;
1283 size_t index = buffer.length;
1284
1285 do
1286 {
1287 uint div = cast(int)(mValue / 10);
1288 char mod = mValue % 10 + '0';
1289 buffer[--index] = mod; // Line 22
1290 mValue = div;
1291 } while (mValue);
1292
1293 //printf("buffer.ptr = %p, index = %d\n", buffer.ptr, cast(int)index);
1294 return dup(buffer[index .. $]);
1295 }
1296
1297 char[] dup(char[] a)
1298 {
1299 //printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1300 a[0] = 1; // segfault
1301 return a;
1302 }
1303
1304 ////////////////////////////////////////////////////////////////////////
1305
1306 int stripLeft(int str, int dc)
1307 {
1308 while (true)
1309 {
1310 int a = str;
1311 int s = a;
1312 str += 1;
1313 if (dc) return s;
1314 }
1315 }
1316
1317 void test14829()
1318 {
1319 if (stripLeft(3, 1) != 3) // fails with -O
1320 assert(0);
1321 }
1322
1323
1324 ////////////////////////////////////////////////////////////////////////
1325
1326 void test3()
1327 {
1328 int[6] a;
1329 int[] b;
1330 b = a;
1331 b = (b.ptr + b.length - 5)[0 .. b.ptr + b.length - 1 - a.ptr];
1332 assert(b.ptr == a.ptr + 1);
1333 assert(b.length == 5);
1334 }
1335
1336 ////////////////////////////////////////////////////////////////////////
1337 // https://issues.dlang.org/show_bug.cgi?id=14782
1338
1339
1340 void test14782()
1341 {
1342 static struct Foo
1343 {
1344 long a = 8;
1345 int b = 7;
1346 }
1347
1348 static Foo[1] fun() { Foo[1] a; return a; }
1349
1350 auto result = fun();
1351 assert(result[0].a == 8);
1352 assert(result[0].b == 7);
1353 }
1354
1355 ////////////////////////////////////////////////////////////////////////
1356
1357 void test14987()
1358 {
1359 static struct Foo
1360 {
1361 int b = 7;
1362 }
1363 static assert((Foo[4]).sizeof == 16);
1364
1365 static Foo[4] fun() { Foo[4] a; return a; }
1366
1367 auto result = fun();
1368 assert(result[0].b == 7);
1369 assert(result[1].b == 7);
1370 assert(result[2].b == 7);
1371 assert(result[3].b == 7);
1372 }
1373
1374 ////////////////////////////////////////////////////////////////////////
1375
1376 void[] calloc15272(size_t bc) nothrow pure
1377 {
1378 assert(bc == 1);
1379 return new void[1];
1380 }
1381
1382 void test15272()
1383 {
1384 void[] scache = cast(void[])"abc";
1385 size_t count = 1;
1386 void[]* buckets = &scache;
1387 *buckets = calloc15272(count)[0 .. count];
1388 }
1389
1390 /*****************************************
1391 * https://issues.dlang.org/show_bug.cgi?id=15861
1392 */
1393
1394 void test15861()
1395 {
1396 double val = 4286853117.;
1397
1398 (){
1399 assert(val == 4286853117.);
1400 }();
1401 }
1402
1403 ////////////////////////////////////////////////////////////////////////
1404
1405 // https://issues.dlang.org/show_bug.cgi?id=15629 comment 3
1406 // -O
1407
1408 void test15629()
1409 {
1410 int[] a = [3];
1411 int value = a[0] >= 0 ? a[0] : -a[0];
1412 assert(a[0] == 3);
1413 writeln(value, a);
1414 }
1415
1416 void writeln(int v, int[] a)
1417 {
1418 }
1419
1420 ////////////////////////////////////////////////////////////////////////
1421
1422 real binPosPow2() { return 1.0L; }
1423
1424 real binPow2()
1425 {
1426 return 1.0L/binPosPow2();
1427 }
1428
1429 void test4()
1430 {
1431 assert(binPow2() == 1.0L);
1432 }
1433
1434 ////////////////////////////////////////////////////////////////////////
1435 // https://issues.dlang.org/show_bug.cgi?id=13474
1436
1437
1438 double sumKBN(double s = 0.0)
1439 {
1440 import core.math : fabs;
1441 double c = 0.0;
1442 foreach(double x; [1, 1e100, 1, -1e100])
1443 {
1444 x = multiply(x);
1445 double t = s + x;
1446 if(s.fabs >= x.fabs)
1447 {
1448 double y = s-t;
1449 c += y+x;
1450 }
1451 else
1452 {
1453 double y = x-t;
1454 c += y+s;
1455 }
1456 s = t;
1457 }
1458 return s + c;
1459 }
1460
1461 double multiply(double a) { return a * 10000; }
1462
1463 void test13474()
1464 {
1465 double r = 20000;
1466 assert(r == sumKBN());
1467 }
1468
1469 ////////////////////////////////////////////////////////////////////////
1470 // https://issues.dlang.org/show_bug.cgi?id=16699
1471
1472 ulong[1] parseDateRange()
1473 {
1474 try
1475 {
1476 ulong[1] result;
1477 result[0] = 6;
1478 return result;
1479 }
1480 finally
1481 {
1482 }
1483 }
1484
1485 void test16699()
1486 {
1487 ulong[1] range = parseDateRange();
1488 assert(range[0] == 6);
1489 }
1490
1491 ////////////////////////////////////////////////////////////////////////
1492
1493 // https://issues.dlang.org/show_bug.cgi?id=16102
1494
1495 struct S16102 { ~this() { } }
1496
1497 long[1] f16102()
1498 {
1499 S16102 a;
1500 return [1];
1501 }
1502
1503 void test16102()
1504 {
1505 assert( f16102() == [1] );
1506 }
1507
1508 ////////////////////////////////////////////////////////////////////////
1509
1510 void test5a(ulong x, ulong y)
1511 {
1512 int a;
1513 if (x >> 32)
1514 a = 1;
1515 else
1516 a = 2;
1517 assert(a == 1);
1518
1519 if (y >> 32)
1520 a = 1;
1521 else
1522 a = 2;
1523 assert(a == 2);
1524 }
1525
1526 void test5()
1527 {
1528 test5a(uint.max + 1L, uint.max);
1529 }
1530
1531 ////////////////////////////////////////////////////////////////////////
1532
1533 /* Test the pattern:
1534 * replace (e ? i1 : i2) with (i1 + e * (i2 - i1))
1535 * when e1 is 0 or 1 and (i2-i1) is a power of 2.
1536 */
1537
1538 int foo61(int i)
1539 {
1540 return (i % 2 != 0) ? 4 : 2;
1541 }
1542
1543 int foo62(int i)
1544 {
1545 return (i % 2 != 0) ? 2 : 4;
1546 }
1547
1548 bool bar6(bool b) { return b; }
1549
1550 int foo63(bool b)
1551 {
1552 return bar6(b) ? 16 : 8;
1553 }
1554
1555 int foo64(bool b)
1556 {
1557 return bar6(b) ? 8 : 16;
1558 }
1559
1560 void test6()
1561 {
1562 if (foo61(0) != 2) assert(0);
1563 if (foo61(1) != 4) assert(0);
1564 if (foo62(0) != 4) assert(0);
1565 if (foo62(1) != 2) assert(0);
1566 if (foo63(0) != 8) assert(0);
1567 if (foo63(1) != 16) assert(0);
1568 if (foo64(0) != 16) assert(0);
1569 if (foo64(1) != 8) assert(0);
1570 }
1571
1572 ////////////////////////////////////////////////////////////////////////
1573
1574 int dataflow(int b) {
1575 int ret;
1576
1577 if (b==4)
1578 ret = 3;
1579 else
1580 ret = 5;
1581
1582 if (ret == 4)
1583 return 0;
1584 else
1585 return 1;
1586 }
1587
1588 void testeqeqranges()
1589 {
1590 int i = dataflow(4);
1591 if (i != 1)
1592 assert(0);
1593 }
1594
1595 ////////////////////////////////////////////////////////////////////////
1596
1597 // https://issues.dlang.org/show_bug.cgi?id=16189
1598
1599 void test16189()
1600 {
1601 ubyte[9][1] data;
1602 uint a = 0;
1603 loop:
1604 data[0] = data[a];
1605 a--;
1606 bool b = false;
1607 if (b) goto loop;
1608 assert(a == -1); // was failing with -O
1609 }
1610
1611
1612 ////////////////////////////////////////////////////////////////////////
1613
1614 // https://issues.dlang.org/show_bug.cgi?id=16997
1615
1616 void test16997()
1617 {
1618 /* Exhaustively test all signed and unsigned byte promotions for
1619 * - + and ~
1620 */
1621 for (int i = 0; i < 256; ++i)
1622 {
1623 ubyte c = cast(ubyte)i;
1624
1625 int i1 = cast(int)(~c);
1626 int i2 = cast(int)(~cast(int)c);
1627
1628 //printf("%d, %d\n", i1, i2);
1629 assert(i1 == i2);
1630
1631 i1 = cast(int)(+c);
1632 i2 = cast(int)(+cast(int)c);
1633 assert(i1 == i2);
1634
1635 i1 = cast(int)(-c);
1636 i2 = cast(int)(-cast(int)c);
1637 assert(i1 == i2);
1638 }
1639
1640 for (int i = 0; i < 256; ++i)
1641 {
1642 byte c = cast(byte)i;
1643
1644 int i1 = cast(int)(~c);
1645 int i2 = cast(int)(~cast(int)c);
1646
1647 //printf("%d, %d\n", i1, i2);
1648 assert(i1 == i2);
1649
1650 i1 = cast(int)(+c);
1651 i2 = cast(int)(+cast(int)c);
1652 assert(i1 == i2);
1653
1654 i1 = cast(int)(-c);
1655 i2 = cast(int)(-cast(int)c);
1656 assert(i1 == i2);
1657 }
1658 }
1659
1660 ////////////////////////////////////////////////////////////////////////
1661
1662 void test18315() // https://issues.dlang.org/show_bug.cgi?id=18315
1663 {
1664 int i = int.min;
1665 bool b = i > 0;
1666 assert(!b);
1667 b = 0 < i;
1668 assert(!b);
1669 }
1670
1671 ////////////////////////////////////////////////////////////////////////
1672
1673 // https://issues.dlang.org/show_bug.cgi?id=18461
1674
1675 void test18461()
1676 {
1677 import core.bitop;
1678
1679 size_t test_val = 0b0001_0000;
1680
1681 if (bt(&test_val, 4) == 0)
1682 assert(false);
1683 }
1684
1685 ////////////////////////////////////////////////////////////////////////
1686
1687 void test18730() // https://issues.dlang.org/show_bug.cgi?id=18730
1688 {
1689 static if (size_t.sizeof == 8)
1690 {
1691 static int bt18730_64_64(in ulong* p, ulong bitnum) pure @system
1692 {
1693 return ((p[bitnum >> 6] & (1L << (bitnum & 63)))) != 0;
1694 }
1695 static int bt18730_64_32(in ulong* p, uint bitnum) pure @system
1696 {
1697 return ((p[bitnum >> 6] & (1L << (bitnum & 63)))) != 0;
1698 }
1699 static int bt18730_32_64(in uint* p, ulong bitnum) pure @system
1700 {
1701 return ((p[bitnum >> 5] & (1 << (bitnum & 31)))) != 0;
1702 }
1703
1704 // Check that bt_64_64 uses a 64-bit register for the offset.
1705 {
1706 enum bitIndex = int.max + 1L;
1707 auto a = new ulong[](bitIndex / 64 + 1);
1708 a[bitIndex / 64] = 1;
1709 assert(bt18730_64_64(a.ptr, bitIndex));
1710 assert(!bt18730_64_64(a.ptr, bitIndex + 1));
1711 assert(!bt18730_64_64(a.ptr, bitIndex - 1));
1712 }
1713 // Check that bt_64_32 uses a 32-bit register for the offset.
1714 {
1715 static int f(ulong* p, ulong bitnum)
1716 {
1717 return bt18730_64_32(p, cast(uint) bitnum);
1718 }
1719 enum bitIndex = uint.max + 1L;
1720 assert(cast(uint) bitIndex == 0);
1721 ulong s = 1;
1722 assert(f(&s, bitIndex));
1723 }
1724 /* Check that bt_32_64 does not become a 64-bit bt instruction. Would lead
1725 to a segfault when trying to load 8 bytes while only 4 are accessible. */
1726 version (Posix)
1727 {{
1728 import core.sys.posix.sys.mman;
1729 import core.sys.posix.unistd;
1730 // Allocate two pages.
1731 immutable sz = 2 * sysconf(_SC_PAGESIZE);
1732 auto m = mmap(null, sz, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
1733 // Discard the higher page. It becomes unreadable.
1734 munmap(m + sz / 2, sz / 2);
1735 // Try looking at the last 4 bytes of the readable page.
1736 uint* p = cast(uint*) (m + sz / 2 - uint.sizeof);
1737 bt18730_32_64(p, 0);
1738 munmap(m, sz / 2); // Free the readable page.
1739 }}
1740 }
1741 }
1742
1743 ////////////////////////////////////////////////////////////////////////
1744
1745 void test19497() // https://issues.dlang.org/show_bug.cgi?id=19497
1746 {
1747 {
1748 ubyte[1024] data;
1749 ushort* ushortPtr = cast(ushort*) data.ptr;
1750 *ushortPtr++ = 0xfe00;
1751 printf("ushortPtr(%p)\n", ushortPtr);
1752 fflush(stdout);
1753 }
1754
1755 alias Seq(stuff ...) = stuff;
1756 static foreach (T; Seq!(ubyte, ushort, uint, ulong, byte, short, int, long))
1757 {{
1758 T[2] data = 0x2A;
1759 T* q = &data[0];
1760 *q++ = cast(T) 0x1122334455667788;
1761 if (*q != 0x2A) assert(false);
1762 }}
1763
1764 {
1765 static int toStringz(string s) { return s.length > 0 ? s[0] : 0; }
1766 static void toAStringz(in string[] a, int* az)
1767 {
1768 foreach (string s; a)
1769 {
1770 *az++ = toStringz(s);
1771 }
1772 }
1773 string[1] sa = ["abc"];
1774 int[2] tgt = 0x2a;
1775 toAStringz(sa[], tgt.ptr);
1776 if (tgt[0] != 'a') assert(false);
1777 if (tgt[1] != 0x2a) assert(false);
1778 }
1779 }
1780
1781 ////////////////////////////////////////////////////////////////////////
1782
1783 // https://issues.dlang.org/show_bug.cgi?id=18794
1784
1785 bool method18794(size_t* p)
1786 {
1787 int bitIdx = 0;
1788 func18794();
1789 return (*p & (1UL << bitIdx)) != 0;
1790 }
1791
1792 void func18794() {}
1793
1794 void prep18794()
1795 {
1796 asm {}
1797 ulong[2] x = -1;
1798 }
1799
1800 void test18794()
1801 {
1802 prep18794();
1803 size_t s;
1804 method18794(&s);
1805 }
1806
1807 ////////////////////////////////////////////////////////////////////////
1808
1809 /* Test the optimization
1810 * (e1+c)-e2 => (e1-e2)+c
1811 */
1812
1813 void testelmin()
1814 {
1815 static void foo(int i)
1816 {
1817 static ubyte[4] bar()
1818 {
1819 ubyte[4] array;
1820 foreach (i, ref a; array)
1821 a = cast(ubyte)(i + 1);
1822 return array;
1823 }
1824
1825 static void test(int i, ubyte* p)
1826 {
1827 foreach (j; 0 .. 4)
1828 assert(p[i * 4 + j] == j + 1);
1829 }
1830
1831 ubyte[32] data;
1832 data[i*4..(i+1)*4] = bar(); // optimize to single MOV
1833
1834 test(i, data.ptr);
1835 }
1836
1837 foo(4);
1838 }
1839
1840 ////////////////////////////////////////////////////////////////////////
1841
1842 const(char)* fastpar(string s)
1843 {
1844 return s.ptr + s.length;
1845 }
1846
1847 void testfastpar()
1848 {
1849 string s = "abcde";
1850 auto p = fastpar(s);
1851 assert(*p == 0);
1852 }
1853
1854 ////////////////////////////////////////////////////////////////////////
1855 // https://issues.dlang.org/show_bug.cgi?id=20363
1856
1857 ulong foo20363(double d)
1858 {
1859 ulong u = * cast(ulong*) &d;
1860 return (u >> 1) & 1;
1861 }
1862
1863 void test20363()
1864 {
1865 ulong u = 0b10;
1866 if (foo20363(*cast(double*) &u) == 0)
1867 assert(false);
1868 }
1869
1870 ////////////////////////////////////////////////////////////////////////
1871
1872
1873 T testfooa(T)(T value)
1874 {
1875 return 10 - (value * 57); // gets rewritten into (value*-57)+10
1876 }
1877
1878 T testfoob(T)(T value)
1879 {
1880 return (value * -57) + 10;
1881 }
1882
1883 void testNegConst()
1884 {
1885 assert(testfooa(1) == -47);
1886 assert(testfoob(1) == -47);
1887 assert(testfooa(1.0) == -47);
1888 assert(testfoob(1.0) == -47);
1889 }
1890
1891 ////////////////////////////////////////////////////////////////////////
1892
1893 // https://issues.dlang.org/show_bug.cgi?id=16317
1894
1895 int add8ret3(ref int s)
1896 {
1897 s += 8;
1898 return 3;
1899 }
1900
1901 int binAdd(int val)
1902 {
1903 val = val + add8ret3(val);
1904 return val;
1905 }
1906
1907 void test16317()
1908 {
1909 assert(binAdd(1) == (1 + 3));
1910 static assert(binAdd(1) == (1 + 3));
1911 }
1912
1913 ////////////////////////////////////////////////////////////////////////
1914
1915 // https://issues.dlang.org/show_bug.cgi?id=20050
1916
1917 int test20050_g = 0;
1918 void test20050_impure_function_1() { ++test20050_g; }
1919 void function() test20050_get_impure_function() pure
1920 {
1921 static void impure_function_2()
1922 {
1923 ++test20050_g;
1924 test20050_impure_function_1();
1925 }
1926 return &impure_function_2;
1927 }
1928 void test20050()
1929 {
1930 auto f = test20050_get_impure_function();
1931 f();
1932 assert(test20050_g == 2);
1933 }
1934
1935 ////////////////////////////////////////////////////////////////////////
1936
1937 // https://github.com/dlang/dmd/pull/11238
1938
1939 int testCpStatic1(int y)
1940 {
1941 __gshared int yyy = 7;
1942 auto x = yyy; // no copy-propagation
1943 if (y)
1944 return x;
1945 return x + 3;
1946 }
1947
1948 void testCpStatic()
1949 {
1950 assert(testCpStatic1(1) == 7);
1951 assert(testCpStatic1(0) == 10);
1952 }
1953
1954 ////////////////////////////////////////////////////////////////////////
1955 // https://issues.dlang.org/show_bug.cgi?id=20991
1956
1957 int x7;
1958
1959 void bar7(int i)
1960 {
1961 assert(i == x7);
1962 ++x7;
1963 }
1964
1965 void test7()
1966 {
1967 for (int i = 0; i <= 1; ++i)
1968 bar7(i);
1969 assert(x7 == 2);
1970 }
1971
1972 ////////////////////////////////////////////////////////////////////////
1973
1974 // https://github.com/dlang/dmd/pull/11388
1975
1976 ushort byteswap(ushort x) pure
1977 {
1978 // Should be detected and XCHG instruction generated
1979 return cast(ushort) (((x >> 8) & 0xFF) | ((x << 8) & 0xFF00u));
1980 }
1981
1982 void testbyteswap()
1983 {
1984 assert(byteswap(0xF234) == 0x34F2);
1985 static ushort xx = 0xF234;
1986 assert(byteswap(xx) == 0x34F2);
1987 }
1988
1989 ////////////////////////////////////////////////////////////////////////
1990
1991 // These should all be recognized by the compiler and generate ROL or ROR
1992 // instructions.
1993
1994 uint rol32(uint x, uint n)
1995 {
1996 return (x << n) | (x >> (32 - n));
1997 }
1998
1999 uint ror32(uint x, uint n)
2000 {
2001 return (x >> n) | (x << (32 - n));
2002 }
2003
2004 ulong rol64(ulong x, uint n)
2005 {
2006 return (x << n) | (x >> (64 - n));
2007 }
2008
2009 ulong ror64(ulong x, uint n)
2010 {
2011 return (x >> n) | (x << (64 - n));
2012 }
2013
2014 void testrolror()
2015 {
2016 assert(ror32(0x0123_4567u, 4) == 0x7012_3456);
2017 assert(rol32(0x7012_3456u, 4) == 0x0123_4567);
2018
2019 assert(ror64(0x0123_4567_89AB_CDEFuL, 4) == 0xF012_3456_789A_BCDE);
2020 assert(rol64(0xF012_3456_789A_BCDEuL, 4) == 0x0123_4567_89AB_CDEF);
2021 }
2022
2023 ////////////////////////////////////////////////////////////////////////
2024
2025 // https://issues.dlang.org/show_bug.cgi?id=20162
2026
2027 void test20162()
2028 {
2029 static long f(long a)
2030 {
2031 assert(a == -1L);
2032 return a;
2033 }
2034
2035 foreach (i; 1 .. 2)
2036 {
2037 foreach (j; 0 .. 2)
2038 {
2039 printf("%d %d %llx\n", i,
2040 ((i != 0) ? -1 : +1),
2041 f((i != 0) ? -1 : +1));
2042 }
2043 }
2044 }
2045
2046 ////////////////////////////////////////////////////////////////////////
2047 // https://issues.dlang.org/show_bug.cgi?id=3713
2048
2049 int star1(int i)
2050 {
2051 return i ? star1(i - 1) : 0;
2052 }
2053
2054 int star2(int i)
2055 {
2056 return i == 0 ? 0 : star2(i - 1);
2057 }
2058
2059 int star3(int i)
2060 {
2061 if (i == 0)
2062 return 0;
2063 return i == 2 ? star3(i - 2) : star3(i - 1);
2064 }
2065
2066 int star4(int i)
2067 {
2068 return (i == 0) ? 0
2069 : i != 2 ? star4(i - 1)
2070 : star4(i - 2);
2071 }
2072
2073 void test3713()
2074 {
2075 assert(star1(10) == 0);
2076 assert(star2(10) == 0);
2077 assert(star3(10) == 0);
2078 assert(star4(10) == 0);
2079 }
2080
2081 ////////////////////////////////////////////////////////////////////////
2082
2083 void testsbbrex()
2084 {
2085 // special code is generated for these two cases
2086 static long foolt(dchar c)
2087 {
2088 return c < 0x10000 ? 1 : 2;
2089 }
2090
2091 static long fooge(uint c)
2092 {
2093 return c >= 0x10000 ? 1L : 2L;
2094 }
2095
2096 assert(foolt(0) == 1);
2097 assert(foolt(0x10000) == 2);
2098 assert(fooge(0) == 2);
2099 assert(fooge(0x10000) == 1);
2100 }
2101
2102
2103 ////////////////////////////////////////////////////////////////////////
2104
2105 // https://issues.dlang.org/show_bug.cgi?id=19846
2106
2107 alias Void = byte[0];
2108 static immutable Void VOID; // = [];
2109
2110 __gshared int x19846;
2111
2112 Void print19846()
2113 {
2114 //printf("This should print\n");
2115 x19846 = 3;
2116 return VOID;
2117 }
2118
2119 Void identity19846(Void value, out int i)
2120 {
2121 i = 7;
2122 return value;
2123 }
2124
2125 void test19846()
2126 {
2127 int i;
2128 identity19846(print19846(), i);
2129 //printf("i = %d\n", i);
2130 assert(x19846 == 3);
2131 assert(i == 7);
2132 }
2133
2134 ////////////////////////////////////////////////////////////////////////
2135
2136 // Some tests for OPmemcpy
2137
2138 enum N = 128;
2139
2140 ubyte[N] def()
2141 {
2142 ubyte[N] array;
2143 foreach (i, ref a; array)
2144 a = cast(ubyte)(i + 1);
2145 return array;
2146 }
2147
2148
2149 void ghi(ubyte* p)
2150 {
2151 foreach (i; 0 .. N)
2152 assert(p[i] == i + 1);
2153 }
2154
2155 void testmemcpy()
2156 {
2157 ubyte[N] bits;
2158 ubyte[N] bits2;
2159 bits2[0..N] = bits[0..N] = def();
2160 ghi(bits.ptr);
2161 ghi(bits2.ptr);
2162
2163 __gshared size_t n = N;
2164 ubyte[N] bits3;
2165 ubyte[N] bits4;
2166 bits4[0..n] = bits3[0..n] = def();
2167 ghi(bits3.ptr);
2168 ghi(bits4.ptr);
2169 }
2170
2171 ////////////////////////////////////////////////////////////////////////
2172
2173
2174 /* Test all the cases of uses of LEA for multiplication by a constant
2175 */
2176
2177 T testlea(uint C, T)(T x, T y)
2178 {
2179 y = y * C; // cdmul()
2180 x *= C; // cdmulass()
2181 return x + y;
2182 }
2183
2184 void testleax(uint C)(uint X, uint Y)
2185 {
2186 assert(testlea!C(X,Y) == C * (X + Y));
2187 assert(testlea!C(cast(long)X,cast(long)Y) == cast(long)C*X + cast(long)C*Y);
2188 }
2189
2190 void testMulLea()
2191 {
2192 testleax!3(10,11);
2193 testleax!5(10,11);
2194 testleax!6(10,11);
2195 testleax!9(10,11);
2196
2197 testleax!10(10,11);
2198 testleax!12(10,11);
2199 testleax!18(10,11);
2200 testleax!20(10,11);
2201 testleax!24(10,11);
2202 testleax!36(10,11);
2203 testleax!40(10,11);
2204 testleax!72(10,11);
2205
2206 testleax!37(10,11);
2207 testleax!74(10,11);
2208 testleax!13(10,11);
2209 testleax!26(10,11);
2210 }
2211
2212 ////////////////////////////////////////////////////////////////////////
2213
2214 /* Test *= of register pair
2215 */
2216
2217 void testMulAssPair()
2218 {
2219 static ulong pow(ulong x, int m)
2220 {
2221 ulong v = x;
2222 ulong p = 1;
2223 while (1)
2224 {
2225 if (m & 1)
2226 p *= v;
2227 m >>= 1;
2228 if (!m)
2229 break;
2230 v *= v;
2231 }
2232 return p;
2233 }
2234
2235 enum ulong e_10_pow_19 = 10uL^^19;
2236 assert(e_10_pow_19 == pow(10uL, 19));
2237 }
2238
2239 ////////////////////////////////////////////////////////////////////////
2240 // https://issues.dlang.org/show_bug.cgi?id=21038
2241
2242 const(wchar)* x21038 = "xz";
2243 const(dchar)* name21038 = "abcd";
2244
2245 void test21038()
2246 {
2247 assert((cast(size_t) x21038) % wchar.sizeof == 0);
2248 assert((cast(size_t) name21038) % dchar.sizeof == 0);
2249 }
2250
2251 ////////////////////////////////////////////////////////////////////////
2252 // https://issues.dlang.org/show_bug.cgi?id=21325
2253
2254 real f21325(const real x) pure @safe nothrow @nogc
2255 {
2256 return (x != 0.0L) ? x : real.nan;
2257 }
2258
2259 void test21325() @safe
2260 {
2261 ulong x = 0uL;
2262 while(true)
2263 {
2264 const y = f21325(x); // should set y to real.nan
2265
2266 assert(y != y);
2267
2268 if (++x)
2269 return; // good
2270 }
2271 }
2272
2273 ////////////////////////////////////////////////////////////////////////
2274 // https://issues.dlang.org/show_bug.cgi?id=16274
2275
2276 extern(C) int pair(short a, ushort b, byte c, ubyte d);
2277
2278 struct S
2279 {
2280 // provide alternate implementation of .pair()
2281 pragma(mangle, "pair")
2282 extern(C) static void pair(int a, int b, int c, int d)
2283 {
2284 //printf("%d %d %d %d\n", a, b, c, d);
2285 assert(a == -1);
2286 assert(b == 2);
2287 assert(c == -3);
2288 assert(d == 4);
2289 }
2290 }
2291
2292 void test16274()
2293 {
2294 version (X86_64)
2295 pair(-1, 2, -3, 4);
2296 version (X86)
2297 pair(-1, 2, -3, 4);
2298 }
2299
2300 ////////////////////////////////////////////////////////////////////////
2301 // https://issues.dlang.org/show_bug.cgi?id=16268
2302
2303 void test16268()
2304 {
2305 static void f(byte x)
2306 {
2307 for (byte i = 0; i <= x && i >= 0; ++i)
2308 {
2309 assert(i >= 0);
2310 assert(i != -1);
2311 //printf("%d\n", i);
2312 }
2313 }
2314
2315 f(byte.max);
2316 }
2317
2318 ////////////////////////////////////////////////////////////////////////
2319 // https://issues.dlang.org/show_bug.cgi?id=11435
2320
2321 void test11435a()
2322 {
2323 alias T = byte;
2324
2325 static void fun(T c, T b, int v)
2326 {
2327 }
2328
2329 static void abc(T[] b)
2330 {
2331 fun(b[0], b[1], 0);
2332 }
2333
2334 version(Windows)
2335 {
2336 import core.sys.windows.windows;
2337 auto p = VirtualAlloc(null, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2338 }
2339 else
2340 {
2341 import core.sys.posix.sys.mman;
2342 auto p = mmap(null, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0L);
2343 }
2344 assert(p);
2345 auto px = (cast(T*)(p + 4096 - 2 * T.sizeof));
2346 abc(px[0..2]);
2347 }
2348
2349 void test11435b()
2350 {
2351 import core.sys.windows.windows;
2352 alias T = short;
2353
2354 static void fun(T c, T b, int v)
2355 {
2356 }
2357
2358 static void abc(T[] b)
2359 {
2360 fun(b[0], b[1], 0);
2361 }
2362
2363 version(Windows)
2364 {
2365 import core.sys.windows.windows;
2366 auto p = VirtualAlloc(null, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2367 }
2368 else
2369 {
2370 import core.sys.posix.sys.mman;
2371 auto p = mmap(null, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0L);
2372 }
2373 assert(p);
2374 auto px = (cast(T*)(p + 4096 - 2 * T.sizeof));
2375 abc(px[0..2]);
2376 }
2377
2378 ////////////////////////////////////////////////////////////////////////
2379 // https://issues.dlang.org/show_bug.cgi?id=21513
2380
2381 struct Stuff
2382 {
2383 size_t c; // declare after items and not crash !
2384 ubyte[1] items;
2385 }
2386
2387 void grow(ref Stuff stuff)
2388 {
2389 with (stuff)
2390 {
2391 const oldCapacity = c;
2392 items.ptr[0..oldCapacity] = items.ptr[0..0]; // use literal 0 instead of
2393 items.ptr[0] = 0; // oldcapacity and no crash !
2394 }
2395 }
2396
2397 void test21513()
2398 {
2399 Stuff stuff;
2400 grow(stuff);
2401 }
2402
2403 ////////////////////////////////////////////////////////////////////////
2404 // https://issues.dlang.org/show_bug.cgi?id=21526
2405
2406 double f21256(double a, double b) {
2407 double c = a + b;
2408 return c;
2409 }
2410
2411 void test21256()
2412 {
2413 union DX
2414 {
2415 double d;
2416 ulong l;
2417 }
2418
2419 DX a, b;
2420 a.l = 0x4341c37937e08000;
2421 b.l = 0x4007ffcb923a29c7;
2422
2423 DX r;
2424 r.d = f21256(a.d, b.d);
2425 //if (r.d != 0x1.1c37937e08001p+53)
2426 //printf("r = %A should be 0x1.1c37937e08001p+53 %A\n", r.d, 0x1.1c37937e08001p+53);
2427 //assert(r == 0x1.1c37937e08001p+53);
2428
2429 // cannot seem to get the two to produce the same value
2430 assert(r.l == 0x4341c37937e08001 || // value using XMM
2431 r.l == 0x4341c37937e08002); // value using x87
2432 }
2433
2434 ////////////////////////////////////////////////////////////////////////
2435 // https://issues.dlang.org/show_bug.cgi?id=21816
2436
2437 bool test21816a(float t)
2438 {
2439 return cast(bool)t;
2440 }
2441
2442 void test21816()
2443 {
2444 assert(test21816a(float.nan));
2445 }
2446
2447 ////////////////////////////////////////////////////////////////////////
2448 // https://issues.dlang.org/show_bug.cgi?id=21835
2449
2450 struct Point21835
2451 {
2452 float f = 3.0;
2453 double d = 4.0;
2454 real r = 5.0;
2455 }
2456
2457 void test21835y()
2458 {
2459 Point21835[1] arr;
2460 if (arr[0].f != 3.0) assert(0);
2461 if (arr[0].d != 4.0) assert(0);
2462 if (arr[0].r != 5.0) assert(0);
2463 }
2464
2465 struct Point21835x
2466 {
2467 float f = 0.0;
2468 double d = 0.0;
2469 real r = 0.0;
2470 }
2471
2472 void test21835()
2473 {
2474 test21835y();
2475 Point21835x[1] arr;
2476 if (arr[0].f != 0.0) assert(0);
2477 if (arr[0].d != 0.0) assert(0);
2478 if (arr[0].r != 0.0) assert(0);
2479 }
2480
2481 ////////////////////////////////////////////////////////////////////////
2482 // https://github.com/dlang/dmd/pull/16187#issuecomment-1946534649
2483
2484 void testDoWhileContinue()
2485 {
2486 int i = 10;
2487 do
2488 {
2489 continue;
2490 }
2491 while(--i > 0);
2492 }
2493
2494 ////////////////////////////////////////////////////////////////////////
2495 // https://github.com/dlang/dmd/issues/20574
2496
2497 int test20574x(int i, int y)
2498 {
2499 return i ? y : y;
2500 }
2501
2502 void test20574()
2503 {
2504 assert(test20574x(1, 2) == 2);
2505 assert(test20574x(0, 2) == 2);
2506 }
2507
2508 ////////////////////////////////////////////////////////////////////////
2509
2510 struct S8
2511 {
2512 int x,y,z;
2513 }
2514
2515 int test8x(S8 s)
2516 {
2517 s = s;
2518 return s.y;
2519 }
2520
2521 void test8()
2522 {
2523 S8 s;
2524 s.y = 2;
2525 assert(test8x(s) == 2);
2526 }
2527
2528 ////////////////////////////////////////////////////////////////////////
2529
2530 struct S9
2531 {
2532 int a,b;
2533 ~this() { }
2534 }
2535
2536
2537 S9 test9x(ref S9 arg)
2538 {
2539 return arg;
2540 }
2541
2542 void test9()
2543 {
2544 S9 s;
2545 s.b = 3;
2546 S9 t = test9x(s);
2547 assert(t.b == 3);
2548 }
2549
2550 ////////////////////////////////////////////////////////////////////////
2551
2552 int main()
2553 {
2554 // All the various integer divide tests
2555 testsdiv2();
2556 testulldiv();
2557 testfastudiv();
2558 testsldiv();
2559 testslmod();
2560 testfastdiv();
2561 testdivdiv();
2562 testdivcmp();
2563
2564 testgoto();
2565 testswitch();
2566 testdo();
2567 testbreak();
2568 teststringswitch();
2569 teststrarg();
2570 test12164();
2571 testsizes();
2572 testarrayinit();
2573 testU();
2574 testbittest();
2575 test8658();
2576 test3918();
2577 test12051();
2578 testdocond();
2579 testnegcom();
2580 test11565();
2581 test23743();
2582 testoror();
2583 testbt();
2584 test12095(0);
2585 testandand();
2586 testor_combine();
2587 testshrshl();
2588 test13383();
2589 test13190();
2590 test13485();
2591 test14436();
2592 test10715();
2593 test10678();
2594 test7565();
2595 test13023(0x10_0000_0000);
2596 test12833();
2597 test9449();
2598 test9449x();
2599 test12057();
2600 test13784();
2601 test14220();
2602 test14829();
2603 test3();
2604 test14782();
2605 test14987();
2606 test15272();
2607 test15861();
2608 test15629();
2609 test4();
2610 test13474();
2611 test16699();
2612 test16102();
2613 test5();
2614 test6();
2615 testeqeqranges();
2616 test16189();
2617 test16997();
2618 test18315();
2619 test18461();
2620 test18730();
2621 test19497();
2622 test18794();
2623 testelmin();
2624 testfastpar();
2625 test20363();
2626 testNegConst();
2627 test16317();
2628 test20050();
2629 testCpStatic();
2630 test7();
2631 testbyteswap();
2632 testrolror();
2633 test20162();
2634 test3713();
2635 testsbbrex();
2636 test19846();
2637 testmemcpy();
2638 testMulLea();
2639 testMulAssPair();
2640 test21038();
2641 test21325();
2642 test16274();
2643 test16268();
2644 test11435a();
2645 test11435b();
2646 test21513();
2647 test21256();
2648 test21816();
2649 test21835();
2650 testDoWhileContinue();
2651 test20574();
2652 test8();
2653 test9();
2654
2655 printf("Success\n");
2656 return 0;
2657 }