[cfe-dev] computing a conservatively rounded square of a double

Geoffrey Irving irving at naml.us
Wed Mar 26 11:36:17 PDT 2014


I am trying to compute conservative lower and upper bounds for the
square of a double.  I have set the rounding mode to FE_UPWARDS
elsewhere, so the code is

struct Interval {
  double nlo, hi;
};

Interval inspect_singleton_sqr(const double x) {
  Interval s;
  s.nlo = x * -x;
  s.hi = x * x;
  return s;
}

Both multiplies are necessary, since they round in different
directions.  However, clang does not know this, assumes that x * -x =
-(x * x), and simplifies down to a single multiply:

.LCPI1_0:
  .quad -9223372036854775808    # double -0.000000e+00
  .quad -9223372036854775808    # double -0.000000e+00
  .text
  .globl  _Z21inspect_singleton_sqrd
  .align  16, 0x90
  .type _Z21inspect_singleton_sqrd, at function
_Z21inspect_singleton_sqrd:             # @_Z21inspect_singleton_sqrd
  .cfi_startproc
# BB#0:
  vmulsd  %xmm0, %xmm0, %xmm1
  vxorpd  .LCPI1_0(%rip), %xmm1, %xmm0
  ret
.Ltmp1:
  .size _Z21inspect_singleton_sqrd, .Ltmp1-_Z21inspect_singleton_sqrd
  .cfi_endproc

I realize this is unsupported behavior, but it would be nice to still
be able to use clang to do numerical computation.  Is there a way to
convince clang to not pull the multiplication outside addition?  I
would like to avoid interfering with other optimizations in the
process: the problem is easy to solve by making a multiply function
and marking it never inline, but that would be terrible for
performance.  I am happy to write clang specific code that turns on
only when __clang__ is defined.

Thanks,
Geoffrey



More information about the cfe-dev mailing list