<div dir="ltr"><div><div>You can make up the i128 by shifting and masking.<br><br>Here's a snippet of IR from my compiler showing how i64 quantities are loaded from memory, combined to make an i128, added, and decomposed back to i64. LLVM is smart enough to figure out that the shifting isn't really required.<br><br>  %17 = getelementptr inbounds i64* %13, i32 1<br>  %18 = load i64* %17<br>  %19 = zext i64 %18 to i128<br>  %20 = shl i128 %19, 64<br>  %21 = getelementptr inbounds i64* %13, i32 0<br>  %22 = load i64* %21<br>  %23 = zext i64 %22 to i128<br>  %24 = or i128 %20, %23<br>  %25 = getelementptr inbounds i64* %15, i32 1<br>  %26 = load i64* %25<br>  %27 = zext i64 %26 to i128<br>  %28 = shl i128 %27, 64<br>  %29 = getelementptr inbounds i64* %15, i32 0<br>  %30 = load i64* %29<br>  %31 = zext i64 %30 to i128<br>  %32 = or i128 %28, %31<br>  %33 = add i128 %24, %32<br>  %34 = trunc i128 %33 to i64<br>  %35 = getelementptr inbounds i64* %16, i32 0<br>  store i64 %34, i64* %35<br>  %36 = lshr i128 %33, 64<br>  %37 = trunc i128 %36 to i64<br>  %38 = getelementptr inbounds i64* %16, i32 1<br>  store i64 %37, i64* %38<br><br></div>And here's the x64_64 assembly that LLVM produced from the above:<br><br>0000000000000000 <LMtop.Fadd>:<br>   0:   48 8b 47 18             mov    0x18(%rdi),%rax<br>   4:   48 8b 48 40             mov    0x40(%rax),%rcx<br>   8:   48 8b 50 48             mov    0x48(%rax),%rdx<br>   c:   48 03 48 30             add    0x30(%rax),%rcx<br>  10:   48 13 50 38             adc    0x38(%rax),%rdx<br>  14:   48 89 48 50             mov    %rcx,0x50(%rax)<br>  18:   48 89 50 58             mov    %rdx,0x58(%rax)<br>  1c:   c3                      retq   <br><br></div>The approach looks horrid, but the results are what you want, and work with LLVM as-is today.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 13, 2015 at 7:52 AM, Clinton Mead <span dir="ltr"><<a href="mailto:clintonmead@gmail.com" target="_blank">clintonmead@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm looking for code that produces the results that the i128 code produces but by instead using multiple i64s. Like I said, this is Haskell primops generating the llvm code, so it would really good if I could just point LLVM in the right direction instead of having to write a new primop for GHC. If I can see working LLVM code that does an efficient addition with carry on multiple machine size words I can then perhaps work out the Haskell primops that would produce that code.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 11, 2015 at 3:44 AM, David Jones <span dir="ltr"><<a href="mailto:djones@xtreme-eda.com" target="_blank">djones@xtreme-eda.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div><div>This might "just work" on many targets by using the appropriate integral type.<br><br></div>e.g. on x86_64, something like<br><br></div>%a = add i128 %b, %c<br><br></div>will lower to<br><br></div>add rax,rbx<br></div>adc rcx,rdx<br><br></div>Some caveats, based on my experience with LLVM 3.3, so things may have changed by then:<br><br></div>- Not all targets support operations on integers wider than the "native width". e.g. on x86_64, trying to multiply i256 x i256 will net you an assert. I believe i128 x i128 is handled, perhaps by a function call, but nothing larger.<br></div>- At least on x86_64, everything is fully unrolled. So adding i2048 + i2048 will generate an ADD and 31 ADC instructions, with no looping. This makes sense for "small" multiprecision integers, but can get silly as things get wider.<br><br></div>In my project, I am using i128 in a few places (add, subtract, shift) to get efficient code, and relying on my own library functions for anything wider.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Mon, Nov 9, 2015 at 11:46 PM, Clinton Mead via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">I'm trying to work out LLVM code which generates something similar to the following when adding large multiword numbers stored as separate words:<div><br></div><div>ADD x1 x1</div><div>ADC x2 y2</div><div>ADC x3 y3</div><div><br></div><div>etc, where such a three argument add like ADC on x86 (which includes a carry in the addition) is available as a machine op.</div><div><br></div><div>The background to this is that I'm trying to implement fast multiword addition in Haskell, which can compile via LLVM, so I thought if I knew what LLVM code generates the ideal assembly I can work backwards and use the appropriate Haskell prim-ops. </div><div><br></div><div>Of course one can use GMP, but I'd suspect with two-four word additions the loss of inlining opportunities and the cost of the external call would be greater than the addition itself. So it would be great to be able to work out how to do it in LLVM and then either implement it with current GHC or add an extra GHC prim-op which allows GHC to access the appropriate LLVM code.</div><div><br></div><div>Sorry I don't know much about LLVM besides my brief readings of the docs, and I've posted here because I couldn't find an llvm-users list. There's a clang-users list, but I didn't think that would be appropriate because GHC compiles via LLVM directly, not via clang.</div><div><br></div><div>Any help or guidance appreciated.</div><span><font color="#888888"><div><br></div><div>Clinton</div><div><br></div></font></span></div>
<br></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</blockquote></div><br></div>