将 u128 复制到 [u64;2]

A. *_* K. 1 types arbitrary-precision rust

如何u128将 a 转换为 s 数组u64。当将 a 传递u128给采用任意精度 int 的 API 时,该 API 需要 3 个参数

LLVMValueRef LLVMConstIntOfArbitraryPrecision   (
  LLVMTypeRef IntTy,
  unsigned  NumWords,
  const uint64_t Words[] 
)
Run Code Online (Sandbox Code Playgroud)

前两个参数已知(IntTy=LLVMInt128TypeNumWords= 2)。第三个参数需要一个 s 数组uint64_t。提供的u128需要转换为u64的数组。从rust 文档来看,它似乎u128可以转换为字节数组,例如

let buff: [u8; 16] = u128_val.to_ne_bytes();
let Words: [u64; 2] = ?? // What to do here?
Run Code Online (Sandbox Code Playgroud)

如何buff转换为数组Words?另外,如何处理字节序。为简单起见,代码生成器和 API 将在具有相同字节序的机器上运行。

Sta*_*eur 8

我只是手工制作,这样可以避免任何不安全,并且很直接并且关心字节顺序:

pub fn foo(a: u128) -> [u64; 2] {
    [(a >> 64) as u64, a as u64]
}
Run Code Online (Sandbox Code Playgroud)

这翻译为:

playground::foo:
    movq    %rdi, %rax
    movq    %rdx, (%rdi)
    movq    %rsi, 8(%rdi)
    retq
Run Code Online (Sandbox Code Playgroud)