[英]Division using GMP's low-level API
我正在使用 GMP 的低級接口( mpn_
,請參見https://gmplib.org/manual/Low_002dlevel-Functions )進行一些固定大小的 192 位(三肢)integer 計算。
目前,我正在嘗試將一個隨機 uint192 除以另一個隨機 uint192,但無法將 select 正確地分配給 function。 有多個候選人:
mpn_tdiv_qr
- 文檔說“除數的最重要部分必須非零”mpn_divrem
- 文檔說“過時”mpn_divrem_1
- 將單個肢體作為除數mpn_divexact_1
- 文檔說“期望......准確划分” So there is an obsolete function ( mpn_divrem
), a function that takes small (<= 64bit) divisors only ( mpn_divrem_1
), one that has undefined behavior with remainders ( mpn_divexact_1
) and one that takes large (>= 129bit) divisors only ( mpn_tdiv_qr
)。
我嘗試mpn_tdiv_qr
,因為我認為我誤解了文檔並且因為它取代了過時的 function。 但是當最重要的肢體為零時,它實際上會崩潰。
我錯過了什么? 如何划分兩個隨機的 3 肢數? 或除數為 65..128 個非零位的數字(例如0xffffffff'ffffffffffffffff'ffffffffffffffff / 0xff'ffffffffffffffff
)?
好的,解決方案很簡單:不是將分配的肢體數傳遞給mpn_tdiv_qr
,而是通過肢體數減去除數的前導零肢體數。 例如,當使用 3 個肢體和一個具體的除數時, limb[2] == 0
, limb[1] != 0
並且limb[0] == 0
a 2
被傳遞給除數長度。 這樣可以保證最重要的肢體(在這種情況下[1]
)是非零的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.