example.odin 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //+ignore
  2. package big
  3. /*
  4. Copyright 2021 Jeroen van Rijn <[email protected]>.
  5. Made available under Odin's BSD-2 license.
  6. A BigInt implementation in Odin.
  7. For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
  8. The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
  9. */
  10. import "core:fmt"
  11. import "core:mem"
  12. print_configation :: proc() {
  13. fmt.printf(
  14. `Configuration:
  15. DIGIT_BITS %v
  16. MIN_DIGIT_COUNT %v
  17. MAX_DIGIT_COUNT %v
  18. EFAULT_DIGIT_COUNT %v
  19. MAX_COMBA %v
  20. WARRAY %v
  21. MUL_KARATSUBA_CUTOFF %v
  22. SQR_KARATSUBA_CUTOFF %v
  23. MUL_TOOM_CUTOFF %v
  24. SQR_TOOM_CUTOFF %v
  25. `, _DIGIT_BITS,
  26. _MIN_DIGIT_COUNT,
  27. _MAX_DIGIT_COUNT,
  28. _DEFAULT_DIGIT_COUNT,
  29. _MAX_COMBA,
  30. _WARRAY,
  31. _MUL_KARATSUBA_CUTOFF,
  32. _SQR_KARATSUBA_CUTOFF,
  33. _MUL_TOOM_CUTOFF,
  34. _SQR_TOOM_CUTOFF,
  35. );
  36. fmt.println();
  37. }
  38. print :: proc(name: string, a: ^Int, base := i8(16)) {
  39. as, err := itoa(a, base);
  40. defer delete(as);
  41. if err == .OK {
  42. fmt.printf("%v (base: %v, bits used: %v): %v\n", name, base, count_bits(a), as);
  43. } else {
  44. fmt.printf("%v (error: %v): %v\n", name, err, a);
  45. }
  46. }
  47. demo :: proc() {
  48. a, b, c: ^Int;
  49. err: Error;
  50. defer destroy(a);
  51. defer destroy(b);
  52. defer destroy(c);
  53. a, err = init(512);
  54. b, err = init(a);
  55. c, err = init(-4);
  56. print("a", a, 2);
  57. print("b", b, 2);
  58. print("c", c, 2);
  59. fmt.println("=== a = a & b ===");
  60. err = and(a, a, b);
  61. fmt.printf("a &= b error: %v\n", err);
  62. print("a", a, 2);
  63. print("b", b, 10);
  64. fmt.println("\n\n=== b = abs(c) ===");
  65. c.sign = .Negative;
  66. abs(b, c); // copy c to b.
  67. print("b", b);
  68. print("c", c);
  69. fmt.println("\n\n=== Set a to (1 << 120) - 1 ===");
  70. if err = power_of_two(a, 120); err != .OK {
  71. fmt.printf("Error %v while setting a to 1 << 120.\n", err);
  72. }
  73. if err = sub(a, a, 1); err != .OK {
  74. fmt.printf("Error %v while subtracting 1 from a\n", err);
  75. }
  76. print("a", a, 16);
  77. fmt.println("Expected a to be: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
  78. }
  79. main :: proc() {
  80. ta := mem.Tracking_Allocator{};
  81. mem.tracking_allocator_init(&ta, context.allocator);
  82. context.allocator = mem.tracking_allocator(&ta);
  83. // print_configation();
  84. demo();
  85. if len(ta.allocation_map) > 0 {
  86. for _, v in ta.allocation_map {
  87. fmt.printf("Leaked %v bytes @ %v\n", v.size, v.location);
  88. }
  89. }
  90. if len(ta.bad_free_array) > 0 {
  91. fmt.println("Bad frees:");
  92. for v in ta.bad_free_array {
  93. fmt.println(v);
  94. }
  95. }
  96. }