example.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //+ignore
  2. /*
  3. Copyright 2021 Jeroen van Rijn <[email protected]>.
  4. Made available under Odin's BSD-3 license.
  5. A BigInt implementation in Odin.
  6. For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
  7. The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
  8. */
  9. package math_big
  10. import "core:fmt"
  11. import "core:mem"
  12. print_configation :: proc() {
  13. fmt.printf(
  14. `
  15. Configuration:
  16. _DIGIT_BITS %v
  17. _SMALL_MEMORY %v
  18. _MIN_DIGIT_COUNT %v
  19. _MAX_DIGIT_COUNT %v
  20. _DEFAULT_DIGIT_COUNT %v
  21. _MAX_COMBA %v
  22. _WARRAY %v
  23. _TAB_SIZE %v
  24. _MAX_WIN_SIZE %v
  25. MATH_BIG_USE_FROBENIUS_TEST %v
  26. Runtime tunable:
  27. MUL_KARATSUBA_CUTOFF %v
  28. SQR_KARATSUBA_CUTOFF %v
  29. MUL_TOOM_CUTOFF %v
  30. SQR_TOOM_CUTOFF %v
  31. MAX_ITERATIONS_ROOT_N %v
  32. FACTORIAL_MAX_N %v
  33. FACTORIAL_BINARY_SPLIT_CUTOFF %v
  34. FACTORIAL_BINARY_SPLIT_MAX_RECURSIONS %v
  35. USE_MILLER_RABIN_ONLY %v
  36. `, _DIGIT_BITS,
  37. _LOW_MEMORY,
  38. _MIN_DIGIT_COUNT,
  39. _MAX_DIGIT_COUNT,
  40. _DEFAULT_DIGIT_COUNT,
  41. _MAX_COMBA,
  42. _WARRAY,
  43. _TAB_SIZE,
  44. _MAX_WIN_SIZE,
  45. MATH_BIG_USE_FROBENIUS_TEST,
  46. MUL_KARATSUBA_CUTOFF,
  47. SQR_KARATSUBA_CUTOFF,
  48. MUL_TOOM_CUTOFF,
  49. SQR_TOOM_CUTOFF,
  50. MAX_ITERATIONS_ROOT_N,
  51. FACTORIAL_MAX_N,
  52. FACTORIAL_BINARY_SPLIT_CUTOFF,
  53. FACTORIAL_BINARY_SPLIT_MAX_RECURSIONS,
  54. USE_MILLER_RABIN_ONLY,
  55. );
  56. }
  57. print :: proc(name: string, a: ^Int, base := i8(10), print_name := true, newline := true, print_extra_info := false) {
  58. assert_if_nil(a);
  59. as, err := itoa(a, base);
  60. defer delete(as);
  61. cb := internal_count_bits(a);
  62. if print_name {
  63. fmt.printf("%v", name);
  64. }
  65. if err != nil {
  66. fmt.printf("%v (error: %v | %v)", name, err, a);
  67. }
  68. fmt.printf("%v", as);
  69. if print_extra_info {
  70. fmt.printf(" (base: %v, bits: %v (digits: %v), flags: %v)", base, cb, a.used, a.flags);
  71. }
  72. if newline {
  73. fmt.println();
  74. }
  75. }
  76. //printf :: fmt.printf;
  77. demo :: proc() {
  78. a, b, c, d, e, f, res := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{};
  79. defer destroy(a, b, c, d, e, f, res);
  80. err: Error;
  81. frob: bool;
  82. prime: bool;
  83. // USE_MILLER_RABIN_ONLY = true;
  84. // set(a, "3317044064679887385961979"); // Composite: 17 × 1709 × 1366183751 × 83570142193
  85. set(a, "359334085968622831041960188598043661065388726959079837"); // 6th Bell prime
  86. trials := number_of_rabin_miller_trials(internal_count_bits(a));
  87. {
  88. SCOPED_TIMING(.is_prime);
  89. prime, err = internal_int_is_prime(a, trials);
  90. }
  91. print("Candidate prime: ", a);
  92. fmt.printf("%v Miller-Rabin trials needed.\n", trials);
  93. frob, err = internal_int_prime_frobenius_underwood(a);
  94. fmt.printf("Frobenius-Underwood: %v, Prime: %v, Error: %v\n", frob, prime, err);
  95. }
  96. main :: proc() {
  97. ta := mem.Tracking_Allocator{};
  98. mem.tracking_allocator_init(&ta, context.allocator);
  99. context.allocator = mem.tracking_allocator(&ta);
  100. demo();
  101. print_configation();
  102. print_timings();
  103. if len(ta.allocation_map) > 0 {
  104. for _, v in ta.allocation_map {
  105. fmt.printf("Leaked %v bytes @ %v\n", v.size, v.location);
  106. }
  107. }
  108. if len(ta.bad_free_array) > 0 {
  109. fmt.println("Bad frees:");
  110. for v in ta.bad_free_array {
  111. fmt.println(v);
  112. }
  113. }
  114. }