test.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 exports procedures for use with the test.py test suite.
  8. */
  9. package math_big_tests
  10. /*
  11. TODO: Write tests for `internal_*` and test reusing parameters with the public implementations.
  12. */
  13. import "base:runtime"
  14. import "core:strings"
  15. import "core:math/big"
  16. PyRes :: struct {
  17. res: cstring,
  18. err: big.Error,
  19. }
  20. print_to_buffer :: proc(val: ^big.Int) -> cstring {
  21. context = runtime.default_context()
  22. r, _ := big.int_itoa_cstring(val, 16, context.allocator)
  23. return r
  24. }
  25. @export test_initialize_constants :: proc "c" () -> (res: u64) {
  26. context = runtime.default_context()
  27. _ = big.initialize_constants()
  28. return u64(big._DIGIT_NAILS)
  29. }
  30. @export test_error_string :: proc "c" (err: big.Error) -> (res: cstring) {
  31. context = runtime.default_context()
  32. es := big.Error_String
  33. return strings.clone_to_cstring(es[err], context.allocator)
  34. }
  35. @export test_add :: proc "c" (a, b: cstring) -> (res: PyRes) {
  36. context = runtime.default_context()
  37. err: big.Error
  38. aa, bb, sum := &big.Int{}, &big.Int{}, &big.Int{}
  39. defer big.internal_destroy(aa, bb, sum)
  40. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":add:atoi(a):", err=err} }
  41. if err = big.atoi(bb, string(b), 16); err != nil { return PyRes{res=":add:atoi(b):", err=err} }
  42. if bb.used == 1 {
  43. if err = #force_inline big.internal_add(sum, aa, bb.digit[0]); err != nil { return PyRes{res=":add:add(sum,a,b):", err=err} }
  44. } else {
  45. if err = #force_inline big.internal_add(sum, aa, bb); err != nil { return PyRes{res=":add:add(sum,a,b):", err=err} }
  46. }
  47. r := print_to_buffer(sum)
  48. return PyRes{res = r, err = nil}
  49. }
  50. @export test_sub :: proc "c" (a, b: cstring) -> (res: PyRes) {
  51. context = runtime.default_context()
  52. err: big.Error
  53. aa, bb, sum := &big.Int{}, &big.Int{}, &big.Int{}
  54. defer big.internal_destroy(aa, bb, sum)
  55. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":sub:atoi(a):", err=err} }
  56. if err = big.atoi(bb, string(b), 16); err != nil { return PyRes{res=":sub:atoi(b):", err=err} }
  57. if bb.used == 1 {
  58. if err = #force_inline big.internal_sub(sum, aa, bb.digit[0]); err != nil { return PyRes{res=":sub:sub(sum,a,b):", err=err} }
  59. } else {
  60. if err = #force_inline big.internal_sub(sum, aa, bb); err != nil { return PyRes{res=":sub:sub(sum,a,b):", err=err} }
  61. }
  62. r := print_to_buffer(sum)
  63. if err != nil { return PyRes{res=":sub:itoa(sum):", err=err} }
  64. return PyRes{res = r, err = nil}
  65. }
  66. @export test_mul :: proc "c" (a, b: cstring) -> (res: PyRes) {
  67. context = runtime.default_context()
  68. err: big.Error
  69. aa, bb, product := &big.Int{}, &big.Int{}, &big.Int{}
  70. defer big.internal_destroy(aa, bb, product)
  71. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":mul:atoi(a):", err=err} }
  72. if err = big.atoi(bb, string(b), 16); err != nil { return PyRes{res=":mul:atoi(b):", err=err} }
  73. if err = #force_inline big.internal_mul(product, aa, bb); err != nil { return PyRes{res=":mul:mul(product,a,b):", err=err} }
  74. r := print_to_buffer(product)
  75. return PyRes{res = r, err = nil}
  76. }
  77. @export test_sqr :: proc "c" (a: cstring) -> (res: PyRes) {
  78. context = runtime.default_context()
  79. err: big.Error
  80. aa, square := &big.Int{}, &big.Int{}
  81. defer big.internal_destroy(aa, square)
  82. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":sqr:atoi(a):", err=err} }
  83. if err = #force_inline big.internal_sqr(square, aa); err != nil { return PyRes{res=":sqr:sqr(square,a):", err=err} }
  84. r := print_to_buffer(square)
  85. return PyRes{res = r, err = nil}
  86. }
  87. /*
  88. NOTE(Jeroen): For simplicity, we don't return the quotient and the remainder, just the quotient.
  89. */
  90. @export test_div :: proc "c" (a, b: cstring) -> (res: PyRes) {
  91. context = runtime.default_context()
  92. err: big.Error
  93. aa, bb, quotient := &big.Int{}, &big.Int{}, &big.Int{}
  94. defer big.internal_destroy(aa, bb, quotient)
  95. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":div:atoi(a):", err=err} }
  96. if err = big.atoi(bb, string(b), 16); err != nil { return PyRes{res=":div:atoi(b):", err=err} }
  97. if err = #force_inline big.internal_div(quotient, aa, bb); err != nil { return PyRes{res=":div:div(quotient,a,b):", err=err} }
  98. r := print_to_buffer(quotient)
  99. return PyRes{res = r, err = nil}
  100. }
  101. /*
  102. res = log(a, base)
  103. */
  104. @export test_log :: proc "c" (a: cstring, base := big.DIGIT(2)) -> (res: PyRes) {
  105. context = runtime.default_context()
  106. err: big.Error
  107. l: int
  108. aa := &big.Int{}
  109. defer big.internal_destroy(aa)
  110. if err = big.atoi(aa, string(a), 16); err != nil { return PyRes{res=":log:atoi(a):", err=err} }
  111. if l, err = #force_inline big.internal_log(aa, base); err != nil { return PyRes{res=":log:log(a, base):", err=err} }
  112. #force_inline big.internal_zero(aa)
  113. aa.digit[0] = big.DIGIT(l) & big._MASK
  114. aa.digit[1] = big.DIGIT(l) >> big._DIGIT_BITS
  115. aa.used = 2
  116. big.clamp(aa)
  117. r := print_to_buffer(aa)
  118. return PyRes{res = r, err = nil}
  119. }
  120. /*
  121. dest = base^power
  122. */
  123. @export test_pow :: proc "c" (base: cstring, power := int(2)) -> (res: PyRes) {
  124. context = runtime.default_context()
  125. err: big.Error
  126. dest, bb := &big.Int{}, &big.Int{}
  127. defer big.internal_destroy(dest, bb)
  128. if err = big.atoi(bb, string(base), 16); err != nil { return PyRes{res=":pow:atoi(base):", err=err} }
  129. if err = #force_inline big.internal_pow(dest, bb, power); err != nil { return PyRes{res=":pow:pow(dest, base, power):", err=err} }
  130. r := print_to_buffer(dest)
  131. return PyRes{res = r, err = nil}
  132. }
  133. /*
  134. dest = sqrt(src)
  135. */
  136. @export test_sqrt :: proc "c" (source: cstring) -> (res: PyRes) {
  137. context = runtime.default_context()
  138. err: big.Error
  139. src := &big.Int{}
  140. defer big.internal_destroy(src)
  141. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":sqrt:atoi(src):", err=err} }
  142. if err = #force_inline big.internal_sqrt(src, src); err != nil { return PyRes{res=":sqrt:sqrt(src):", err=err} }
  143. r := print_to_buffer(src)
  144. return PyRes{res = r, err = nil}
  145. }
  146. /*
  147. dest = root_n(src, power)
  148. */
  149. @export test_root_n :: proc "c" (source: cstring, power: int) -> (res: PyRes) {
  150. context = runtime.default_context()
  151. err: big.Error
  152. src := &big.Int{}
  153. defer big.internal_destroy(src)
  154. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":root_n:atoi(src):", err=err} }
  155. if err = #force_inline big.internal_root_n(src, src, power); err != nil { return PyRes{res=":root_n:root_n(src):", err=err} }
  156. r := print_to_buffer(src)
  157. return PyRes{res = r, err = nil}
  158. }
  159. /*
  160. dest = shr_digit(src, digits)
  161. */
  162. @export test_shr_leg :: proc "c" (source: cstring, digits: int) -> (res: PyRes) {
  163. context = runtime.default_context()
  164. err: big.Error
  165. src := &big.Int{}
  166. defer big.internal_destroy(src)
  167. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":shr_digit:atoi(src):", err=err} }
  168. if err = #force_inline big._private_int_shr_leg(src, digits); err != nil { return PyRes{res=":shr_digit:shr_digit(src):", err=err} }
  169. r := print_to_buffer(src)
  170. return PyRes{res = r, err = nil}
  171. }
  172. /*
  173. dest = shl_digit(src, digits)
  174. */
  175. @export test_shl_leg :: proc "c" (source: cstring, digits: int) -> (res: PyRes) {
  176. context = runtime.default_context()
  177. err: big.Error
  178. src := &big.Int{}
  179. defer big.internal_destroy(src)
  180. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":shl_digit:atoi(src):", err=err} }
  181. if err = #force_inline big._private_int_shl_leg(src, digits); err != nil { return PyRes{res=":shl_digit:shr_digit(src):", err=err} }
  182. r := print_to_buffer(src)
  183. return PyRes{res = r, err = nil}
  184. }
  185. /*
  186. dest = shr(src, bits)
  187. */
  188. @export test_shr :: proc "c" (source: cstring, bits: int) -> (res: PyRes) {
  189. context = runtime.default_context()
  190. err: big.Error
  191. src := &big.Int{}
  192. defer big.internal_destroy(src)
  193. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":shr:atoi(src):", err=err} }
  194. if err = #force_inline big.internal_shr(src, src, bits); err != nil { return PyRes{res=":shr:shr(src, bits):", err=err} }
  195. r := print_to_buffer(src)
  196. return PyRes{res = r, err = nil}
  197. }
  198. /*
  199. dest = shr_signed(src, bits)
  200. */
  201. @export test_shr_signed :: proc "c" (source: cstring, bits: int) -> (res: PyRes) {
  202. context = runtime.default_context()
  203. err: big.Error
  204. src := &big.Int{}
  205. defer big.internal_destroy(src)
  206. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":shr_signed:atoi(src):", err=err} }
  207. if err = #force_inline big.internal_shr_signed(src, src, bits); err != nil { return PyRes{res=":shr_signed:shr_signed(src, bits):", err=err} }
  208. r := print_to_buffer(src)
  209. return PyRes{res = r, err = nil}
  210. }
  211. /*
  212. dest = shl(src, bits)
  213. */
  214. @export test_shl :: proc "c" (source: cstring, bits: int) -> (res: PyRes) {
  215. context = runtime.default_context()
  216. err: big.Error
  217. src := &big.Int{}
  218. defer big.internal_destroy(src)
  219. if err = big.atoi(src, string(source), 16); err != nil { return PyRes{res=":shl:atoi(src):", err=err} }
  220. if err = #force_inline big.internal_shl(src, src, bits); err != nil { return PyRes{res=":shl:shl(src, bits):", err=err} }
  221. r := print_to_buffer(src)
  222. return PyRes{res = r, err = nil}
  223. }
  224. /*
  225. dest = factorial(n)
  226. */
  227. @export test_factorial :: proc "c" (n: int) -> (res: PyRes) {
  228. context = runtime.default_context()
  229. err: big.Error
  230. dest := &big.Int{}
  231. defer big.internal_destroy(dest)
  232. if err = #force_inline big.internal_int_factorial(dest, n); err != nil { return PyRes{res=":factorial:factorial(n):", err=err} }
  233. r := print_to_buffer(dest)
  234. return PyRes{res = r, err = nil}
  235. }
  236. /*
  237. dest = gcd(a, b)
  238. */
  239. @export test_gcd :: proc "c" (a, b: cstring) -> (res: PyRes) {
  240. context = runtime.default_context()
  241. err: big.Error
  242. ai, bi, dest := &big.Int{}, &big.Int{}, &big.Int{}
  243. defer big.internal_destroy(ai, bi, dest)
  244. if err = big.atoi(ai, string(a), 16); err != nil { return PyRes{res=":gcd:atoi(a):", err=err} }
  245. if err = big.atoi(bi, string(b), 16); err != nil { return PyRes{res=":gcd:atoi(b):", err=err} }
  246. if err = #force_inline big.internal_int_gcd_lcm(dest, nil, ai, bi); err != nil { return PyRes{res=":gcd:gcd(a, b):", err=err} }
  247. r := print_to_buffer(dest)
  248. return PyRes{res = r, err = nil}
  249. }
  250. /*
  251. dest = lcm(a, b)
  252. */
  253. @export test_lcm :: proc "c" (a, b: cstring) -> (res: PyRes) {
  254. context = runtime.default_context()
  255. err: big.Error
  256. ai, bi, dest := &big.Int{}, &big.Int{}, &big.Int{}
  257. defer big.internal_destroy(ai, bi, dest)
  258. if err = big.atoi(ai, string(a), 16); err != nil { return PyRes{res=":lcm:atoi(a):", err=err} }
  259. if err = big.atoi(bi, string(b), 16); err != nil { return PyRes{res=":lcm:atoi(b):", err=err} }
  260. if err = #force_inline big.internal_int_gcd_lcm(nil, dest, ai, bi); err != nil { return PyRes{res=":lcm:lcm(a, b):", err=err} }
  261. r := print_to_buffer(dest)
  262. return PyRes{res = r, err = nil}
  263. }
  264. /*
  265. dest = lcm(a, b)
  266. */
  267. @export test_is_square :: proc "c" (a: cstring) -> (res: PyRes) {
  268. context = runtime.default_context()
  269. err: big.Error
  270. square: bool
  271. ai := &big.Int{}
  272. defer big.internal_destroy(ai)
  273. if err = big.atoi(ai, string(a), 16); err != nil { return PyRes{res=":is_square:atoi(a):", err=err} }
  274. if square, err = #force_inline big.internal_int_is_square(ai); err != nil { return PyRes{res=":is_square:is_square(a):", err=err} }
  275. if square {
  276. return PyRes{"True", nil}
  277. }
  278. return PyRes{"False", nil}
  279. }