1 // Prime Test Program (v1.2, September 7 2010)
2 // Written by Ian Seyler
4 // This program checks all odd numbers between 3 and 'maxn' and determines if they are prime.
5 // On exit the program will display the execution time and how many prime numbers were found.
6 // Useful for testing runtime performance between Linux and BareMetal OS.
8 // BareMetal compile using GCC (Tested with 4.5.0)
9 // gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -mno-red-zone -o prime.o prime.c -DBAREMETAL
10 // gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -mno-red-zone -o libBareMetal.o libBareMetal.c
11 // objcopy --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame prime.o
12 // objcopy --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame libBareMetal.o
13 // ld -T app.ld -o prime.app prime.o libBareMetal.o
15 // Linux compile using GCC (Tested with 4.5.0)
16 // gcc -m64 -o prime prime.c -DLINUX
19 // maxn = 500000 primes = 41538
20 // maxn = 1000000 primes = 78498
21 // maxn = 5000000 primes = 348513
22 // maxn = 10000000 primes = 664579
31 #include "libBareMetal.h"
36 // primes is set to 1 since we don't calculate for '2' as it is a known prime number
37 register unsigned long i
, j
, maxn
=1000000, primes
=1;
40 unsigned char tstring
[25];
41 unsigned long start
, finish
;
42 start
= b_get_timercounter();
50 for(i
=3; i
<=maxn
; i
+=2)
54 if(i
%j
==0) break; //Number is divisble by some other number. So break out
60 } //Continue loop up to max number
64 printf("%u in %.0lf seconds\n", primes
, difftime(finish
, start
));
68 finish
= b_get_timercounter();
69 b_int_to_string(primes
, tstring
);
70 b_print_string(tstring
);
71 b_print_string(" in ");
72 finish
= (finish
- start
) / 8;
73 b_int_to_string(finish
, tstring
);
74 b_print_string(tstring
);
75 b_print_string(" seconds\n");