https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128985

            Bug ID: 128985
           Summary: x86: __bf16 EQ/NE with -mavx10.2 -fno-trapping-math
                    uses vcomisbf16 without the PF check
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hongyuw at gcc dot gnu.org
  Target Milestone: ---
            Target: x86_64-*-* i?86-*-*

cat t.c:

__attribute__((noipa)) int eq (__bf16 a, __bf16 b) { return a == b; }
__attribute__((noipa)) int ne (__bf16 a, __bf16 b) { return a != b; }

int
main (void)
{
  __bf16 one = 1.0bf16, two = 2.0bf16;
  __bf16 nan = (__bf16) __builtin_nanf ("");
  __builtin_printf ("eq (1, 1)     = %d (expect 1)\n", eq (one, one));
  __builtin_printf ("ne (1, 2)     = %d (expect 1)\n", ne (one, two));
  __builtin_printf ("eq (NaN, NaN) = %d (expect 0)\n", eq (nan, nan));
  __builtin_printf ("ne (NaN, NaN) = %d (expect 1)\n", ne (nan, nan));
  return 0;
}

gcc -O2 -mavx10.2 -fno-trapping-math t.c && sde -dmr -- ./a.out:

eq (1, 1)     = 1 (expect 1)
ne (1, 2)     = 1 (expect 1)
eq (NaN, NaN) = 1 (expect 0)
ne (NaN, NaN) = 0 (expect 1)

Same result with GCC 15.2.0 and trunk. Code for eq:

        xorl    %eax, %eax
        vcomisbf16      %xmm1, %xmm0
        sete    %al
        ret

vcomisbf16 sets the flags like comiss: an unordered result gives
ZF=PF=CF=1. EQ needs ZF=1 && PF=0 and NE needs ZF=0 || PF=1, but only
ZF is tested here. With a NaN operand the result got flipped for EQ/NE.

Since GCC15.1 EQ/NE are treated as single-flag comparisons under TARGET_AVX10_2
(ix86_trivial_fp_comparison_operator, ix86_fp_compare_code_to_integer),
assuming vcomx/vucomx semantics. There is no vcomxbf16, so for BFmode the flags
setter is plain vcomisbf16 and the assumption doesn't hold. 

Manually widen BF compare to SF when HONOR_NANS should fix this.

Reply via email to