]> icculus.org git repositories - icculus/xz.git/blob - tests/bcj_test.c
Added test_compress.sh and bunch of files needed by it.
[icculus/xz.git] / tests / bcj_test.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       bcj_test.c
4 /// \brief      Source code of compress_prepared_bcj_*
5 ///
6 /// This is a simple program that should make the compiler to generate
7 /// PC-relative branches, jumps, and calls. The compiled files can then
8 /// be used to test the branch conversion filters. Note that this program
9 /// itself does nothing useful.
10 ///
11 /// Compiling: gcc -std=c99 -fPIC bcj_test.c
12 /// Don't optimize or strip.
13 //
14 //  This code has been put into the public domain.
15 //
16 //  This library is distributed in the hope that it will be useful,
17 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21
22 extern int jump(int a, int b);
23
24
25 extern int
26 call(int a, int b)
27 {
28         if (a < b)
29                 a = jump(a, b);
30
31         return a;
32 }
33
34
35 extern int
36 jump(int a, int b)
37 {
38         // The loop generates conditional jump backwards.
39         while (1) {
40                 if (a < b) {
41                         a *= 2;
42                         a += 3 * b;
43                         break;
44                 } else {
45                         // Put enough code here to prevent JMP SHORT on x86.
46                         a += b;
47                         a /= 2;
48                         b += b % 5;
49                         a -= b / 3;
50                         b = 2 * b + a - 1;
51                         a *= b + a + 1;
52                         b += a - 1;
53                         a += b * 2 - a / 5;
54                 }
55         }
56
57         return a;
58 }
59
60
61 int
62 main(int argc, char **argv)
63 {
64         int a = call(argc, argc + 1);
65         return a == 0;
66 }