internal.odin 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  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. ========================== Low-level routines ==========================
  9. IMPORTANT: `internal_*` procedures make certain assumptions about their input.
  10. The public functions that call them are expected to satisfy their sanity check requirements.
  11. This allows `internal_*` call `internal_*` without paying this overhead multiple times.
  12. Where errors can occur, they are of course still checked and returned as appropriate.
  13. When importing `math:core/big` to implement an involved algorithm of your own, you are welcome
  14. to use these procedures instead of their public counterparts.
  15. Most inputs and outputs are expected to be passed an initialized `Int`, for example.
  16. Exceptions include `quotient` and `remainder`, which are allowed to be `nil` when the calling code doesn't need them.
  17. Check the comments above each `internal_*` implementation to see what constraints it expects to have met.
  18. We pass the custom allocator to procedures by default using the pattern `context.allocator = allocator`.
  19. This way we don't have to add `, allocator` at the end of each call.
  20. TODO: Handle +/- Infinity and NaN.
  21. */
  22. package math_big
  23. import "core:mem"
  24. import "core:intrinsics"
  25. import rnd "core:math/rand"
  26. /*
  27. Low-level addition, unsigned. Handbook of Applied Cryptography, algorithm 14.7.
  28. Assumptions:
  29. `dest`, `a` and `b` != `nil` and have been initalized.
  30. */
  31. internal_int_add_unsigned :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  32. dest := dest; x := a; y := b
  33. context.allocator = allocator
  34. old_used, min_used, max_used, i: int
  35. if x.used < y.used {
  36. x, y = y, x
  37. }
  38. min_used = y.used
  39. max_used = x.used
  40. old_used = dest.used
  41. internal_grow(dest, max(max_used + 1, _DEFAULT_DIGIT_COUNT)) or_return
  42. dest.used = max_used + 1
  43. /*
  44. All parameters have been initialized.
  45. */
  46. /* Zero the carry */
  47. carry := DIGIT(0)
  48. #no_bounds_check for i = 0; i < min_used; i += 1 {
  49. /*
  50. Compute the sum one _DIGIT at a time.
  51. dest[i] = a[i] + b[i] + carry;
  52. */
  53. dest.digit[i] = x.digit[i] + y.digit[i] + carry
  54. /*
  55. Compute carry
  56. */
  57. carry = dest.digit[i] >> _DIGIT_BITS
  58. /*
  59. Mask away carry from result digit.
  60. */
  61. dest.digit[i] &= _MASK
  62. }
  63. if min_used != max_used {
  64. /*
  65. Now copy higher words, if any, in A+B.
  66. If A or B has more digits, add those in.
  67. */
  68. #no_bounds_check for ; i < max_used; i += 1 {
  69. dest.digit[i] = x.digit[i] + carry
  70. /*
  71. Compute carry
  72. */
  73. carry = dest.digit[i] >> _DIGIT_BITS
  74. /*
  75. Mask away carry from result digit.
  76. */
  77. dest.digit[i] &= _MASK
  78. }
  79. }
  80. /*
  81. Add remaining carry.
  82. */
  83. dest.digit[i] = carry
  84. /*
  85. Zero remainder.
  86. */
  87. internal_zero_unused(dest, old_used)
  88. /*
  89. Adjust dest.used based on leading zeroes.
  90. */
  91. return internal_clamp(dest)
  92. }
  93. internal_add_unsigned :: proc { internal_int_add_unsigned, }
  94. /*
  95. Low-level addition, signed. Handbook of Applied Cryptography, algorithm 14.7.
  96. Assumptions:
  97. `dest`, `a` and `b` != `nil` and have been initalized.
  98. */
  99. internal_int_add_signed :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  100. x := a; y := b
  101. context.allocator = allocator
  102. /*
  103. Handle both negative or both positive.
  104. */
  105. if x.sign == y.sign {
  106. dest.sign = x.sign
  107. return #force_inline internal_int_add_unsigned(dest, x, y)
  108. }
  109. /*
  110. One positive, the other negative.
  111. Subtract the one with the greater magnitude from the other.
  112. The result gets the sign of the one with the greater magnitude.
  113. */
  114. if #force_inline internal_lt_abs(a, b) {
  115. x, y = y, x
  116. }
  117. dest.sign = x.sign
  118. return #force_inline internal_int_sub_unsigned(dest, x, y)
  119. }
  120. internal_add_signed :: proc { internal_int_add_signed, }
  121. /*
  122. Low-level addition Int+DIGIT, signed. Handbook of Applied Cryptography, algorithm 14.7.
  123. Assumptions:
  124. `dest` and `a` != `nil` and have been initalized.
  125. `dest` is large enough (a.used + 1) to fit result.
  126. */
  127. internal_int_add_digit :: proc(dest, a: ^Int, digit: DIGIT, allocator := context.allocator) -> (err: Error) {
  128. context.allocator = allocator
  129. internal_grow(dest, a.used + 1) or_return
  130. /*
  131. Fast paths for destination and input Int being the same.
  132. */
  133. if dest == a {
  134. /*
  135. Fast path for dest.digit[0] + digit fits in dest.digit[0] without overflow.
  136. */
  137. if dest.sign == .Zero_or_Positive && (dest.digit[0] + digit < _DIGIT_MAX) {
  138. dest.digit[0] += digit
  139. dest.used += 1
  140. return internal_clamp(dest)
  141. }
  142. /*
  143. Can be subtracted from dest.digit[0] without underflow.
  144. */
  145. if a.sign == .Negative && (dest.digit[0] > digit) {
  146. dest.digit[0] -= digit
  147. dest.used += 1
  148. return internal_clamp(dest)
  149. }
  150. }
  151. /*
  152. If `a` is negative and `|a|` >= `digit`, call `dest = |a| - digit`
  153. */
  154. if a.sign == .Negative && (a.used > 1 || a.digit[0] >= digit) {
  155. /*
  156. Temporarily fix `a`'s sign.
  157. */
  158. a.sign = .Zero_or_Positive
  159. /*
  160. dest = |a| - digit
  161. */
  162. if err = #force_inline internal_int_add_digit(dest, a, digit); err != nil {
  163. /*
  164. Restore a's sign.
  165. */
  166. a.sign = .Negative
  167. return err
  168. }
  169. /*
  170. Restore sign and set `dest` sign.
  171. */
  172. a.sign = .Negative
  173. dest.sign = .Negative
  174. return internal_clamp(dest)
  175. }
  176. /*
  177. Remember the currently used number of digits in `dest`.
  178. */
  179. old_used := dest.used
  180. /*
  181. If `a` is positive
  182. */
  183. if a.sign == .Zero_or_Positive {
  184. /*
  185. Add digits, use `carry`.
  186. */
  187. i: int
  188. carry := digit
  189. #no_bounds_check for i = 0; i < a.used; i += 1 {
  190. dest.digit[i] = a.digit[i] + carry
  191. carry = dest.digit[i] >> _DIGIT_BITS
  192. dest.digit[i] &= _MASK
  193. }
  194. /*
  195. Set final carry.
  196. */
  197. dest.digit[i] = carry
  198. /*
  199. Set `dest` size.
  200. */
  201. dest.used = a.used + 1
  202. } else {
  203. /*
  204. `a` was negative and |a| < digit.
  205. */
  206. dest.used = 1
  207. /*
  208. The result is a single DIGIT.
  209. */
  210. dest.digit[0] = digit - a.digit[0] if a.used == 1 else digit
  211. }
  212. /*
  213. Sign is always positive.
  214. */
  215. dest.sign = .Zero_or_Positive
  216. /*
  217. Zero remainder.
  218. */
  219. internal_zero_unused(dest, old_used)
  220. /*
  221. Adjust dest.used based on leading zeroes.
  222. */
  223. return internal_clamp(dest)
  224. }
  225. internal_add :: proc { internal_int_add_signed, internal_int_add_digit, }
  226. internal_int_incr :: proc(dest: ^Int, allocator := context.allocator) -> (err: Error) {
  227. return #force_inline internal_add(dest, dest, 1)
  228. }
  229. internal_incr :: proc { internal_int_incr, }
  230. /*
  231. Low-level subtraction, dest = number - decrease. Assumes |number| > |decrease|.
  232. Handbook of Applied Cryptography, algorithm 14.9.
  233. Assumptions:
  234. `dest`, `number` and `decrease` != `nil` and have been initalized.
  235. */
  236. internal_int_sub_unsigned :: proc(dest, number, decrease: ^Int, allocator := context.allocator) -> (err: Error) {
  237. context.allocator = allocator
  238. dest := dest; x := number; y := decrease
  239. old_used := dest.used
  240. min_used := y.used
  241. max_used := x.used
  242. i: int
  243. grow(dest, max(max_used, _DEFAULT_DIGIT_COUNT)) or_return
  244. dest.used = max_used
  245. /*
  246. All parameters have been initialized.
  247. */
  248. borrow := DIGIT(0)
  249. #no_bounds_check for i = 0; i < min_used; i += 1 {
  250. dest.digit[i] = (x.digit[i] - y.digit[i] - borrow)
  251. /*
  252. borrow = carry bit of dest[i]
  253. Note this saves performing an AND operation since if a carry does occur,
  254. it will propagate all the way to the MSB.
  255. As a result a single shift is enough to get the carry.
  256. */
  257. borrow = dest.digit[i] >> ((size_of(DIGIT) * 8) - 1)
  258. /*
  259. Clear borrow from dest[i].
  260. */
  261. dest.digit[i] &= _MASK
  262. }
  263. /*
  264. Now copy higher words if any, e.g. if A has more digits than B
  265. */
  266. #no_bounds_check for ; i < max_used; i += 1 {
  267. dest.digit[i] = x.digit[i] - borrow
  268. /*
  269. borrow = carry bit of dest[i]
  270. Note this saves performing an AND operation since if a carry does occur,
  271. it will propagate all the way to the MSB.
  272. As a result a single shift is enough to get the carry.
  273. */
  274. borrow = dest.digit[i] >> ((size_of(DIGIT) * 8) - 1)
  275. /*
  276. Clear borrow from dest[i].
  277. */
  278. dest.digit[i] &= _MASK
  279. }
  280. /*
  281. Zero remainder.
  282. */
  283. internal_zero_unused(dest, old_used)
  284. /*
  285. Adjust dest.used based on leading zeroes.
  286. */
  287. return internal_clamp(dest)
  288. }
  289. internal_sub_unsigned :: proc { internal_int_sub_unsigned, }
  290. /*
  291. Low-level subtraction, signed. Handbook of Applied Cryptography, algorithm 14.9.
  292. dest = number - decrease. Assumes |number| > |decrease|.
  293. Assumptions:
  294. `dest`, `number` and `decrease` != `nil` and have been initalized.
  295. */
  296. internal_int_sub_signed :: proc(dest, number, decrease: ^Int, allocator := context.allocator) -> (err: Error) {
  297. context.allocator = allocator
  298. number := number; decrease := decrease
  299. if number.sign != decrease.sign {
  300. /*
  301. Subtract a negative from a positive, OR subtract a positive from a negative.
  302. In either case, ADD their magnitudes and use the sign of the first number.
  303. */
  304. dest.sign = number.sign
  305. return #force_inline internal_int_add_unsigned(dest, number, decrease)
  306. }
  307. /*
  308. Subtract a positive from a positive, OR negative from a negative.
  309. First, take the difference between their magnitudes, then...
  310. */
  311. if #force_inline internal_lt_abs(number, decrease) {
  312. /*
  313. The second has a larger magnitude.
  314. The result has the *opposite* sign from the first number.
  315. */
  316. dest.sign = .Negative if number.sign == .Zero_or_Positive else .Zero_or_Positive
  317. number, decrease = decrease, number
  318. } else {
  319. /*
  320. The first has a larger or equal magnitude.
  321. Copy the sign from the first.
  322. */
  323. dest.sign = number.sign
  324. }
  325. return #force_inline internal_int_sub_unsigned(dest, number, decrease)
  326. }
  327. /*
  328. Low-level subtraction, signed. Handbook of Applied Cryptography, algorithm 14.9.
  329. dest = number - decrease. Assumes |number| > |decrease|.
  330. Assumptions:
  331. `dest`, `number` != `nil` and have been initalized.
  332. `dest` is large enough (number.used + 1) to fit result.
  333. */
  334. internal_int_sub_digit :: proc(dest, number: ^Int, digit: DIGIT, allocator := context.allocator) -> (err: Error) {
  335. context.allocator = allocator
  336. internal_grow(dest, number.used + 1) or_return
  337. dest := dest; digit := digit
  338. /*
  339. All parameters have been initialized.
  340. Fast paths for destination and input Int being the same.
  341. */
  342. if dest == number {
  343. /*
  344. Fast path for `dest` is negative and unsigned addition doesn't overflow the lowest digit.
  345. */
  346. if dest.sign == .Negative && (dest.digit[0] + digit < _DIGIT_MAX) {
  347. dest.digit[0] += digit
  348. return nil
  349. }
  350. /*
  351. Can be subtracted from dest.digit[0] without underflow.
  352. */
  353. if number.sign == .Zero_or_Positive && (dest.digit[0] > digit) {
  354. dest.digit[0] -= digit
  355. return nil
  356. }
  357. }
  358. /*
  359. If `a` is negative, just do an unsigned addition (with fudged signs).
  360. */
  361. if number.sign == .Negative {
  362. t := number
  363. t.sign = .Zero_or_Positive
  364. err = #force_inline internal_int_add_digit(dest, t, digit)
  365. dest.sign = .Negative
  366. internal_clamp(dest)
  367. return err
  368. }
  369. old_used := dest.used
  370. /*
  371. if `a`<= digit, simply fix the single digit.
  372. */
  373. if number.used == 1 && (number.digit[0] <= digit) || number.used == 0 {
  374. dest.digit[0] = digit - number.digit[0] if number.used == 1 else digit
  375. dest.sign = .Negative
  376. dest.used = 1
  377. } else {
  378. dest.sign = .Zero_or_Positive
  379. dest.used = number.used
  380. /*
  381. Subtract with carry.
  382. */
  383. carry := digit
  384. #no_bounds_check for i := 0; i < number.used; i += 1 {
  385. dest.digit[i] = number.digit[i] - carry
  386. carry = dest.digit[i] >> (_DIGIT_TYPE_BITS - 1)
  387. dest.digit[i] &= _MASK
  388. }
  389. }
  390. /*
  391. Zero remainder.
  392. */
  393. internal_zero_unused(dest, old_used)
  394. /*
  395. Adjust dest.used based on leading zeroes.
  396. */
  397. return internal_clamp(dest)
  398. }
  399. internal_sub :: proc { internal_int_sub_signed, internal_int_sub_digit, }
  400. internal_int_decr :: proc(dest: ^Int, allocator := context.allocator) -> (err: Error) {
  401. return #force_inline internal_sub(dest, dest, 1)
  402. }
  403. internal_decr :: proc { internal_int_decr, }
  404. /*
  405. dest = src / 2
  406. dest = src >> 1
  407. Assumes `dest` and `src` not to be `nil` and have been initialized.
  408. We make no allocations here.
  409. */
  410. internal_int_shr1 :: proc(dest, src: ^Int) -> (err: Error) {
  411. old_used := dest.used; dest.used = src.used
  412. /*
  413. Carry
  414. */
  415. fwd_carry := DIGIT(0)
  416. #no_bounds_check for x := dest.used - 1; x >= 0; x -= 1 {
  417. /*
  418. Get the carry for the next iteration.
  419. */
  420. src_digit := src.digit[x]
  421. carry := src_digit & 1
  422. /*
  423. Shift the current digit, add in carry and store.
  424. */
  425. dest.digit[x] = (src_digit >> 1) | (fwd_carry << (_DIGIT_BITS - 1))
  426. /*
  427. Forward carry to next iteration.
  428. */
  429. fwd_carry = carry
  430. }
  431. /*
  432. Zero remainder.
  433. */
  434. internal_zero_unused(dest, old_used)
  435. /*
  436. Adjust dest.used based on leading zeroes.
  437. */
  438. dest.sign = src.sign
  439. return internal_clamp(dest)
  440. }
  441. /*
  442. dest = src * 2
  443. dest = src << 1
  444. */
  445. internal_int_shl1 :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error) {
  446. context.allocator = allocator
  447. internal_copy(dest, src) or_return
  448. /*
  449. Grow `dest` to accommodate the additional bits.
  450. */
  451. digits_needed := dest.used + 1
  452. internal_grow(dest, digits_needed) or_return
  453. dest.used = digits_needed
  454. mask := (DIGIT(1) << uint(1)) - DIGIT(1)
  455. shift := DIGIT(_DIGIT_BITS - 1)
  456. carry := DIGIT(0)
  457. #no_bounds_check for x:= 0; x < dest.used; x+= 1 {
  458. fwd_carry := (dest.digit[x] >> shift) & mask
  459. dest.digit[x] = (dest.digit[x] << uint(1) | carry) & _MASK
  460. carry = fwd_carry
  461. }
  462. /*
  463. Use final carry.
  464. */
  465. if carry != 0 {
  466. dest.digit[dest.used] = carry
  467. dest.used += 1
  468. }
  469. return internal_clamp(dest)
  470. }
  471. /*
  472. Multiply bigint `a` with int `d` and put the result in `dest`.
  473. Like `internal_int_mul_digit` but with an integer as the small input.
  474. */
  475. internal_int_mul_integer :: proc(dest, a: ^Int, b: $T, allocator := context.allocator) -> (err: Error)
  476. where intrinsics.type_is_integer(T) && T != DIGIT {
  477. context.allocator = allocator
  478. t := &Int{}
  479. defer internal_destroy(t)
  480. /*
  481. DIGIT might be smaller than a long, which excludes the use of `internal_int_mul_digit` here.
  482. */
  483. internal_set(t, b) or_return
  484. internal_mul(dest, a, t) or_return
  485. return
  486. }
  487. /*
  488. Multiply by a DIGIT.
  489. */
  490. internal_int_mul_digit :: proc(dest, src: ^Int, multiplier: DIGIT, allocator := context.allocator) -> (err: Error) {
  491. context.allocator = allocator
  492. assert_if_nil(dest, src)
  493. if multiplier == 0 {
  494. return internal_zero(dest)
  495. }
  496. if multiplier == 1 {
  497. return internal_copy(dest, src)
  498. }
  499. /*
  500. Power of two?
  501. */
  502. if multiplier == 2 {
  503. return #force_inline internal_int_shl1(dest, src)
  504. }
  505. if #force_inline platform_int_is_power_of_two(int(multiplier)) {
  506. ix := internal_log(multiplier, 2) or_return
  507. return internal_shl(dest, src, ix)
  508. }
  509. /*
  510. Ensure `dest` is big enough to hold `src` * `multiplier`.
  511. */
  512. grow(dest, max(src.used + 1, _DEFAULT_DIGIT_COUNT)) or_return
  513. /*
  514. Save the original used count.
  515. */
  516. old_used := dest.used
  517. /*
  518. Set the sign.
  519. */
  520. dest.sign = src.sign
  521. /*
  522. Set up carry.
  523. */
  524. carry := _WORD(0)
  525. /*
  526. Compute columns.
  527. */
  528. ix := 0
  529. #no_bounds_check for ; ix < src.used; ix += 1 {
  530. /*
  531. Compute product and carry sum for this term
  532. */
  533. product := carry + _WORD(src.digit[ix]) * _WORD(multiplier)
  534. /*
  535. Mask off higher bits to get a single DIGIT.
  536. */
  537. dest.digit[ix] = DIGIT(product & _WORD(_MASK))
  538. /*
  539. Send carry into next iteration
  540. */
  541. carry = product >> _DIGIT_BITS
  542. }
  543. /*
  544. Store final carry [if any] and increment used.
  545. */
  546. dest.digit[ix] = DIGIT(carry)
  547. dest.used = src.used + 1
  548. /*
  549. Zero remainder.
  550. */
  551. internal_zero_unused(dest, old_used)
  552. return internal_clamp(dest)
  553. }
  554. /*
  555. High level multiplication (handles sign).
  556. */
  557. internal_int_mul :: proc(dest, src, multiplier: ^Int, allocator := context.allocator) -> (err: Error) {
  558. context.allocator = allocator
  559. /*
  560. Early out for `multiplier` is zero; Set `dest` to zero.
  561. */
  562. if multiplier.used == 0 || src.used == 0 { return internal_zero(dest) }
  563. neg := src.sign != multiplier.sign
  564. if src == multiplier {
  565. /*
  566. Do we need to square?
  567. */
  568. if src.used >= SQR_TOOM_CUTOFF {
  569. /*
  570. Use Toom-Cook?
  571. */
  572. err = #force_inline _private_int_sqr_toom(dest, src)
  573. } else if src.used >= SQR_KARATSUBA_CUTOFF {
  574. /*
  575. Karatsuba?
  576. */
  577. err = #force_inline _private_int_sqr_karatsuba(dest, src)
  578. } else if ((src.used * 2) + 1) < _WARRAY && src.used < (_MAX_COMBA / 2) {
  579. /*
  580. Fast comba?
  581. */
  582. err = #force_inline _private_int_sqr_comba(dest, src)
  583. } else {
  584. err = #force_inline _private_int_sqr(dest, src)
  585. }
  586. } else {
  587. /*
  588. Can we use the balance method? Check sizes.
  589. * The smaller one needs to be larger than the Karatsuba cut-off.
  590. * The bigger one needs to be at least about one `_MUL_KARATSUBA_CUTOFF` bigger
  591. * to make some sense, but it depends on architecture, OS, position of the stars... so YMMV.
  592. * Using it to cut the input into slices small enough for _mul_comba
  593. * was actually slower on the author's machine, but YMMV.
  594. */
  595. min_used := min(src.used, multiplier.used)
  596. max_used := max(src.used, multiplier.used)
  597. digits := src.used + multiplier.used + 1
  598. if min_used >= MUL_KARATSUBA_CUTOFF && (max_used / 2) >= MUL_KARATSUBA_CUTOFF && max_used >= (2 * min_used) {
  599. /*
  600. Not much effect was observed below a ratio of 1:2, but again: YMMV.
  601. */
  602. err = _private_int_mul_balance(dest, src, multiplier)
  603. } else if min_used >= MUL_TOOM_CUTOFF {
  604. /*
  605. Toom path commented out until it no longer fails Factorial 10k or 100k,
  606. as reveaved in the long test.
  607. */
  608. err = #force_inline _private_int_mul_toom(dest, src, multiplier)
  609. } else if min_used >= MUL_KARATSUBA_CUTOFF {
  610. err = #force_inline _private_int_mul_karatsuba(dest, src, multiplier)
  611. } else if digits < _WARRAY && min_used <= _MAX_COMBA {
  612. /*
  613. Can we use the fast multiplier?
  614. * The fast multiplier can be used if the output will
  615. * have less than MP_WARRAY digits and the number of
  616. * digits won't affect carry propagation
  617. */
  618. err = #force_inline _private_int_mul_comba(dest, src, multiplier, digits)
  619. } else {
  620. err = #force_inline _private_int_mul(dest, src, multiplier, digits)
  621. }
  622. }
  623. dest.sign = .Negative if dest.used > 0 && neg else .Zero_or_Positive
  624. return err
  625. }
  626. internal_mul :: proc { internal_int_mul, internal_int_mul_digit, internal_int_mul_integer }
  627. internal_sqr :: proc (dest, src: ^Int, allocator := context.allocator) -> (res: Error) {
  628. /*
  629. We call `internal_mul` and not e.g. `_private_int_sqr` because the former
  630. will dispatch to the optimal implementation depending on the source.
  631. */
  632. return #force_inline internal_mul(dest, src, src, allocator)
  633. }
  634. /*
  635. divmod.
  636. Both the quotient and remainder are optional and may be passed a nil.
  637. `numerator` and `denominator` are expected not to be `nil` and have been initialized.
  638. */
  639. internal_int_divmod :: proc(quotient, remainder, numerator, denominator: ^Int, allocator := context.allocator) -> (err: Error) {
  640. context.allocator = allocator
  641. if denominator.used == 0 { return .Division_by_Zero }
  642. /*
  643. If numerator < denominator then quotient = 0, remainder = numerator.
  644. */
  645. if #force_inline internal_lt_abs(numerator, denominator) {
  646. if remainder != nil {
  647. internal_copy(remainder, numerator) or_return
  648. }
  649. if quotient != nil {
  650. internal_zero(quotient)
  651. }
  652. return nil
  653. }
  654. if (denominator.used > 2 * MUL_KARATSUBA_CUTOFF) && (denominator.used <= (numerator.used / 3) * 2) {
  655. assert(denominator.used >= 160 && numerator.used >= 240, "MUL_KARATSUBA_CUTOFF global not properly set.")
  656. err = _private_int_div_recursive(quotient, remainder, numerator, denominator)
  657. } else {
  658. when true {
  659. err = #force_inline _private_int_div_school(quotient, remainder, numerator, denominator)
  660. } else {
  661. /*
  662. NOTE(Jeroen): We no longer need or use `_private_int_div_small`.
  663. We'll keep it around for a bit until we're reasonably certain div_school is bug free.
  664. */
  665. err = _private_int_div_small(quotient, remainder, numerator, denominator)
  666. }
  667. }
  668. return
  669. }
  670. /*
  671. Single digit division (based on routine from MPI).
  672. The quotient is optional and may be passed a nil.
  673. */
  674. internal_int_divmod_digit :: proc(quotient, numerator: ^Int, denominator: DIGIT, allocator := context.allocator) -> (remainder: DIGIT, err: Error) {
  675. context.allocator = allocator
  676. /*
  677. Cannot divide by zero.
  678. */
  679. if denominator == 0 { return 0, .Division_by_Zero }
  680. /*
  681. Quick outs.
  682. */
  683. if denominator == 1 || numerator.used == 0 {
  684. if quotient != nil {
  685. return 0, internal_copy(quotient, numerator)
  686. }
  687. return 0, err
  688. }
  689. /*
  690. Power of two?
  691. */
  692. if denominator == 2 {
  693. if numerator.used > 0 && numerator.digit[0] & 1 != 0 {
  694. // Remainder is 1 if numerator is odd.
  695. remainder = 1
  696. }
  697. if quotient == nil {
  698. return remainder, nil
  699. }
  700. return remainder, internal_shr(quotient, numerator, 1)
  701. }
  702. ix: int
  703. if platform_int_is_power_of_two(int(denominator)) {
  704. ix = 1
  705. for ix < _DIGIT_BITS && denominator != (1 << uint(ix)) {
  706. ix += 1
  707. }
  708. remainder = numerator.digit[0] & ((1 << uint(ix)) - 1)
  709. if quotient == nil {
  710. return remainder, nil
  711. }
  712. return remainder, internal_shr(quotient, numerator, int(ix))
  713. }
  714. /*
  715. Three?
  716. */
  717. if denominator == 3 {
  718. return _private_int_div_3(quotient, numerator)
  719. }
  720. /*
  721. No easy answer [c'est la vie]. Just division.
  722. */
  723. q := &Int{}
  724. internal_grow(q, numerator.used) or_return
  725. q.used = numerator.used
  726. q.sign = numerator.sign
  727. w := _WORD(0)
  728. for ix = numerator.used - 1; ix >= 0; ix -= 1 {
  729. t := DIGIT(0)
  730. w = (w << _WORD(_DIGIT_BITS) | _WORD(numerator.digit[ix]))
  731. if w >= _WORD(denominator) {
  732. t = DIGIT(w / _WORD(denominator))
  733. w -= _WORD(t) * _WORD(denominator)
  734. }
  735. q.digit[ix] = t
  736. }
  737. remainder = DIGIT(w)
  738. if quotient != nil {
  739. internal_clamp(q)
  740. internal_swap(q, quotient)
  741. }
  742. internal_destroy(q)
  743. return remainder, nil
  744. }
  745. internal_divmod :: proc { internal_int_divmod, internal_int_divmod_digit, }
  746. /*
  747. Asssumes quotient, numerator and denominator to have been initialized and not to be nil.
  748. */
  749. internal_int_div :: proc(quotient, numerator, denominator: ^Int, allocator := context.allocator) -> (err: Error) {
  750. return #force_inline internal_int_divmod(quotient, nil, numerator, denominator, allocator)
  751. }
  752. internal_div :: proc { internal_int_div, }
  753. /*
  754. remainder = numerator % denominator.
  755. 0 <= remainder < denominator if denominator > 0
  756. denominator < remainder <= 0 if denominator < 0
  757. Asssumes quotient, numerator and denominator to have been initialized and not to be nil.
  758. */
  759. internal_int_mod :: proc(remainder, numerator, denominator: ^Int, allocator := context.allocator) -> (err: Error) {
  760. #force_inline internal_int_divmod(nil, remainder, numerator, denominator, allocator) or_return
  761. if remainder.used == 0 || denominator.sign == remainder.sign { return nil }
  762. return #force_inline internal_add(remainder, remainder, denominator, allocator)
  763. }
  764. internal_int_mod_digit :: proc(numerator: ^Int, denominator: DIGIT, allocator := context.allocator) -> (remainder: DIGIT, err: Error) {
  765. return internal_int_divmod_digit(nil, numerator, denominator, allocator)
  766. }
  767. internal_mod :: proc{ internal_int_mod, internal_int_mod_digit, }
  768. /*
  769. remainder = (number + addend) % modulus.
  770. */
  771. internal_int_addmod :: proc(remainder, number, addend, modulus: ^Int, allocator := context.allocator) -> (err: Error) {
  772. #force_inline internal_add(remainder, number, addend, allocator) or_return
  773. return #force_inline internal_mod(remainder, remainder, modulus, allocator)
  774. }
  775. internal_addmod :: proc { internal_int_addmod, }
  776. /*
  777. remainder = (number - decrease) % modulus.
  778. */
  779. internal_int_submod :: proc(remainder, number, decrease, modulus: ^Int, allocator := context.allocator) -> (err: Error) {
  780. #force_inline internal_sub(remainder, number, decrease, allocator) or_return
  781. return #force_inline internal_mod(remainder, remainder, modulus, allocator)
  782. }
  783. internal_submod :: proc { internal_int_submod, }
  784. /*
  785. remainder = (number * multiplicand) % modulus.
  786. */
  787. internal_int_mulmod :: proc(remainder, number, multiplicand, modulus: ^Int, allocator := context.allocator) -> (err: Error) {
  788. #force_inline internal_mul(remainder, number, multiplicand, allocator) or_return
  789. return #force_inline internal_mod(remainder, remainder, modulus, allocator)
  790. }
  791. internal_mulmod :: proc { internal_int_mulmod, }
  792. /*
  793. remainder = (number * number) % modulus.
  794. */
  795. internal_int_sqrmod :: proc(remainder, number, modulus: ^Int, allocator := context.allocator) -> (err: Error) {
  796. #force_inline internal_sqr(remainder, number, allocator) or_return
  797. return #force_inline internal_mod(remainder, remainder, modulus, allocator)
  798. }
  799. internal_sqrmod :: proc { internal_int_sqrmod, }
  800. /*
  801. TODO: Use Sterling's Approximation to estimate log2(N!) to size the result.
  802. This way we'll have to reallocate less, possibly not at all.
  803. */
  804. internal_int_factorial :: proc(res: ^Int, n: int, allocator := context.allocator) -> (err: Error) {
  805. context.allocator = allocator
  806. if n >= FACTORIAL_BINARY_SPLIT_CUTOFF {
  807. return _private_int_factorial_binary_split(res, n)
  808. }
  809. i := len(_factorial_table)
  810. if n < i {
  811. return #force_inline internal_set(res, _factorial_table[n])
  812. }
  813. #force_inline internal_set(res, _factorial_table[i - 1]) or_return
  814. for {
  815. if err = #force_inline internal_mul(res, res, DIGIT(i)); err != nil || i == n {
  816. return err
  817. }
  818. i += 1
  819. }
  820. return nil
  821. }
  822. /*
  823. Returns GCD, LCM or both.
  824. Assumes `a` and `b` to have been initialized.
  825. `res_gcd` and `res_lcm` can be nil or ^Int depending on which results are desired.
  826. */
  827. internal_int_gcd_lcm :: proc(res_gcd, res_lcm, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  828. if res_gcd == nil && res_lcm == nil { return nil }
  829. return #force_inline _private_int_gcd_lcm(res_gcd, res_lcm, a, b, allocator)
  830. }
  831. internal_int_gcd :: proc(res_gcd, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  832. return #force_inline _private_int_gcd_lcm(res_gcd, nil, a, b, allocator)
  833. }
  834. internal_int_lcm :: proc(res_lcm, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  835. return #force_inline _private_int_gcd_lcm(nil, res_lcm, a, b, allocator)
  836. }
  837. /*
  838. remainder = numerator % (1 << bits)
  839. Assumes `remainder` and `numerator` both not to be `nil` and `bits` to be >= 0.
  840. */
  841. internal_int_mod_bits :: proc(remainder, numerator: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
  842. /*
  843. Everything is divisible by 1 << 0 == 1, so this returns 0.
  844. */
  845. if bits == 0 { return internal_zero(remainder) }
  846. /*
  847. If the modulus is larger than the value, return the value.
  848. */
  849. internal_copy(remainder, numerator) or_return
  850. if bits >= (numerator.used * _DIGIT_BITS) {
  851. return
  852. }
  853. /*
  854. Zero digits above the last digit of the modulus.
  855. */
  856. zero_count := (bits / _DIGIT_BITS)
  857. zero_count += 0 if (bits % _DIGIT_BITS == 0) else 1
  858. /*
  859. Zero remainder. Special case, can't use `internal_zero_unused`.
  860. */
  861. if zero_count > 0 {
  862. mem.zero_slice(remainder.digit[zero_count:])
  863. }
  864. /*
  865. Clear the digit that is not completely outside/inside the modulus.
  866. */
  867. remainder.digit[bits / _DIGIT_BITS] &= DIGIT(1 << DIGIT(bits % _DIGIT_BITS)) - DIGIT(1)
  868. return internal_clamp(remainder)
  869. }
  870. /*
  871. ============================= Low-level helpers =============================
  872. `internal_*` helpers don't return an `Error` like their public counterparts do,
  873. because they expect not to be passed `nil` or uninitialized inputs.
  874. This makes them more suitable for `internal_*` functions and some of the
  875. public ones that have already satisfied these constraints.
  876. */
  877. /*
  878. This procedure returns the allocated capacity of an Int.
  879. Assumes `a` not to be `nil`.
  880. */
  881. internal_int_allocated_cap :: #force_inline proc(a: ^Int) -> (cap: int) {
  882. raw := transmute(mem.Raw_Dynamic_Array)a.digit
  883. return raw.cap
  884. }
  885. /*
  886. This procedure will return `true` if the `Int` is initialized, `false` if not.
  887. Assumes `a` not to be `nil`.
  888. */
  889. internal_int_is_initialized :: #force_inline proc(a: ^Int) -> (initialized: bool) {
  890. return internal_int_allocated_cap(a) >= _MIN_DIGIT_COUNT
  891. }
  892. internal_is_initialized :: proc { internal_int_is_initialized, }
  893. /*
  894. This procedure will return `true` if the `Int` is zero, `false` if not.
  895. Assumes `a` not to be `nil`.
  896. */
  897. internal_int_is_zero :: #force_inline proc(a: ^Int) -> (zero: bool) {
  898. return a.used == 0
  899. }
  900. internal_is_zero :: proc {
  901. internal_rat_is_zero,
  902. internal_int_is_zero,
  903. }
  904. /*
  905. This procedure will return `true` if the `Int` is positive, `false` if not.
  906. Assumes `a` not to be `nil`.
  907. */
  908. internal_int_is_positive :: #force_inline proc(a: ^Int) -> (positive: bool) {
  909. return a.sign == .Zero_or_Positive
  910. }
  911. internal_is_positive :: proc { internal_int_is_positive, }
  912. /*
  913. This procedure will return `true` if the `Int` is negative, `false` if not.
  914. Assumes `a` not to be `nil`.
  915. */
  916. internal_int_is_negative :: #force_inline proc(a: ^Int) -> (negative: bool) {
  917. return a.sign == .Negative
  918. }
  919. internal_is_negative :: proc { internal_int_is_negative, }
  920. /*
  921. This procedure will return `true` if the `Int` is even, `false` if not.
  922. Assumes `a` not to be `nil`.
  923. */
  924. internal_int_is_even :: #force_inline proc(a: ^Int) -> (even: bool) {
  925. if internal_is_zero(a) { return true }
  926. /*
  927. `a.used` > 0 here, because the above handled `is_zero`.
  928. We don't need to explicitly test it.
  929. */
  930. return a.digit[0] & 1 == 0
  931. }
  932. internal_is_even :: proc { internal_int_is_even, }
  933. /*
  934. This procedure will return `true` if the `Int` is even, `false` if not.
  935. Assumes `a` not to be `nil`.
  936. */
  937. internal_int_is_odd :: #force_inline proc(a: ^Int) -> (odd: bool) {
  938. return !internal_int_is_even(a)
  939. }
  940. internal_is_odd :: proc { internal_int_is_odd, }
  941. /*
  942. This procedure will return `true` if the `Int` is a power of two, `false` if not.
  943. Assumes `a` not to be `nil`.
  944. */
  945. internal_int_is_power_of_two :: #force_inline proc(a: ^Int) -> (power_of_two: bool) {
  946. /*
  947. Early out for Int == 0.
  948. */
  949. if #force_inline internal_is_zero(a) { return true }
  950. /*
  951. For an `Int` to be a power of two, its bottom limb has to be a power of two.
  952. */
  953. if ! #force_inline platform_int_is_power_of_two(int(a.digit[a.used - 1])) { return false }
  954. /*
  955. We've established that the bottom limb is a power of two.
  956. If it's the only limb, that makes the entire Int a power of two.
  957. */
  958. if a.used == 1 { return true }
  959. /*
  960. For an `Int` to be a power of two, all limbs except the top one have to be zero.
  961. */
  962. for i := 1; i < a.used && a.digit[i - 1] != 0; i += 1 { return false }
  963. return true
  964. }
  965. internal_is_power_of_two :: proc { internal_int_is_power_of_two, }
  966. /*
  967. Compare two `Int`s, signed.
  968. Returns -1 if `a` < `b`, 0 if `a` == `b` and 1 if `b` > `a`.
  969. Expects `a` and `b` both to be valid `Int`s, i.e. initialized and not `nil`.
  970. */
  971. internal_int_compare :: #force_inline proc(a, b: ^Int) -> (comparison: int) {
  972. assert_if_nil(a, b)
  973. a_is_negative := #force_inline internal_is_negative(a)
  974. /*
  975. Compare based on sign.
  976. */
  977. if a.sign != b.sign { return -1 if a_is_negative else +1 }
  978. /*
  979. If `a` is negative, compare in the opposite direction */
  980. if a_is_negative { return #force_inline internal_compare_magnitude(b, a) }
  981. return #force_inline internal_compare_magnitude(a, b)
  982. }
  983. internal_compare :: proc { internal_int_compare, internal_int_compare_digit, }
  984. internal_cmp :: internal_compare
  985. /*
  986. Compare an `Int` to an unsigned number upto `DIGIT & _MASK`.
  987. Returns -1 if `a` < `b`, 0 if `a` == `b` and 1 if `b` > `a`.
  988. Expects: `a` and `b` both to be valid `Int`s, i.e. initialized and not `nil`.
  989. */
  990. internal_int_compare_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (comparison: int) {
  991. assert_if_nil(a)
  992. a_is_negative := #force_inline internal_is_negative(a)
  993. switch {
  994. /*
  995. Compare based on sign first.
  996. */
  997. case a_is_negative: return -1
  998. /*
  999. Then compare on magnitude.
  1000. */
  1001. case a.used > 1: return +1
  1002. /*
  1003. We have only one digit. Compare it against `b`.
  1004. */
  1005. case a.digit[0] < b: return -1
  1006. case a.digit[0] == b: return 0
  1007. case a.digit[0] > b: return +1
  1008. /*
  1009. Unreachable.
  1010. Just here because Odin complains about a missing return value at the bottom of the proc otherwise.
  1011. */
  1012. case: return
  1013. }
  1014. }
  1015. internal_compare_digit :: proc { internal_int_compare_digit, }
  1016. internal_cmp_digit :: internal_compare_digit
  1017. /*
  1018. Compare the magnitude of two `Int`s, unsigned.
  1019. */
  1020. internal_int_compare_magnitude :: #force_inline proc(a, b: ^Int) -> (comparison: int) {
  1021. assert_if_nil(a, b)
  1022. /*
  1023. Compare based on used digits.
  1024. */
  1025. if a.used != b.used {
  1026. if a.used > b.used {
  1027. return +1
  1028. }
  1029. return -1
  1030. }
  1031. /*
  1032. Same number of used digits, compare based on their value.
  1033. */
  1034. #no_bounds_check for n := a.used - 1; n >= 0; n -= 1 {
  1035. if a.digit[n] != b.digit[n] {
  1036. if a.digit[n] > b.digit[n] {
  1037. return +1
  1038. }
  1039. return -1
  1040. }
  1041. }
  1042. return 0
  1043. }
  1044. internal_compare_magnitude :: proc { internal_int_compare_magnitude, }
  1045. internal_cmp_mag :: internal_compare_magnitude
  1046. /*
  1047. bool := a < b
  1048. */
  1049. internal_int_less_than :: #force_inline proc(a, b: ^Int) -> (less_than: bool) {
  1050. return internal_cmp(a, b) == -1
  1051. }
  1052. /*
  1053. bool := a < b
  1054. */
  1055. internal_int_less_than_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (less_than: bool) {
  1056. return internal_cmp_digit(a, b) == -1
  1057. }
  1058. /*
  1059. bool := |a| < |b|
  1060. Compares the magnitudes only, ignores the sign.
  1061. */
  1062. internal_int_less_than_abs :: #force_inline proc(a, b: ^Int) -> (less_than: bool) {
  1063. return internal_cmp_mag(a, b) == -1
  1064. }
  1065. internal_less_than :: proc {
  1066. internal_int_less_than,
  1067. internal_int_less_than_digit,
  1068. }
  1069. internal_lt :: internal_less_than
  1070. internal_less_than_abs :: proc {
  1071. internal_int_less_than_abs,
  1072. }
  1073. internal_lt_abs :: internal_less_than_abs
  1074. /*
  1075. bool := a <= b
  1076. */
  1077. internal_int_less_than_or_equal :: #force_inline proc(a, b: ^Int) -> (less_than_or_equal: bool) {
  1078. return internal_cmp(a, b) <= 0
  1079. }
  1080. /*
  1081. bool := a <= b
  1082. */
  1083. internal_int_less_than_or_equal_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (less_than_or_equal: bool) {
  1084. return internal_cmp_digit(a, b) <= 0
  1085. }
  1086. /*
  1087. bool := |a| <= |b|
  1088. Compares the magnitudes only, ignores the sign.
  1089. */
  1090. internal_int_less_than_or_equal_abs :: #force_inline proc(a, b: ^Int) -> (less_than_or_equal: bool) {
  1091. return internal_cmp_mag(a, b) <= 0
  1092. }
  1093. internal_less_than_or_equal :: proc {
  1094. internal_int_less_than_or_equal,
  1095. internal_int_less_than_or_equal_digit,
  1096. }
  1097. internal_lte :: internal_less_than_or_equal
  1098. internal_less_than_or_equal_abs :: proc {
  1099. internal_int_less_than_or_equal_abs,
  1100. }
  1101. internal_lte_abs :: internal_less_than_or_equal_abs
  1102. /*
  1103. bool := a == b
  1104. */
  1105. internal_int_equals :: #force_inline proc(a, b: ^Int) -> (equals: bool) {
  1106. return internal_cmp(a, b) == 0
  1107. }
  1108. /*
  1109. bool := a == b
  1110. */
  1111. internal_int_equals_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (equals: bool) {
  1112. return internal_cmp_digit(a, b) == 0
  1113. }
  1114. /*
  1115. bool := |a| == |b|
  1116. Compares the magnitudes only, ignores the sign.
  1117. */
  1118. internal_int_equals_abs :: #force_inline proc(a, b: ^Int) -> (equals: bool) {
  1119. return internal_cmp_mag(a, b) == 0
  1120. }
  1121. internal_equals :: proc {
  1122. internal_int_equals,
  1123. internal_int_equals_digit,
  1124. }
  1125. internal_eq :: internal_equals
  1126. internal_equals_abs :: proc {
  1127. internal_int_equals_abs,
  1128. }
  1129. internal_eq_abs :: internal_equals_abs
  1130. /*
  1131. bool := a >= b
  1132. */
  1133. internal_int_greater_than_or_equal :: #force_inline proc(a, b: ^Int) -> (greater_than_or_equal: bool) {
  1134. return internal_cmp(a, b) >= 0
  1135. }
  1136. /*
  1137. bool := a >= b
  1138. */
  1139. internal_int_greater_than_or_equal_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (greater_than_or_equal: bool) {
  1140. return internal_cmp_digit(a, b) >= 0
  1141. }
  1142. /*
  1143. bool := |a| >= |b|
  1144. Compares the magnitudes only, ignores the sign.
  1145. */
  1146. internal_int_greater_than_or_equal_abs :: #force_inline proc(a, b: ^Int) -> (greater_than_or_equal: bool) {
  1147. return internal_cmp_mag(a, b) >= 0
  1148. }
  1149. internal_greater_than_or_equal :: proc {
  1150. internal_int_greater_than_or_equal,
  1151. internal_int_greater_than_or_equal_digit,
  1152. }
  1153. internal_gte :: internal_greater_than_or_equal
  1154. internal_greater_than_or_equal_abs :: proc {
  1155. internal_int_greater_than_or_equal_abs,
  1156. }
  1157. internal_gte_abs :: internal_greater_than_or_equal_abs
  1158. /*
  1159. bool := a > b
  1160. */
  1161. internal_int_greater_than :: #force_inline proc(a, b: ^Int) -> (greater_than: bool) {
  1162. return internal_cmp(a, b) == 1
  1163. }
  1164. /*
  1165. bool := a > b
  1166. */
  1167. internal_int_greater_than_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (greater_than: bool) {
  1168. return internal_cmp_digit(a, b) == 1
  1169. }
  1170. /*
  1171. bool := |a| > |b|
  1172. Compares the magnitudes only, ignores the sign.
  1173. */
  1174. internal_int_greater_than_abs :: #force_inline proc(a, b: ^Int) -> (greater_than: bool) {
  1175. return internal_cmp_mag(a, b) == 1
  1176. }
  1177. internal_greater_than :: proc {
  1178. internal_int_greater_than,
  1179. internal_int_greater_than_digit,
  1180. }
  1181. internal_gt :: internal_greater_than
  1182. internal_greater_than_abs :: proc {
  1183. internal_int_greater_than_abs,
  1184. }
  1185. internal_gt_abs :: internal_greater_than_abs
  1186. /*
  1187. Check if remainders are possible squares - fast exclude non-squares.
  1188. Returns `true` if `a` is a square, `false` if not.
  1189. Assumes `a` not to be `nil` and to have been initialized.
  1190. */
  1191. internal_int_is_square :: proc(a: ^Int, allocator := context.allocator) -> (square: bool, err: Error) {
  1192. context.allocator = allocator
  1193. /*
  1194. Default to Non-square :)
  1195. */
  1196. square = false
  1197. if internal_is_negative(a) { return }
  1198. if internal_is_zero(a) { return }
  1199. /*
  1200. First check mod 128 (suppose that _DIGIT_BITS is at least 7).
  1201. */
  1202. if _private_int_rem_128[127 & a.digit[0]] == 1 { return }
  1203. /*
  1204. Next check mod 105 (3*5*7).
  1205. */
  1206. c: DIGIT
  1207. c, err = internal_mod(a, 105)
  1208. if _private_int_rem_105[c] == 1 { return }
  1209. t := &Int{}
  1210. defer destroy(t)
  1211. set(t, 11 * 13 * 17 * 19 * 23 * 29 * 31) or_return
  1212. internal_mod(t, a, t) or_return
  1213. r: u64
  1214. r, err = internal_int_get(t, u64)
  1215. /*
  1216. Check for other prime modules, note it's not an ERROR but we must
  1217. free "t" so the easiest way is to goto LBL_ERR. We know that err
  1218. is already equal to MP_OKAY from the mp_mod call
  1219. */
  1220. if (1 << (r % 11) & 0x5C4) != 0 { return }
  1221. if (1 << (r % 13) & 0x9E4) != 0 { return }
  1222. if (1 << (r % 17) & 0x5CE8) != 0 { return }
  1223. if (1 << (r % 19) & 0x4F50C) != 0 { return }
  1224. if (1 << (r % 23) & 0x7ACCA0) != 0 { return }
  1225. if (1 << (r % 29) & 0xC2EDD0C) != 0 { return }
  1226. if (1 << (r % 31) & 0x6DE2B848) != 0 { return }
  1227. /*
  1228. Final check - is sqr(sqrt(arg)) == arg?
  1229. */
  1230. sqrt(t, a) or_return
  1231. sqr(t, t) or_return
  1232. square = internal_eq_abs(t, a)
  1233. return
  1234. }
  1235. /*
  1236. ========================= Logs, powers and roots ============================
  1237. */
  1238. /*
  1239. Returns log_base(a).
  1240. Assumes `a` to not be `nil` and have been iniialized.
  1241. */
  1242. internal_int_log :: proc(a: ^Int, base: DIGIT) -> (res: int, err: Error) {
  1243. if base < 2 || DIGIT(base) > _DIGIT_MAX { return -1, .Invalid_Argument }
  1244. if internal_is_negative(a) { return -1, .Math_Domain_Error }
  1245. if internal_is_zero(a) { return -1, .Math_Domain_Error }
  1246. /*
  1247. Fast path for bases that are a power of two.
  1248. */
  1249. if platform_int_is_power_of_two(int(base)) { return _private_log_power_of_two(a, base) }
  1250. /*
  1251. Fast path for `Int`s that fit within a single `DIGIT`.
  1252. */
  1253. if a.used == 1 { return internal_log(a.digit[0], DIGIT(base)) }
  1254. return _private_int_log(a, base)
  1255. }
  1256. /*
  1257. Returns log_base(a), where `a` is a DIGIT.
  1258. */
  1259. internal_digit_log :: proc(a: DIGIT, base: DIGIT) -> (log: int, err: Error) {
  1260. /*
  1261. If the number is smaller than the base, it fits within a fraction.
  1262. Therefore, we return 0.
  1263. */
  1264. if a < base { return 0, nil }
  1265. /*
  1266. If a number equals the base, the log is 1.
  1267. */
  1268. if a == base { return 1, nil }
  1269. N := _WORD(a)
  1270. bracket_low := _WORD(1)
  1271. bracket_high := _WORD(base)
  1272. high := 1
  1273. low := 0
  1274. for bracket_high < N {
  1275. low = high
  1276. bracket_low = bracket_high
  1277. high <<= 1
  1278. bracket_high *= bracket_high
  1279. }
  1280. for high - low > 1 {
  1281. mid := (low + high) >> 1
  1282. bracket_mid := bracket_low * #force_inline internal_small_pow(_WORD(base), _WORD(mid - low))
  1283. if N < bracket_mid {
  1284. high = mid
  1285. bracket_high = bracket_mid
  1286. }
  1287. if N > bracket_mid {
  1288. low = mid
  1289. bracket_low = bracket_mid
  1290. }
  1291. if N == bracket_mid {
  1292. return mid, nil
  1293. }
  1294. }
  1295. if bracket_high == N {
  1296. return high, nil
  1297. } else {
  1298. return low, nil
  1299. }
  1300. }
  1301. internal_log :: proc { internal_int_log, internal_digit_log, }
  1302. /*
  1303. Calculate dest = base^power using a square-multiply algorithm.
  1304. Assumes `dest` and `base` not to be `nil` and to have been initialized.
  1305. */
  1306. internal_int_pow :: proc(dest, base: ^Int, power: int, allocator := context.allocator) -> (err: Error) {
  1307. context.allocator = allocator
  1308. power := power
  1309. /*
  1310. Early outs.
  1311. */
  1312. if #force_inline internal_is_zero(base) {
  1313. /*
  1314. A zero base is a special case.
  1315. */
  1316. if power < 0 {
  1317. internal_zero(dest) or_return
  1318. return .Math_Domain_Error
  1319. }
  1320. if power == 0 { return internal_one(dest) }
  1321. if power > 0 { return internal_zero(dest) }
  1322. }
  1323. if power < 0 {
  1324. /*
  1325. Fraction, so we'll return zero.
  1326. */
  1327. return internal_zero(dest)
  1328. }
  1329. switch(power) {
  1330. case 0:
  1331. /*
  1332. Any base to the power zero is one.
  1333. */
  1334. return #force_inline internal_one(dest)
  1335. case 1:
  1336. /*
  1337. Any base to the power one is itself.
  1338. */
  1339. return copy(dest, base)
  1340. case 2:
  1341. return #force_inline internal_sqr(dest, base)
  1342. }
  1343. g := &Int{}
  1344. internal_copy(g, base) or_return
  1345. /*
  1346. Set initial result.
  1347. */
  1348. internal_one(dest) or_return
  1349. defer internal_destroy(g)
  1350. for power > 0 {
  1351. /*
  1352. If the bit is set, multiply.
  1353. */
  1354. if power & 1 != 0 {
  1355. internal_mul(dest, g, dest) or_return
  1356. }
  1357. /*
  1358. Square.
  1359. */
  1360. if power > 1 {
  1361. internal_sqr(g, g) or_return
  1362. }
  1363. /* shift to next bit */
  1364. power >>= 1
  1365. }
  1366. return
  1367. }
  1368. /*
  1369. Calculate `dest = base^power`.
  1370. Assumes `dest` not to be `nil` and to have been initialized.
  1371. */
  1372. internal_int_pow_int :: proc(dest: ^Int, base, power: int, allocator := context.allocator) -> (err: Error) {
  1373. context.allocator = allocator
  1374. base_t := &Int{}
  1375. defer internal_destroy(base_t)
  1376. internal_set(base_t, base) or_return
  1377. return #force_inline internal_int_pow(dest, base_t, power)
  1378. }
  1379. internal_pow :: proc { internal_int_pow, internal_int_pow_int, }
  1380. internal_exp :: pow
  1381. /*
  1382. */
  1383. internal_small_pow :: proc(base: _WORD, exponent: _WORD) -> (result: _WORD) {
  1384. exponent := exponent; base := base
  1385. result = _WORD(1)
  1386. for exponent != 0 {
  1387. if exponent & 1 == 1 {
  1388. result *= base
  1389. }
  1390. exponent >>= 1
  1391. base *= base
  1392. }
  1393. return result
  1394. }
  1395. /*
  1396. This function is less generic than `root_n`, simpler and faster.
  1397. Assumes `dest` and `src` not to be `nil` and to have been initialized.
  1398. */
  1399. internal_int_sqrt :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error) {
  1400. context.allocator = allocator
  1401. /*
  1402. Must be positive.
  1403. */
  1404. if #force_inline internal_is_negative(src) { return .Invalid_Argument }
  1405. /*
  1406. Easy out. If src is zero, so is dest.
  1407. */
  1408. if #force_inline internal_is_zero(src) { return internal_zero(dest) }
  1409. /*
  1410. Set up temporaries.
  1411. */
  1412. x, y, t1, t2 := &Int{}, &Int{}, &Int{}, &Int{}
  1413. defer internal_destroy(x, y, t1, t2)
  1414. count := #force_inline internal_count_bits(src)
  1415. a, b := count >> 1, count & 1
  1416. internal_int_power_of_two(x, a+b, allocator) or_return
  1417. for {
  1418. /*
  1419. y = (x + n // x) // 2
  1420. */
  1421. internal_div(t1, src, x) or_return
  1422. internal_add(t2, t1, x) or_return
  1423. internal_shr(y, t2, 1) or_return
  1424. if internal_gte(y, x) {
  1425. internal_swap(dest, x)
  1426. return nil
  1427. }
  1428. internal_swap(x, y)
  1429. }
  1430. internal_swap(dest, x)
  1431. return err
  1432. }
  1433. internal_sqrt :: proc { internal_int_sqrt, }
  1434. /*
  1435. Find the nth root of an Integer.
  1436. Result found such that `(dest)**n <= src` and `(dest+1)**n > src`
  1437. This algorithm uses Newton's approximation `x[i+1] = x[i] - f(x[i])/f'(x[i])`,
  1438. which will find the root in `log(n)` time where each step involves a fair bit.
  1439. Assumes `dest` and `src` not to be `nil` and have been initialized.
  1440. */
  1441. internal_int_root_n :: proc(dest, src: ^Int, n: int, allocator := context.allocator) -> (err: Error) {
  1442. context.allocator = allocator
  1443. /*
  1444. Fast path for n == 2
  1445. */
  1446. if n == 2 { return #force_inline internal_sqrt(dest, src) }
  1447. if n < 0 || n > int(_DIGIT_MAX) { return .Invalid_Argument }
  1448. if n & 1 == 0 && #force_inline internal_is_negative(src) { return .Invalid_Argument }
  1449. /*
  1450. Set up temporaries.
  1451. */
  1452. t1, t2, t3, a := &Int{}, &Int{}, &Int{}, &Int{}
  1453. defer internal_destroy(t1, t2, t3)
  1454. /*
  1455. If `src` is negative fudge the sign but keep track.
  1456. */
  1457. a.sign = .Zero_or_Positive
  1458. a.used = src.used
  1459. a.digit = src.digit
  1460. /*
  1461. If "n" is larger than INT_MAX it is also larger than
  1462. log_2(src) because the bit-length of the "src" is measured
  1463. with an int and hence the root is always < 2 (two).
  1464. */
  1465. if n > max(int) / 2 {
  1466. err = set(dest, 1)
  1467. dest.sign = a.sign
  1468. return err
  1469. }
  1470. /*
  1471. Compute seed: 2^(log_2(src)/n + 2)
  1472. */
  1473. ilog2 := internal_count_bits(src)
  1474. /*
  1475. "src" is smaller than max(int), we can cast safely.
  1476. */
  1477. if ilog2 < n {
  1478. err = internal_one(dest)
  1479. dest.sign = a.sign
  1480. return err
  1481. }
  1482. ilog2 /= n
  1483. if ilog2 == 0 {
  1484. err = internal_one(dest)
  1485. dest.sign = a.sign
  1486. return err
  1487. }
  1488. /*
  1489. Start value must be larger than root.
  1490. */
  1491. ilog2 += 2
  1492. internal_int_power_of_two(t2, ilog2) or_return
  1493. c: int
  1494. iterations := 0
  1495. for {
  1496. /* t1 = t2 */
  1497. internal_copy(t1, t2) or_return
  1498. /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
  1499. /* t3 = t1**(b-1) */
  1500. internal_pow(t3, t1, n-1) or_return
  1501. /* numerator */
  1502. /* t2 = t1**b */
  1503. internal_mul(t2, t1, t3) or_return
  1504. /* t2 = t1**b - a */
  1505. internal_sub(t2, t2, a) or_return
  1506. /* denominator */
  1507. /* t3 = t1**(b-1) * b */
  1508. internal_mul(t3, t3, DIGIT(n)) or_return
  1509. /* t3 = (t1**b - a)/(b * t1**(b-1)) */
  1510. internal_div(t3, t2, t3) or_return
  1511. internal_sub(t2, t1, t3) or_return
  1512. /*
  1513. Number of rounds is at most log_2(root). If it is more it
  1514. got stuck, so break out of the loop and do the rest manually.
  1515. */
  1516. if ilog2 -= 1; ilog2 == 0 { break }
  1517. if internal_eq(t1, t2) { break }
  1518. iterations += 1
  1519. if iterations == MAX_ITERATIONS_ROOT_N {
  1520. return .Max_Iterations_Reached
  1521. }
  1522. }
  1523. /* Result can be off by a few so check. */
  1524. /* Loop beneath can overshoot by one if found root is smaller than actual root. */
  1525. iterations = 0
  1526. for {
  1527. internal_pow(t2, t1, n) or_return
  1528. c = internal_cmp(t2, a)
  1529. if c == 0 {
  1530. swap(dest, t1)
  1531. return nil
  1532. } else if c == -1 {
  1533. internal_add(t1, t1, DIGIT(1)) or_return
  1534. } else {
  1535. break
  1536. }
  1537. iterations += 1
  1538. if iterations == MAX_ITERATIONS_ROOT_N {
  1539. return .Max_Iterations_Reached
  1540. }
  1541. }
  1542. iterations = 0
  1543. /*
  1544. Correct overshoot from above or from recurrence.
  1545. */
  1546. for {
  1547. internal_pow(t2, t1, n) or_return
  1548. if internal_lt(t2, a) { break }
  1549. internal_sub(t1, t1, DIGIT(1)) or_return
  1550. iterations += 1
  1551. if iterations == MAX_ITERATIONS_ROOT_N {
  1552. return .Max_Iterations_Reached
  1553. }
  1554. }
  1555. /*
  1556. Set the result.
  1557. */
  1558. internal_swap(dest, t1)
  1559. /*
  1560. Set the sign of the result.
  1561. */
  1562. dest.sign = src.sign
  1563. return err
  1564. }
  1565. internal_root_n :: proc { internal_int_root_n, }
  1566. /*
  1567. Other internal helpers
  1568. */
  1569. /*
  1570. Deallocates the backing memory of one or more `Int`s.
  1571. Asssumes none of the `integers` to be a `nil`.
  1572. */
  1573. internal_int_destroy :: proc(integers: ..^Int) {
  1574. integers := integers
  1575. for a in &integers {
  1576. if internal_int_allocated_cap(a) > 0 {
  1577. mem.zero_slice(a.digit[:])
  1578. free(&a.digit[0])
  1579. }
  1580. a = &Int{}
  1581. }
  1582. }
  1583. internal_destroy :: proc{
  1584. internal_int_destroy,
  1585. internal_rat_destroy,
  1586. }
  1587. /*
  1588. Helpers to set an `Int` to a specific value.
  1589. */
  1590. internal_int_set_from_integer :: proc(dest: ^Int, src: $T, minimize := false, allocator := context.allocator) -> (err: Error)
  1591. where intrinsics.type_is_integer(T) {
  1592. context.allocator = allocator
  1593. src := src
  1594. internal_error_if_immutable(dest) or_return
  1595. /*
  1596. Most internal procs asssume an Int to have already been initialize,
  1597. but as this is one of the procs that initializes, we have to check the following.
  1598. */
  1599. internal_clear_if_uninitialized_single(dest) or_return
  1600. dest.flags = {} // We're not -Inf, Inf, NaN or Immutable.
  1601. dest.used = 0
  1602. dest.sign = .Zero_or_Positive if src >= 0 else .Negative
  1603. src = internal_abs(src)
  1604. #no_bounds_check for src != 0 {
  1605. dest.digit[dest.used] = DIGIT(src) & _MASK
  1606. dest.used += 1
  1607. src >>= _DIGIT_BITS
  1608. }
  1609. internal_zero_unused(dest)
  1610. return nil
  1611. }
  1612. internal_set :: proc { internal_int_set_from_integer, internal_int_copy, int_atoi }
  1613. internal_copy_digits :: #force_inline proc(dest, src: ^Int, digits: int, offset := int(0)) -> (err: Error) {
  1614. #force_inline internal_error_if_immutable(dest) or_return
  1615. /*
  1616. If dest == src, do nothing
  1617. */
  1618. return #force_inline _private_copy_digits(dest, src, digits, offset)
  1619. }
  1620. /*
  1621. Copy one `Int` to another.
  1622. */
  1623. internal_int_copy :: proc(dest, src: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1624. context.allocator = allocator
  1625. /*
  1626. If dest == src, do nothing
  1627. */
  1628. if (dest == src) { return nil }
  1629. internal_error_if_immutable(dest) or_return
  1630. /*
  1631. Grow `dest` to fit `src`.
  1632. If `dest` is not yet initialized, it will be using `allocator`.
  1633. */
  1634. needed := src.used if minimize else max(src.used, _DEFAULT_DIGIT_COUNT)
  1635. internal_grow(dest, needed, minimize) or_return
  1636. /*
  1637. Copy everything over and zero high digits.
  1638. */
  1639. internal_copy_digits(dest, src, src.used)
  1640. dest.used = src.used
  1641. dest.sign = src.sign
  1642. dest.flags = src.flags &~ {.Immutable}
  1643. internal_zero_unused(dest)
  1644. return nil
  1645. }
  1646. internal_copy :: proc { internal_int_copy, }
  1647. /*
  1648. In normal code, you can also write `a, b = b, a`.
  1649. However, that only swaps within the current scope.
  1650. This helper swaps completely.
  1651. */
  1652. internal_int_swap :: #force_inline proc(a, b: ^Int) {
  1653. a.used, b.used = b.used, a.used
  1654. a.sign, b.sign = b.sign, a.sign
  1655. a.digit, b.digit = b.digit, a.digit
  1656. }
  1657. internal_swap :: proc {
  1658. internal_int_swap,
  1659. internal_rat_swap,
  1660. }
  1661. /*
  1662. Set `dest` to |`src`|.
  1663. */
  1664. internal_int_abs :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error) {
  1665. context.allocator = allocator
  1666. /*
  1667. If `dest == src`, just fix `dest`'s sign.
  1668. */
  1669. if (dest == src) {
  1670. dest.sign = .Zero_or_Positive
  1671. return nil
  1672. }
  1673. /*
  1674. Copy `src` to `dest`
  1675. */
  1676. internal_copy(dest, src) or_return
  1677. /*
  1678. Fix sign.
  1679. */
  1680. dest.sign = .Zero_or_Positive
  1681. return nil
  1682. }
  1683. internal_platform_abs :: proc(n: $T) -> T where intrinsics.type_is_integer(T) {
  1684. return n if n >= 0 else -n
  1685. }
  1686. internal_abs :: proc{ internal_int_abs, internal_platform_abs, }
  1687. /*
  1688. Set `dest` to `-src`.
  1689. */
  1690. internal_int_neg :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error) {
  1691. context.allocator = allocator
  1692. /*
  1693. If `dest == src`, just fix `dest`'s sign.
  1694. */
  1695. sign := Sign.Negative
  1696. if #force_inline internal_is_zero(src) || #force_inline internal_is_negative(src) {
  1697. sign = .Zero_or_Positive
  1698. }
  1699. if dest == src {
  1700. dest.sign = sign
  1701. return nil
  1702. }
  1703. /*
  1704. Copy `src` to `dest`
  1705. */
  1706. internal_copy(dest, src) or_return
  1707. /*
  1708. Fix sign.
  1709. */
  1710. dest.sign = sign
  1711. return nil
  1712. }
  1713. internal_neg :: proc { internal_int_neg, }
  1714. /*
  1715. hac 14.61, pp608.
  1716. */
  1717. internal_int_inverse_modulo :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  1718. context.allocator = allocator
  1719. /*
  1720. For all n in N and n > 0, n = 0 mod 1.
  1721. */
  1722. if internal_is_positive(a) && internal_eq(b, 1) { return internal_zero(dest) }
  1723. /*
  1724. `b` cannot be negative and has to be > 1
  1725. */
  1726. if internal_is_negative(b) || internal_gt(b, 1) { return .Invalid_Argument }
  1727. /*
  1728. If the modulus is odd we can use a faster routine instead.
  1729. */
  1730. if internal_is_odd(b) { return _private_inverse_modulo_odd(dest, a, b) }
  1731. return _private_inverse_modulo(dest, a, b)
  1732. }
  1733. internal_invmod :: proc{ internal_int_inverse_modulo, }
  1734. /*
  1735. Helpers to extract values from the `Int`.
  1736. Offset is zero indexed.
  1737. */
  1738. internal_int_bitfield_extract_bool :: proc(a: ^Int, offset: int) -> (val: bool, err: Error) {
  1739. limb := offset / _DIGIT_BITS
  1740. if limb < 0 || limb >= a.used { return false, .Invalid_Argument }
  1741. i := _WORD(1 << _WORD((offset % _DIGIT_BITS)))
  1742. return bool(_WORD(a.digit[limb]) & i), nil
  1743. }
  1744. internal_int_bitfield_extract_single :: proc(a: ^Int, offset: int) -> (bit: _WORD, err: Error) {
  1745. limb := offset / _DIGIT_BITS
  1746. if limb < 0 || limb >= a.used { return 0, .Invalid_Argument }
  1747. i := _WORD(1 << _WORD((offset % _DIGIT_BITS)))
  1748. return 1 if ((_WORD(a.digit[limb]) & i) != 0) else 0, nil
  1749. }
  1750. internal_int_bitfield_extract :: proc(a: ^Int, offset, count: int) -> (res: _WORD, err: Error) #no_bounds_check {
  1751. /*
  1752. Early out for single bit.
  1753. */
  1754. if count == 1 {
  1755. limb := offset / _DIGIT_BITS
  1756. if limb < 0 || limb >= a.used { return 0, .Invalid_Argument }
  1757. i := _WORD(1 << _WORD((offset % _DIGIT_BITS)))
  1758. return 1 if ((_WORD(a.digit[limb]) & i) != 0) else 0, nil
  1759. }
  1760. if count > _WORD_BITS || count < 1 { return 0, .Invalid_Argument }
  1761. /*
  1762. There are 3 possible cases.
  1763. - [offset:][:count] covers 1 DIGIT,
  1764. e.g. offset: 0, count: 60 = bits 0..59
  1765. - [offset:][:count] covers 2 DIGITS,
  1766. e.g. offset: 5, count: 60 = bits 5..59, 0..4
  1767. e.g. offset: 0, count: 120 = bits 0..59, 60..119
  1768. - [offset:][:count] covers 3 DIGITS,
  1769. e.g. offset: 40, count: 100 = bits 40..59, 0..59, 0..19
  1770. e.g. offset: 40, count: 120 = bits 40..59, 0..59, 0..39
  1771. */
  1772. limb := offset / _DIGIT_BITS
  1773. bits_left := count
  1774. bits_offset := offset % _DIGIT_BITS
  1775. num_bits := min(bits_left, _DIGIT_BITS - bits_offset)
  1776. shift := offset % _DIGIT_BITS
  1777. mask := (_WORD(1) << uint(num_bits)) - 1
  1778. res = (_WORD(a.digit[limb]) >> uint(shift)) & mask
  1779. bits_left -= num_bits
  1780. if bits_left == 0 { return res, nil }
  1781. res_shift := num_bits
  1782. num_bits = min(bits_left, _DIGIT_BITS)
  1783. mask = (1 << uint(num_bits)) - 1
  1784. res |= (_WORD(a.digit[limb + 1]) & mask) << uint(res_shift)
  1785. bits_left -= num_bits
  1786. if bits_left == 0 { return res, nil }
  1787. mask = (1 << uint(bits_left)) - 1
  1788. res_shift += _DIGIT_BITS
  1789. res |= (_WORD(a.digit[limb + 2]) & mask) << uint(res_shift)
  1790. return res, nil
  1791. }
  1792. /*
  1793. Helpers to (un)set a bit in an Int.
  1794. Offset is zero indexed.
  1795. */
  1796. internal_int_bitfield_set_single :: proc(a: ^Int, offset: int) -> (err: Error) {
  1797. limb := offset / _DIGIT_BITS
  1798. if limb < 0 || limb >= a.used { return .Invalid_Argument }
  1799. i := DIGIT(1 << uint((offset % _DIGIT_BITS)))
  1800. a.digit[limb] |= i
  1801. return
  1802. }
  1803. internal_int_bitfield_unset_single :: proc(a: ^Int, offset: int) -> (err: Error) {
  1804. limb := offset / _DIGIT_BITS
  1805. if limb < 0 || limb >= a.used { return .Invalid_Argument }
  1806. i := DIGIT(1 << uint((offset % _DIGIT_BITS)))
  1807. a.digit[limb] &= _MASK - i
  1808. return
  1809. }
  1810. internal_int_bitfield_toggle_single :: proc(a: ^Int, offset: int) -> (err: Error) {
  1811. limb := offset / _DIGIT_BITS
  1812. if limb < 0 || limb >= a.used { return .Invalid_Argument }
  1813. i := DIGIT(1 << uint((offset % _DIGIT_BITS)))
  1814. a.digit[limb] ~= i
  1815. return
  1816. }
  1817. /*
  1818. Resize backing store.
  1819. We don't need to pass the allocator, because the storage itself stores it.
  1820. Assumes `a` not to be `nil`, and to have already been initialized.
  1821. */
  1822. internal_int_shrink :: proc(a: ^Int) -> (err: Error) {
  1823. needed := max(_MIN_DIGIT_COUNT, a.used)
  1824. if a.used != needed { return internal_grow(a, needed, true) }
  1825. return nil
  1826. }
  1827. internal_shrink :: proc { internal_int_shrink, }
  1828. internal_int_grow :: proc(a: ^Int, digits: int, allow_shrink := false, allocator := context.allocator) -> (err: Error) {
  1829. /*
  1830. We need at least _MIN_DIGIT_COUNT or a.used digits, whichever is bigger.
  1831. The caller is asking for `digits`. Let's be accomodating.
  1832. */
  1833. cap := internal_int_allocated_cap(a)
  1834. needed := max(_MIN_DIGIT_COUNT, a.used, digits)
  1835. if !allow_shrink {
  1836. needed = max(needed, cap)
  1837. }
  1838. /*
  1839. If not yet iniialized, initialize the `digit` backing with the allocator we were passed.
  1840. */
  1841. if cap == 0 {
  1842. a.digit = make([dynamic]DIGIT, needed, allocator)
  1843. } else if cap != needed {
  1844. /*
  1845. `[dynamic]DIGIT` already knows what allocator was used for it, so resize will do the right thing.
  1846. */
  1847. resize(&a.digit, needed)
  1848. }
  1849. /*
  1850. Let's see if the allocation/resize worked as expected.
  1851. */
  1852. if len(a.digit) != needed {
  1853. return .Out_Of_Memory
  1854. }
  1855. return nil
  1856. }
  1857. internal_grow :: proc { internal_int_grow, }
  1858. /*
  1859. Clear `Int` and resize it to the default size.
  1860. Assumes `a` not to be `nil`.
  1861. */
  1862. internal_int_clear :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1863. raw := transmute(mem.Raw_Dynamic_Array)a.digit
  1864. if raw.cap != 0 {
  1865. mem.zero_slice(a.digit[:a.used])
  1866. }
  1867. a.sign = .Zero_or_Positive
  1868. a.used = 0
  1869. return #force_inline internal_grow(a, a.used, minimize, allocator)
  1870. }
  1871. internal_clear :: proc { internal_int_clear, }
  1872. internal_zero :: internal_clear
  1873. /*
  1874. Set the `Int` to 1 and optionally shrink it to the minimum backing size.
  1875. */
  1876. internal_int_one :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1877. return internal_copy(a, INT_ONE, minimize, allocator)
  1878. }
  1879. internal_one :: proc { internal_int_one, }
  1880. /*
  1881. Set the `Int` to -1 and optionally shrink it to the minimum backing size.
  1882. */
  1883. internal_int_minus_one :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1884. return internal_copy(a, INT_MINUS_ONE, minimize, allocator)
  1885. }
  1886. internal_minus_one :: proc { internal_int_minus_one, }
  1887. /*
  1888. Set the `Int` to Inf and optionally shrink it to the minimum backing size.
  1889. */
  1890. internal_int_inf :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1891. return internal_copy(a, INT_INF, minimize, allocator)
  1892. }
  1893. internal_inf :: proc { internal_int_inf, }
  1894. /*
  1895. Set the `Int` to -Inf and optionally shrink it to the minimum backing size.
  1896. */
  1897. internal_int_minus_inf :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1898. return internal_copy(a, INT_MINUS_INF, minimize, allocator)
  1899. }
  1900. internal_minus_inf :: proc { internal_int_inf, }
  1901. /*
  1902. Set the `Int` to NaN and optionally shrink it to the minimum backing size.
  1903. */
  1904. internal_int_nan :: proc(a: ^Int, minimize := false, allocator := context.allocator) -> (err: Error) {
  1905. return internal_copy(a, INT_NAN, minimize, allocator)
  1906. }
  1907. internal_nan :: proc { internal_int_nan, }
  1908. internal_int_power_of_two :: proc(a: ^Int, power: int, allocator := context.allocator) -> (err: Error) {
  1909. context.allocator = allocator
  1910. if power < 0 || power > _MAX_BIT_COUNT { return .Invalid_Argument }
  1911. /*
  1912. Grow to accomodate the single bit.
  1913. */
  1914. a.used = (power / _DIGIT_BITS) + 1
  1915. internal_grow(a, a.used) or_return
  1916. /*
  1917. Zero the entirety.
  1918. */
  1919. mem.zero_slice(a.digit[:])
  1920. /*
  1921. Set the bit.
  1922. */
  1923. a.digit[power / _DIGIT_BITS] = 1 << uint((power % _DIGIT_BITS))
  1924. return nil
  1925. }
  1926. internal_int_get_u128 :: proc(a: ^Int) -> (res: u128, err: Error) {
  1927. return internal_int_get(a, u128)
  1928. }
  1929. internal_get_u128 :: proc { internal_int_get_u128, }
  1930. internal_int_get_i128 :: proc(a: ^Int) -> (res: i128, err: Error) {
  1931. return internal_int_get(a, i128)
  1932. }
  1933. internal_get_i128 :: proc { internal_int_get_i128, }
  1934. internal_int_get_u64 :: proc(a: ^Int) -> (res: u64, err: Error) {
  1935. return internal_int_get(a, u64)
  1936. }
  1937. internal_get_u64 :: proc { internal_int_get_u64, }
  1938. internal_int_get_i64 :: proc(a: ^Int) -> (res: i64, err: Error) {
  1939. return internal_int_get(a, i64)
  1940. }
  1941. internal_get_i64 :: proc { internal_int_get_i64, }
  1942. internal_int_get_u32 :: proc(a: ^Int) -> (res: u32, err: Error) {
  1943. return internal_int_get(a, u32)
  1944. }
  1945. internal_get_u32 :: proc { internal_int_get_u32, }
  1946. internal_int_get_i32 :: proc(a: ^Int) -> (res: i32, err: Error) {
  1947. return internal_int_get(a, i32)
  1948. }
  1949. internal_get_i32 :: proc { internal_int_get_i32, }
  1950. /*
  1951. TODO: Think about using `count_bits` to check if the value could be returned completely,
  1952. and maybe return max(T), .Integer_Overflow if not?
  1953. */
  1954. internal_int_get :: proc(a: ^Int, $T: typeid) -> (res: T, err: Error) where intrinsics.type_is_integer(T) {
  1955. size_in_bits := int(size_of(T) * 8)
  1956. i := int((size_in_bits + _DIGIT_BITS - 1) / _DIGIT_BITS)
  1957. i = min(int(a.used), i)
  1958. #no_bounds_check for ; i >= 0; i -= 1 {
  1959. res <<= uint(0) if size_in_bits <= _DIGIT_BITS else _DIGIT_BITS
  1960. res |= T(a.digit[i])
  1961. if size_in_bits <= _DIGIT_BITS {
  1962. break
  1963. }
  1964. }
  1965. when !intrinsics.type_is_unsigned(T) {
  1966. /*
  1967. Mask off sign bit.
  1968. */
  1969. res ~= 1 << uint(size_in_bits - 1)
  1970. /*
  1971. Set the sign.
  1972. */
  1973. if a.sign == .Negative { res = -res }
  1974. }
  1975. return
  1976. }
  1977. internal_get :: proc { internal_int_get, }
  1978. internal_int_get_float :: proc(a: ^Int) -> (res: f64, err: Error) {
  1979. /*
  1980. log2(max(f64)) is approximately 1020, or 17 legs with the 64-bit storage.
  1981. */
  1982. legs :: 1020 / _DIGIT_BITS
  1983. l := min(a.used, legs)
  1984. fac := f64(1 << _DIGIT_BITS)
  1985. d := 0.0
  1986. #no_bounds_check for i := l; i >= 0; i -= 1 {
  1987. d = (d * fac) + f64(a.digit[i])
  1988. }
  1989. res = -d if a.sign == .Negative else d
  1990. return
  1991. }
  1992. /*
  1993. The `and`, `or` and `xor` binops differ in two lines only.
  1994. We could handle those with a switch, but that adds overhead.
  1995. TODO: Implement versions that take a DIGIT immediate.
  1996. */
  1997. /*
  1998. 2's complement `and`, returns `dest = a & b;`
  1999. */
  2000. internal_int_and :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  2001. context.allocator = allocator
  2002. used := max(a.used, b.used) + 1
  2003. /*
  2004. Grow the destination to accomodate the result.
  2005. */
  2006. internal_grow(dest, used) or_return
  2007. neg_a := #force_inline internal_is_negative(a)
  2008. neg_b := #force_inline internal_is_negative(b)
  2009. neg := neg_a && neg_b
  2010. ac, bc, cc := DIGIT(1), DIGIT(1), DIGIT(1)
  2011. #no_bounds_check for i := 0; i < used; i += 1 {
  2012. x, y: DIGIT
  2013. /*
  2014. Convert to 2's complement if negative.
  2015. */
  2016. if neg_a {
  2017. ac += _MASK if i >= a.used else (~a.digit[i] & _MASK)
  2018. x = ac & _MASK
  2019. ac >>= _DIGIT_BITS
  2020. } else {
  2021. x = 0 if i >= a.used else a.digit[i]
  2022. }
  2023. /*
  2024. Convert to 2's complement if negative.
  2025. */
  2026. if neg_b {
  2027. bc += _MASK if i >= b.used else (~b.digit[i] & _MASK)
  2028. y = bc & _MASK
  2029. bc >>= _DIGIT_BITS
  2030. } else {
  2031. y = 0 if i >= b.used else b.digit[i]
  2032. }
  2033. dest.digit[i] = x & y
  2034. /*
  2035. Convert to to sign-magnitude if negative.
  2036. */
  2037. if neg {
  2038. cc += ~dest.digit[i] & _MASK
  2039. dest.digit[i] = cc & _MASK
  2040. cc >>= _DIGIT_BITS
  2041. }
  2042. }
  2043. dest.used = used
  2044. dest.sign = .Negative if neg else .Zero_or_Positive
  2045. return internal_clamp(dest)
  2046. }
  2047. internal_and :: proc { internal_int_and, }
  2048. /*
  2049. 2's complement `or`, returns `dest = a | b;`
  2050. */
  2051. internal_int_or :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  2052. context.allocator = allocator
  2053. used := max(a.used, b.used) + 1
  2054. /*
  2055. Grow the destination to accomodate the result.
  2056. */
  2057. internal_grow(dest, used) or_return
  2058. neg_a := #force_inline internal_is_negative(a)
  2059. neg_b := #force_inline internal_is_negative(b)
  2060. neg := neg_a || neg_b
  2061. ac, bc, cc := DIGIT(1), DIGIT(1), DIGIT(1)
  2062. #no_bounds_check for i := 0; i < used; i += 1 {
  2063. x, y: DIGIT
  2064. /*
  2065. Convert to 2's complement if negative.
  2066. */
  2067. if neg_a {
  2068. ac += _MASK if i >= a.used else (~a.digit[i] & _MASK)
  2069. x = ac & _MASK
  2070. ac >>= _DIGIT_BITS
  2071. } else {
  2072. x = 0 if i >= a.used else a.digit[i]
  2073. }
  2074. /*
  2075. Convert to 2's complement if negative.
  2076. */
  2077. if neg_b {
  2078. bc += _MASK if i >= b.used else (~b.digit[i] & _MASK)
  2079. y = bc & _MASK
  2080. bc >>= _DIGIT_BITS
  2081. } else {
  2082. y = 0 if i >= b.used else b.digit[i]
  2083. }
  2084. dest.digit[i] = x | y
  2085. /*
  2086. Convert to to sign-magnitude if negative.
  2087. */
  2088. if neg {
  2089. cc += ~dest.digit[i] & _MASK
  2090. dest.digit[i] = cc & _MASK
  2091. cc >>= _DIGIT_BITS
  2092. }
  2093. }
  2094. dest.used = used
  2095. dest.sign = .Negative if neg else .Zero_or_Positive
  2096. return internal_clamp(dest)
  2097. }
  2098. internal_or :: proc { internal_int_or, }
  2099. /*
  2100. 2's complement `xor`, returns `dest = a ~ b;`
  2101. */
  2102. internal_int_xor :: proc(dest, a, b: ^Int, allocator := context.allocator) -> (err: Error) {
  2103. context.allocator = allocator
  2104. used := max(a.used, b.used) + 1
  2105. /*
  2106. Grow the destination to accomodate the result.
  2107. */
  2108. internal_grow(dest, used) or_return
  2109. neg_a := #force_inline internal_is_negative(a)
  2110. neg_b := #force_inline internal_is_negative(b)
  2111. neg := neg_a != neg_b
  2112. ac, bc, cc := DIGIT(1), DIGIT(1), DIGIT(1)
  2113. #no_bounds_check for i := 0; i < used; i += 1 {
  2114. x, y: DIGIT
  2115. /*
  2116. Convert to 2's complement if negative.
  2117. */
  2118. if neg_a {
  2119. ac += _MASK if i >= a.used else (~a.digit[i] & _MASK)
  2120. x = ac & _MASK
  2121. ac >>= _DIGIT_BITS
  2122. } else {
  2123. x = 0 if i >= a.used else a.digit[i]
  2124. }
  2125. /*
  2126. Convert to 2's complement if negative.
  2127. */
  2128. if neg_b {
  2129. bc += _MASK if i >= b.used else (~b.digit[i] & _MASK)
  2130. y = bc & _MASK
  2131. bc >>= _DIGIT_BITS
  2132. } else {
  2133. y = 0 if i >= b.used else b.digit[i]
  2134. }
  2135. dest.digit[i] = x ~ y
  2136. /*
  2137. Convert to to sign-magnitude if negative.
  2138. */
  2139. if neg {
  2140. cc += ~dest.digit[i] & _MASK
  2141. dest.digit[i] = cc & _MASK
  2142. cc >>= _DIGIT_BITS
  2143. }
  2144. }
  2145. dest.used = used
  2146. dest.sign = .Negative if neg else .Zero_or_Positive
  2147. return internal_clamp(dest)
  2148. }
  2149. internal_xor :: proc { internal_int_xor, }
  2150. /*
  2151. dest = ~src
  2152. */
  2153. internal_int_complement :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error) {
  2154. context.allocator = allocator
  2155. /*
  2156. Temporarily fix sign.
  2157. */
  2158. old_sign := src.sign
  2159. neg := #force_inline internal_is_zero(src) || #force_inline internal_is_positive(src)
  2160. src.sign = .Negative if neg else .Zero_or_Positive
  2161. err = #force_inline internal_sub(dest, src, 1)
  2162. /*
  2163. Restore sign.
  2164. */
  2165. src.sign = old_sign
  2166. return err
  2167. }
  2168. internal_complement :: proc { internal_int_complement, }
  2169. /*
  2170. quotient, remainder := numerator >> bits;
  2171. `remainder` is allowed to be passed a `nil`, in which case `mod` won't be computed.
  2172. */
  2173. internal_int_shrmod :: proc(quotient, remainder, numerator: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
  2174. context.allocator = allocator
  2175. bits := bits
  2176. if bits < 0 { return .Invalid_Argument }
  2177. internal_copy(quotient, numerator) or_return
  2178. /*
  2179. Shift right by a certain bit count (store quotient and optional remainder.)
  2180. `numerator` should not be used after this.
  2181. */
  2182. if remainder != nil {
  2183. internal_int_mod_bits(remainder, numerator, bits) or_return
  2184. }
  2185. /*
  2186. Shift by as many digits in the bit count.
  2187. */
  2188. if bits >= _DIGIT_BITS {
  2189. internal_shr_digit(quotient, bits / _DIGIT_BITS) or_return
  2190. }
  2191. /*
  2192. Shift any bit count < _DIGIT_BITS.
  2193. */
  2194. bits %= _DIGIT_BITS
  2195. if bits != 0 {
  2196. mask := DIGIT(1 << uint(bits)) - 1
  2197. shift := DIGIT(_DIGIT_BITS - bits)
  2198. carry := DIGIT(0)
  2199. #no_bounds_check for x := quotient.used - 1; x >= 0; x -= 1 {
  2200. /*
  2201. Get the lower bits of this word in a temp.
  2202. */
  2203. fwd_carry := quotient.digit[x] & mask
  2204. /*
  2205. Shift the current word and mix in the carry bits from the previous word.
  2206. */
  2207. quotient.digit[x] = (quotient.digit[x] >> uint(bits)) | (carry << shift)
  2208. /*
  2209. Update carry from forward carry.
  2210. */
  2211. carry = fwd_carry
  2212. }
  2213. }
  2214. return internal_clamp(numerator)
  2215. }
  2216. internal_shrmod :: proc { internal_int_shrmod, }
  2217. internal_int_shr :: proc(dest, source: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
  2218. return #force_inline internal_shrmod(dest, nil, source, bits, allocator)
  2219. }
  2220. internal_shr :: proc { internal_int_shr, }
  2221. /*
  2222. Shift right by `digits` * _DIGIT_BITS bits.
  2223. */
  2224. internal_int_shr_digit :: proc(quotient: ^Int, digits: int, allocator := context.allocator) -> (err: Error) {
  2225. context.allocator = allocator
  2226. if digits <= 0 { return nil }
  2227. /*
  2228. If digits > used simply zero and return.
  2229. */
  2230. if digits > quotient.used { return internal_zero(quotient) }
  2231. /*
  2232. Much like `int_shl_digit`, this is implemented using a sliding window,
  2233. except the window goes the other way around.
  2234. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  2235. /\ | ---->
  2236. \-------------------/ ---->
  2237. */
  2238. #no_bounds_check for x := 0; x < (quotient.used - digits); x += 1 {
  2239. quotient.digit[x] = quotient.digit[x + digits]
  2240. }
  2241. quotient.used -= digits
  2242. internal_zero_unused(quotient)
  2243. return internal_clamp(quotient)
  2244. }
  2245. internal_shr_digit :: proc { internal_int_shr_digit, }
  2246. /*
  2247. Shift right by a certain bit count with sign extension.
  2248. */
  2249. internal_int_shr_signed :: proc(dest, src: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
  2250. context.allocator = allocator
  2251. if src.sign == .Zero_or_Positive {
  2252. return internal_shr(dest, src, bits)
  2253. }
  2254. internal_int_add_digit(dest, src, DIGIT(1)) or_return
  2255. internal_shr(dest, dest, bits) or_return
  2256. return internal_sub(dest, src, DIGIT(1))
  2257. }
  2258. internal_shr_signed :: proc { internal_int_shr_signed, }
  2259. /*
  2260. Shift left by a certain bit count.
  2261. */
  2262. internal_int_shl :: proc(dest, src: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
  2263. context.allocator = allocator
  2264. bits := bits
  2265. if bits < 0 { return .Invalid_Argument }
  2266. internal_copy(dest, src) or_return
  2267. /*
  2268. Grow `dest` to accommodate the additional bits.
  2269. */
  2270. digits_needed := dest.used + (bits / _DIGIT_BITS) + 1
  2271. internal_grow(dest, digits_needed) or_return
  2272. dest.used = digits_needed
  2273. /*
  2274. Shift by as many digits in the bit count as we have.
  2275. */
  2276. if bits >= _DIGIT_BITS {
  2277. internal_shl_digit(dest, bits / _DIGIT_BITS) or_return
  2278. }
  2279. /*
  2280. Shift any remaining bit count < _DIGIT_BITS
  2281. */
  2282. bits %= _DIGIT_BITS
  2283. if bits != 0 {
  2284. mask := (DIGIT(1) << uint(bits)) - DIGIT(1)
  2285. shift := DIGIT(_DIGIT_BITS - bits)
  2286. carry := DIGIT(0)
  2287. #no_bounds_check for x:= 0; x < dest.used; x+= 1 {
  2288. fwd_carry := (dest.digit[x] >> shift) & mask
  2289. dest.digit[x] = (dest.digit[x] << uint(bits) | carry) & _MASK
  2290. carry = fwd_carry
  2291. }
  2292. /*
  2293. Use final carry.
  2294. */
  2295. if carry != 0 {
  2296. dest.digit[dest.used] = carry
  2297. dest.used += 1
  2298. }
  2299. }
  2300. return internal_clamp(dest)
  2301. }
  2302. internal_shl :: proc { internal_int_shl, }
  2303. /*
  2304. Shift left by `digits` * _DIGIT_BITS bits.
  2305. */
  2306. internal_int_shl_digit :: proc(quotient: ^Int, digits: int, allocator := context.allocator) -> (err: Error) {
  2307. context.allocator = allocator
  2308. if digits <= 0 { return nil }
  2309. /*
  2310. No need to shift a zero.
  2311. */
  2312. if #force_inline internal_is_zero(quotient) {
  2313. return nil
  2314. }
  2315. /*
  2316. Resize `quotient` to accomodate extra digits.
  2317. */
  2318. #force_inline internal_grow(quotient, quotient.used + digits) or_return
  2319. /*
  2320. Increment the used by the shift amount then copy upwards.
  2321. */
  2322. /*
  2323. Much like `int_shr_digit`, this is implemented using a sliding window,
  2324. except the window goes the other way around.
  2325. */
  2326. #no_bounds_check for x := quotient.used; x > 0; x -= 1 {
  2327. quotient.digit[x+digits-1] = quotient.digit[x-1]
  2328. }
  2329. quotient.used += digits
  2330. mem.zero_slice(quotient.digit[:digits])
  2331. return nil
  2332. }
  2333. internal_shl_digit :: proc { internal_int_shl_digit, }
  2334. /*
  2335. Count bits in an `Int`.
  2336. Assumes `a` not to be `nil` and to have been initialized.
  2337. */
  2338. internal_count_bits :: proc(a: ^Int) -> (count: int) {
  2339. /*
  2340. Fast path for zero.
  2341. */
  2342. if #force_inline internal_is_zero(a) { return {} }
  2343. /*
  2344. Get the number of DIGITs and use it.
  2345. */
  2346. count = (a.used - 1) * _DIGIT_BITS
  2347. /*
  2348. Take the last DIGIT and count the bits in it.
  2349. */
  2350. clz := int(intrinsics.count_leading_zeros(a.digit[a.used - 1]))
  2351. count += (_DIGIT_TYPE_BITS - clz)
  2352. return
  2353. }
  2354. /*
  2355. Returns the number of trailing zeroes before the first one.
  2356. Differs from regular `ctz` in that 0 returns 0.
  2357. Assumes `a` not to be `nil` and have been initialized.
  2358. */
  2359. internal_int_count_lsb :: proc(a: ^Int) -> (count: int, err: Error) {
  2360. /*
  2361. Easy out.
  2362. */
  2363. if #force_inline internal_is_zero(a) { return {}, nil }
  2364. /*
  2365. Scan lower digits until non-zero.
  2366. */
  2367. x: int
  2368. #no_bounds_check for x = 0; x < a.used && a.digit[x] == 0; x += 1 {}
  2369. when true {
  2370. q := a.digit[x]
  2371. x *= _DIGIT_BITS
  2372. x += internal_count_lsb(q)
  2373. } else {
  2374. lnz := []int{
  2375. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  2376. }
  2377. q := a.digit[x]
  2378. x *= _DIGIT_BITS
  2379. if q & 1 == 0 {
  2380. p: DIGIT
  2381. for {
  2382. p = q & 15
  2383. x += lnz[p]
  2384. q >>= 4
  2385. if p != 0 { break }
  2386. }
  2387. }
  2388. }
  2389. return x, nil
  2390. }
  2391. internal_platform_count_lsb :: #force_inline proc(a: $T) -> (count: int)
  2392. where intrinsics.type_is_integer(T) && intrinsics.type_is_unsigned(T) {
  2393. return int(intrinsics.count_trailing_zeros(a)) if a > 0 else 0
  2394. }
  2395. internal_count_lsb :: proc { internal_int_count_lsb, internal_platform_count_lsb, }
  2396. internal_int_random_digit :: proc(r: ^rnd.Rand = nil) -> (res: DIGIT) {
  2397. when _DIGIT_BITS == 60 { // DIGIT = u64
  2398. return DIGIT(rnd.uint64(r)) & _MASK
  2399. } else when _DIGIT_BITS == 28 { // DIGIT = u32
  2400. return DIGIT(rnd.uint32(r)) & _MASK
  2401. } else {
  2402. panic("Unsupported DIGIT size.")
  2403. }
  2404. return 0 // We shouldn't get here.
  2405. }
  2406. internal_int_random :: proc(dest: ^Int, bits: int, r: ^rnd.Rand = nil, allocator := context.allocator) -> (err: Error) {
  2407. context.allocator = allocator
  2408. bits := bits
  2409. if bits <= 0 { return .Invalid_Argument }
  2410. digits := bits / _DIGIT_BITS
  2411. bits %= _DIGIT_BITS
  2412. if bits > 0 {
  2413. digits += 1
  2414. }
  2415. #force_inline internal_grow(dest, digits) or_return
  2416. for i := 0; i < digits; i += 1 {
  2417. dest.digit[i] = int_random_digit(r) & _MASK
  2418. }
  2419. if bits > 0 {
  2420. dest.digit[digits - 1] &= ((1 << uint(bits)) - 1)
  2421. }
  2422. dest.used = digits
  2423. return nil
  2424. }
  2425. internal_random :: proc { internal_int_random, }
  2426. /*
  2427. Internal helpers.
  2428. */
  2429. internal_assert_initialized :: proc(a: ^Int, loc := #caller_location) {
  2430. assert(internal_is_initialized(a), "`Int` was not properly initialized.", loc)
  2431. }
  2432. internal_clear_if_uninitialized_single :: proc(arg: ^Int, allocator := context.allocator) -> (err: Error) {
  2433. context.allocator = allocator
  2434. if ! #force_inline internal_is_initialized(arg) {
  2435. return #force_inline internal_grow(arg, _DEFAULT_DIGIT_COUNT)
  2436. }
  2437. return err
  2438. }
  2439. internal_clear_if_uninitialized_multi :: proc(args: ..^Int, allocator := context.allocator) -> (err: Error) {
  2440. context.allocator = allocator
  2441. for i in args {
  2442. if ! #force_inline internal_is_initialized(i) {
  2443. e := #force_inline internal_grow(i, _DEFAULT_DIGIT_COUNT)
  2444. if e != nil { err = e }
  2445. }
  2446. }
  2447. return err
  2448. }
  2449. internal_clear_if_uninitialized :: proc {internal_clear_if_uninitialized_single, internal_clear_if_uninitialized_multi, }
  2450. internal_error_if_immutable_single :: proc(arg: ^Int) -> (err: Error) {
  2451. if arg != nil && .Immutable in arg.flags { return .Assignment_To_Immutable }
  2452. return nil
  2453. }
  2454. internal_error_if_immutable_multi :: proc(args: ..^Int) -> (err: Error) {
  2455. for i in args {
  2456. if i != nil && .Immutable in i.flags { return .Assignment_To_Immutable }
  2457. }
  2458. return nil
  2459. }
  2460. internal_error_if_immutable :: proc {internal_error_if_immutable_single, internal_error_if_immutable_multi, }
  2461. /*
  2462. Allocates several `Int`s at once.
  2463. */
  2464. internal_int_init_multi :: proc(integers: ..^Int, allocator := context.allocator) -> (err: Error) {
  2465. context.allocator = allocator
  2466. integers := integers
  2467. for a in &integers {
  2468. internal_clear(a) or_return
  2469. }
  2470. return nil
  2471. }
  2472. internal_init_multi :: proc { internal_int_init_multi, }
  2473. /*
  2474. Trim unused digits.
  2475. This is used to ensure that leading zero digits are trimmed and the leading "used" digit will be non-zero.
  2476. Typically very fast. Also fixes the sign if there are no more leading digits.
  2477. */
  2478. internal_clamp :: proc(a: ^Int) -> (err: Error) {
  2479. for a.used > 0 && a.digit[a.used - 1] == 0 { a.used -= 1 }
  2480. if #force_inline internal_is_zero(a) { a.sign = .Zero_or_Positive }
  2481. return nil
  2482. }
  2483. internal_int_zero_unused :: #force_inline proc(dest: ^Int, old_used := -1) {
  2484. /*
  2485. If we don't pass the number of previously used DIGITs, we zero all remaining ones.
  2486. */
  2487. zero_count: int
  2488. if old_used == -1 {
  2489. zero_count = len(dest.digit) - dest.used
  2490. } else {
  2491. zero_count = old_used - dest.used
  2492. }
  2493. /*
  2494. Zero remainder.
  2495. */
  2496. if zero_count > 0 && dest.used < len(dest.digit) {
  2497. mem.zero_slice(dest.digit[dest.used:][:zero_count])
  2498. }
  2499. }
  2500. internal_zero_unused :: proc { internal_int_zero_unused, }
  2501. /*
  2502. ========================== End of low-level routines ==========================
  2503. */