[LLVMdev] bitcast and bitwise operations on floating point numbers

86 views
Skip to first unread message

Simon Byrne

unread,
Jan 20, 2015, 2:15:58 PM1/20/15
to llv...@cs.uiuc.edu
There are some occasions where it is useful to perform bitwise AND/OR
on floating point numbers (e.g. when working with extended precision).

An easy way to do this is to bitcast to an unsigned integer, do your
operation, then bitcast back to a float, which will generate IR along
the lines of:

%1 = bitcast double %0 to i64
%2 = and i64 %1, -4294967296
%3 = bitcast i64 %2 to double
ret double %3

Is LLVM able translate this to use the relevant floating point
instructions when available (i.e. ANDPD/VANDPD on x86)?

(this originally came up on julia-dev:
https://groups.google.com/d/topic/julia-dev/5y1y6Uj8T1A/discussion)

Simon
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Duncan P. N. Exon Smith

unread,
Jan 20, 2015, 7:22:14 PM1/20/15
to Simon Byrne, llv...@cs.uiuc.edu

> On 2015 Jan 20, at 09:08, Simon Byrne <simon...@gmail.com> wrote:
>
> There are some occasions where it is useful to perform bitwise AND/OR
> on floating point numbers (e.g. when working with extended precision).
>
> An easy way to do this is to bitcast to an unsigned integer, do your
> operation, then bitcast back to a float, which will generate IR along
> the lines of:
>
> %1 = bitcast double %0 to i64
> %2 = and i64 %1, -4294967296
> %3 = bitcast i64 %2 to double
> ret double %3
>
> Is LLVM able translate this to use the relevant floating point
> instructions when available (i.e. ANDPD/VANDPD on x86)?
>
> (this originally came up on julia-dev:
> https://groups.google.com/d/topic/julia-dev/5y1y6Uj8T1A/discussion)
>
> Simon

Best way to find out is to run it. Looks like no, but maybe it would if
you played around with the triple.

$ cat t.ll
define double @foo(double %arg) {
%1 = bitcast double %arg to i64
%2 = and i64 %1, -4294967296
%3 = bitcast i64 %2 to double
ret double %3
}
$ opt -O3 < t.ll | llc -O3
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 14, 1
.globl _foo
.align 4, 0x90
_foo: ## @foo
## BB#0:
movd %xmm0, %rax
movabsq $-4294967296, %rcx ## imm = 0xFFFFFFFF00000000
andq %rax, %rcx
movd %rcx, %xmm0
retq


.subsections_via_symbols
Reply all
Reply to author
Forward
0 new messages