1
0

sieve.c 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "vm.h"
  2. #include "libcflat.h"
  3. int sieve(char* data, int size)
  4. {
  5. int i, j, r = 0;
  6. for (i = 0; i < size; ++i)
  7. data[i] = 1;
  8. data[0] = data[1] = 0;
  9. for (i = 2; i < size; ++i)
  10. if (data[i]) {
  11. ++r;
  12. for (j = i*2; j < size; j += i)
  13. data[j] = 0;
  14. }
  15. return r;
  16. }
  17. void test_sieve(const char *msg, char *data, int size)
  18. {
  19. int r;
  20. printf("%s:", msg);
  21. r = sieve(data, size);
  22. printf("%d out of %d\n", r, size);
  23. }
  24. #define STATIC_SIZE 1000000
  25. #define VSIZE 2000000
  26. char static_data[STATIC_SIZE];
  27. int main()
  28. {
  29. void *v;
  30. int i;
  31. printf("starting sieve\n");
  32. test_sieve("static", static_data, STATIC_SIZE);
  33. setup_vm();
  34. test_sieve("mapped", static_data, STATIC_SIZE);
  35. for (i = 0; i < 3; ++i) {
  36. v = vmalloc(VSIZE);
  37. test_sieve("virtual", v, VSIZE);
  38. vfree(v);
  39. }
  40. return 0;
  41. }