api.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  31. /*
  32. err = sub(dest, a, b);
  33. */
  34. sub :: proc {
  35. /*
  36. int_sub :: proc(dest, a, b: ^Int) -> (err: Error)
  37. */
  38. int_sub,
  39. /*
  40. int_sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error)
  41. */
  42. int_sub_digit,
  43. }
  44. /*
  45. === === === === === === === === === === === === === === === === === === === === === === === ===
  46. Comparisons.
  47. See `compare.odin`.
  48. === === === === === === === === === === === === === === === === === === === === === === === ===
  49. */
  50. is_initialized :: proc {
  51. /*
  52. int_is_initialized :: proc(a: ^Int) -> bool
  53. */
  54. int_is_initialized,
  55. }
  56. is_zero :: proc {
  57. /*
  58. int_is_zero :: proc(a: ^Int) -> bool
  59. */
  60. int_is_zero,
  61. }
  62. is_positive :: proc {
  63. /*
  64. int_is_positive :: proc(a: ^Int) -> bool
  65. */
  66. int_is_positive,
  67. }
  68. is_pos :: is_positive
  69. is_negative :: proc {
  70. /*
  71. int_is_negative :: proc(a: ^Int) -> bool
  72. */
  73. int_is_negative,
  74. }
  75. is_neg :: is_negative
  76. is_even :: proc {
  77. /*
  78. int_is_even :: proc(a: ^Int) -> bool
  79. */
  80. int_is_even,
  81. }
  82. is_odd :: proc {
  83. /*
  84. int_is_odd :: proc(a: ^Int) -> bool
  85. */
  86. int_is_odd,
  87. }
  88. is_power_of_two :: proc {
  89. /*
  90. platform_int_is_power_of_two :: proc(a: int) -> bool
  91. */
  92. platform_int_is_power_of_two,
  93. /*
  94. int_is_power_of_two :: proc(a: ^Int) -> (res: bool)
  95. */
  96. int_is_power_of_two,
  97. }
  98. compare :: proc {
  99. /*
  100. Compare two `Int`s, signed.
  101. int_compare :: proc(a, b: ^Int) -> Comparison_Flag
  102. */
  103. int_compare,
  104. /*
  105. Compare an `Int` to an unsigned number upto the size of the backing type.
  106. int_compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag
  107. */
  108. int_compare_digit,
  109. }
  110. cmp :: compare
  111. compare_magnitude :: proc {
  112. /*
  113. Compare the magnitude of two `Int`s, unsigned.
  114. */
  115. int_compare_magnitude,
  116. }
  117. cmp_mag :: compare_magnitude
  118. /*
  119. === === === === === === === === === === === === === === === === === === === === === === === ===
  120. Initialization and other helpers.
  121. See `helpers.odin`.
  122. === === === === === === === === === === === === === === === === === === === === === === === ===
  123. */
  124. destroy :: proc {
  125. /*
  126. Clears one or more `Int`s and dellocates their backing memory.
  127. int_destroy :: proc(integers: ..^Int)
  128. */
  129. int_destroy,
  130. }