api.odin 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright 2021 Jeroen van Rijn <[email protected]>.
  3. Made available under Odin's BSD-3 license.
  4. An arbitrary precision mathematics implementation in Odin.
  5. For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
  6. The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
  7. This file collects public proc maps and their aliases.
  8. */
  9. package math_big
  10. /*
  11. === === === === === === === === === === === === === === === === === === === === === === === ===
  12. Basic arithmetic.
  13. See `public.odin`.
  14. === === === === === === === === === === === === === === === === === === === === === === === ===
  15. */
  16. /*
  17. High-level addition. Handles sign.
  18. */
  19. add :: proc {
  20. /*
  21. int_add :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error)
  22. */
  23. int_add,
  24. /*
  25. Adds the unsigned `DIGIT` immediate to an `Int`, such that the
  26. `DIGIT` doesn't have to be turned into an `Int` first.
  27. int_add_digit :: proc(dest, a: ^Int, digit: DIGIT, allocator := context.allocator) -> (err: Error)
  28. */
  29. int_add_digit,
  30. rat_add_rat,
  31. rat_add_int,
  32. int_add_rat,
  33. }
  34. /*
  35. err = sub(dest, a, b);
  36. */
  37. sub :: proc {
  38. /*
  39. int_sub :: proc(dest, a, b: ^Int) -> (err: Error)
  40. */
  41. int_sub,
  42. /*
  43. int_sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error)
  44. */
  45. int_sub_digit,
  46. rat_sub_rat,
  47. rat_sub_int,
  48. int_sub_rat,
  49. }
  50. /*
  51. === === === === === === === === === === === === === === === === === === === === === === === ===
  52. Comparisons.
  53. See `compare.odin`.
  54. === === === === === === === === === === === === === === === === === === === === === === === ===
  55. */
  56. is_initialized :: proc {
  57. /*
  58. int_is_initialized :: proc(a: ^Int) -> bool
  59. */
  60. int_is_initialized,
  61. }
  62. is_zero :: proc {
  63. /*
  64. int_is_zero :: proc(a: ^Int) -> bool
  65. */
  66. int_is_zero,
  67. /*
  68. rat_is_zero :: proc(a: ^Rat) -> bool
  69. */
  70. rat_is_zero,
  71. }
  72. is_positive :: proc {
  73. /*
  74. int_is_positive :: proc(a: ^Int) -> bool
  75. */
  76. int_is_positive,
  77. rat_is_positive,
  78. }
  79. is_pos :: is_positive
  80. is_negative :: proc {
  81. /*
  82. int_is_negative :: proc(a: ^Int) -> bool
  83. */
  84. int_is_negative,
  85. rat_is_negative,
  86. }
  87. is_neg :: is_negative
  88. is_even :: proc {
  89. /*
  90. int_is_even :: proc(a: ^Int) -> bool
  91. */
  92. int_is_even,
  93. rat_is_even,
  94. }
  95. is_odd :: proc {
  96. /*
  97. int_is_odd :: proc(a: ^Int) -> bool
  98. */
  99. int_is_odd,
  100. rat_is_odd,
  101. }
  102. is_power_of_two :: proc {
  103. /*
  104. platform_int_is_power_of_two :: proc(a: int) -> bool
  105. */
  106. platform_int_is_power_of_two,
  107. /*
  108. int_is_power_of_two :: proc(a: ^Int) -> (res: bool)
  109. */
  110. int_is_power_of_two,
  111. }
  112. compare :: proc {
  113. /*
  114. Compare two `Int`s, signed.
  115. int_compare :: proc(a, b: ^Int) -> Comparison_Flag
  116. */
  117. int_compare,
  118. /*
  119. Compare an `Int` to an unsigned number upto the size of the backing type.
  120. int_compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag
  121. */
  122. int_compare_digit,
  123. }
  124. cmp :: compare
  125. compare_magnitude :: proc {
  126. /*
  127. Compare the magnitude of two `Int`s, unsigned.
  128. */
  129. int_compare_magnitude,
  130. }
  131. cmp_mag :: compare_magnitude
  132. /*
  133. === === === === === === === === === === === === === === === === === === === === === === === ===
  134. Initialization and other helpers.
  135. See `helpers.odin`.
  136. === === === === === === === === === === === === === === === === === === === === === === === ===
  137. */
  138. destroy :: proc {
  139. /*
  140. Clears one or more `Int`s and dellocates their backing memory.
  141. int_destroy :: proc(integers: ..^Int)
  142. */
  143. int_destroy,
  144. }