* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / baremetal0.5.2 / programs / prime.c
blobf6587596e18ff224798e8d690a174dc4a2f86f23
1 // Prime Test Program (v1.2, September 7 2010)
2 // Written by Ian Seyler
3 //
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.
7 //
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
17 // strip prime
19 // maxn = 500000 primes = 41538
20 // maxn = 1000000 primes = 78498
21 // maxn = 5000000 primes = 348513
22 // maxn = 10000000 primes = 664579
25 #ifdef LINUX
26 #include <stdio.h>
27 #include <time.h>
28 #endif
30 #ifdef BAREMETAL
31 #include "libBareMetal.h"
32 #endif
34 int main()
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;
39 #ifdef BAREMETAL
40 unsigned char tstring[25];
41 unsigned long start, finish;
42 start = b_get_timercounter();
43 #endif
45 #ifdef LINUX
46 time_t start, finish;
47 time(&start);
48 #endif
50 for(i=3; i<=maxn; i+=2)
52 for(j=2; j<=i-1; j++)
54 if(i%j==0) break; //Number is divisble by some other number. So break out
56 if(i==j)
58 primes = primes + 1;
60 } //Continue loop up to max number
62 #ifdef LINUX
63 time(&finish);
64 printf("%u in %.0lf seconds\n", primes, difftime(finish, start));
65 #endif
67 #ifdef BAREMETAL
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");
76 #endif
78 return 0;
81 // EOF