Fix prelude definitions of abs/signum for Floats/Doubles

Tyson Whitehead twhitehead at gmail.com
Thu Apr 11 17:05:27 CEST 2013


On April 11, 2013 04:24:26 Simon Peyton-Jones wrote:
> It would be fantastic if someone could investigate Levent's suggestion "Of
> course, implementations can take advantage of the underlying CPU's native
> floating-point abs/sign functions if available as well, avoiding explicit
> tests at the Haskell code; based on the underlying platform"

Compiling

  double test(double value) {
    return fabs(value)
  }

with -O2 -S in gcc gives you

test:
.LFB3:
	.cfi_startproc
	movsd	.LC0(%rip), %xmm1
	andpd	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE3:
	.size	test, .-test
	.section	.rodata.cst16,"aM", at progbits,16
	.align 16
.LC0:
	.long	4294967295
	.long	2147483647
	.long	0
	.long	0
	.ident	"GCC: (Debian 4.7.2-5) 4.7.2"
	.section	.note.GNU-stack,"", at progbits

That is, it does an andpd (and packed double) operation against the constant 
2^63-1 (all 1 except the top bit) to just mask out the sign bit.

This should work with NaN and Inf too as the former ignores the sign bit and 
the later interpets it in the expectd manner.

Cheers!  -Tyson



More information about the Libraries mailing list