]> git.sur5r.net Git - cc65/blob - test/ref/cc65101102.c
Only for jumps, the lib uses named asm labels in branches
[cc65] / test / ref / cc65101102.c
1 /*
2   !!DESCRIPTION!!
3   !!ORIGIN!!      testsuite
4   !!LICENCE!!     Public Domain
5   !!AUTHOR!!      Marc 'BlackJack' Rintsch
6 */
7
8 /*
9 Compiler is build from cc65-snapshot-2.13.9.20101031 sources.
10
11 Expected results and also what I get from this without any optimisations
12 are: 48663 and 49218
13
14 When I turn on ''-O'': 58096 and 58096.  After swapping the two variable
15 declaration lines in 'calculate_checksum()' the results are correct
16 with ''-O''.
17
18 But with ''--O --static-locals'' the results are incorrect again (31757
19 and 15408).  ''--static-locals'' alone works though.
20 */
21
22 #include <stdio.h>
23 #include <stdint.h>
24
25 // uint16_t __fastcall__ calculate_checksum(uint8_t *block);
26 uint8_t block[256];
27
28 uint16_t calculate_checksum(uint8_t *block)
29 {
30     uint16_t i, result = 0xffff;
31     uint8_t j;
32
33     for (i = 0; i < 256; ++i) {
34         result ^= block[i] << 8;
35         for (j = 0; j < 8; ++j) {
36             if (result & (1 << 15)) {
37                 result = (result << 1) ^ 0x1021;
38             } else {
39                 result <<= 1;
40             }
41         }
42     }
43     return ~result;
44 }
45
46 int main(void)
47 {
48     uint16_t i;
49
50     printf("zeroes: %u\n", calculate_checksum(block));
51     for (i = 0; i < 256; ++i) block[i] = i;
52     printf("0..255: %u\n", calculate_checksum(block));
53
54     return 0;
55 }
56