[LLVMdev] bitcast and bitwise operations on floating point numbers

Duncan P. N. Exon Smith
Wed, 21 Jan 2015 00:13:22 -0000

> On 2015 Jan 20, at 09:08, Simon Byrne <simonbyrne at 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