1 /** loopp counter narrowing optimizatrion test 2 type: unsigned long, signed long 3 */ 4 #include <testfwk.h> 5 6 #include <limits.h> 7 #include <setjmp.h> 8 9 #if !defined(__SDCC_mcs51) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) 10 unsigned char array[300]; 11 #endif 12 13 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) 14 /* A loop where the counter should be narrowed to an 8-bit unsigned type. */ 15 void loop8(unsigned char *a, {type} n) 16 { 17 for({type} i = 0; i < n; i++) 18 a[i * n] = 8; 19 } 20 21 /* A loop where the counter should be narrowed to a 16-bit unsigned type, but not further. */ loop16(unsigned char * a)22void loop16(unsigned char *a) 23 { 24 for ({type} i = 0; i < 300; i++) 25 a[i] = 16; 26 } 27 28 /* A loop where the subtraction should prevent optimization. */ loopm(unsigned char * a)29void loopm(unsigned char *a) 30 { 31 for ({type} i = (1ul << 20); i < (1ul << 20) + 1; i++) 32 a[i - (1ul << 20)] = 1; 33 } 34 35 void modify1({type} *p) 36 { 37 *p = 17; 38 } 39 40 void modify2({type} *p) 41 { 42 *p = (1ul << 30); 43 } 44 45 /* Loops where access to the counter via pointers should prevent optimization. */ address(unsigned char * a)46void address(unsigned char *a) 47 { 48 for ({type} i = (1ul << 28); i < (1ul << 30); i++) 49 { 50 modify1(&i); 51 a[i] = 17; 52 modify2(&i); 53 } 54 55 for ({type} i = (1ul << 28); i < (1ul << 30); i++) 56 { 57 {type} *p = &i; 58 *p = 18; 59 a[i] = 18; 60 *p = (1ul << 30); 61 } 62 } 63 64 void jump_func(jmp_buf *jp, {type} i) 65 { 66 ASSERT (i == (1ul << 29)); 67 longjmp (*jp, 0); 68 } 69 70 /* A loop where the side-effects from jump_func() should prevent optimization. */ jump(unsigned char * a)71void jump(unsigned char *a) 72 { 73 jmp_buf j; 74 75 if (setjmp (j)) 76 return; 77 78 for ({type} i = (1ul << 29); i < (1ul << 30); i++) 79 { 80 jump_func(&j, i); 81 a[i] = 14; 82 } 83 84 a[0] = 13; 85 } 86 #endif 87 testLoop(void)88void testLoop(void) 89 { 90 #if !defined(__SDCC_mcs51) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) 91 loop8 (array, 3); 92 ASSERT (array[0] == 8); 93 ASSERT (array[3] == 8); 94 ASSERT (array[6] == 8); 95 96 loop16 (array); 97 ASSERT (array[0] == 16); 98 ASSERT (array[17] == 16); 99 ASSERT (array[255] == 16); 100 ASSERT (array[256] == 16); 101 ASSERT (array[299] == 16); 102 103 loopm (array); 104 ASSERT (array[0] == 1); 105 106 address (array); 107 ASSERT (array[17] == 17); 108 ASSERT (array[18] == 18); 109 110 jump (array); 111 ASSERT (array[0] != 13); 112 #endif 113 } 114 115