Skip to content

[libc][math] fdim functions compile to incorrect code #88771

Open
@Flandini

Description

@Flandini

Follow up on #61092 for the reported cases of fdim(-inf, -inf) raising FE_INVALID when it shouldn't and fdim(-max, -den) raising FE_INEXACT when it shouldn't.

Looks like this is caused by compiler optimizations https://godbolt.org/z/z3x8zGWzb evaluating both sides of a conditional operator on O1-O3 in this code.

I am using clang-14, and I get the following asm for fdim:

Dump of assembler code for function __llvm_libc_19_0_0_git::fdim(double, double):
   0x0000000000408440 <+0>:	push   %rbp
   0x0000000000408441 <+1>:	mov    %rsp,%rbp
   0x0000000000408444 <+4>:	movabs $0x7fffffffffffffff,%rax
   0x000000000040844e <+14>:	movabs $0x7ff0000000000000,%rcx
   0x0000000000408458 <+24>:	movq   %xmm0,%rdx
   0x000000000040845d <+29>:	and    %rax,%rdx
   0x0000000000408460 <+32>:	cmp    %rcx,%rdx
   0x0000000000408463 <+35>:	ja     0x408487 <__llvm_libc_19_0_0_git::fdim(double, double)+71>
   0x0000000000408465 <+37>:	movq   %xmm1,%rdx
   0x000000000040846a <+42>:	and    %rax,%rdx
   0x000000000040846d <+45>:	cmp    %rcx,%rdx
   0x0000000000408470 <+48>:	ja     0x408483 <__llvm_libc_19_0_0_git::fdim(double, double)+67>
   0x0000000000408472 <+50>:	movdqa %xmm0,%xmm2
   0x0000000000408476 <+54>:	subsd  %xmm1,%xmm2
   0x000000000040847a <+58>:	cmpltsd %xmm0,%xmm1
   0x000000000040847f <+63>:	andpd  %xmm2,%xmm1
   0x0000000000408483 <+67>:	movapd %xmm1,%xmm0
   0x0000000000408487 <+71>:	pop    %rbp
   0x0000000000408488 <+72>:	retq

Changing the conditional operator to an if-statement gives assembly that doesn't raise these exceptions, but this seems brittle; messing around with compiler explorer, I also sometimes get the same problematic assembly with an if-statement.

In discord, philnik brought up that we need to set FENV_ACCESS to on since clang defaults to off.

Tagging @nickdesaulniers @michaelrj-google.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions