llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] [x86_64] __builtin_fpclassify raises FE_INVALID for denormal numbers

Open zatrazz opened this issue 11 months ago • 3 comments

On x86_64:

$ cat t.c
#include <stdint.h>
#include <fenv.h>
#include <math.h>

typedef union
{
  long double value;
  struct
  {
    uint32_t lsw;
    uint32_t msw;
    int sign_exponent:16;
    unsigned int empty1:16;
    unsigned int empty0:32;
  } parts;
} ieee_long_double_shape_type;

static const ieee_long_double_shape_type pseudo_zero = { .parts = { 0x00000000, 0x00000000, 0x0100 } };

int main (int argc, char *argv[])
{
  feenableexcept (FE_INVALID);
  return __builtin_fpclassify (FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO,  pseudo_zero.value);
}
$ clang t.c -o t -D_GNU_SOURCE -lm
$ ./t
Floating point exception

The glibc math.h adds a fpclassify optimization to use the __builtin_fpclassify, but it is disabled when -fsignaling-nan is used (GCC bug 66462). It seems always enabled on clang/x86_64 because it does define __SUPPORT_SNAN__.

zatrazz avatar Dec 16 '25 18:12 zatrazz

Could you assign this to me?

ItsGunjit avatar Dec 16 '25 18:12 ItsGunjit

@llvm/issue-subscribers-backend-x86

Author: Adhemerval Zanella (zatrazz)

On x86_64:
$ cat t.c
#include &lt;stdint.h&gt;
#include &lt;fenv.h&gt;
#include &lt;math.h&gt;

typedef union
{
  long double value;
  struct
  {
    uint32_t lsw;
    uint32_t msw;
    int sign_exponent:16;
    unsigned int empty1:16;
    unsigned int empty0:32;
  } parts;
} ieee_long_double_shape_type;

static const ieee_long_double_shape_type pseudo_zero = { .parts = { 0x00000000, 0x00000000, 0x0100 } };

int main (int argc, char *argv[])
{
  feenableexcept (FE_INVALID);
  return __builtin_fpclassify (FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO,  pseudo_zero.value);
}
$ clang t.c -o t -D_GNU_SOURCE -lm
$ ./t
Floating point exception

The glibc math.h adds a fpclassify optimization to use the __builtin_fpclassify, but it is disabled when -fsignaling-nan is used (GCC bug 66462). It seems always enabled on clang/x86_64 because it does define __SUPPORT_SNAN__.

llvmbot avatar Dec 17 '25 01:12 llvmbot

@llvm/issue-subscribers-clang-frontend

Author: Adhemerval Zanella (zatrazz)

On x86_64:
$ cat t.c
#include &lt;stdint.h&gt;
#include &lt;fenv.h&gt;
#include &lt;math.h&gt;

typedef union
{
  long double value;
  struct
  {
    uint32_t lsw;
    uint32_t msw;
    int sign_exponent:16;
    unsigned int empty1:16;
    unsigned int empty0:32;
  } parts;
} ieee_long_double_shape_type;

static const ieee_long_double_shape_type pseudo_zero = { .parts = { 0x00000000, 0x00000000, 0x0100 } };

int main (int argc, char *argv[])
{
  feenableexcept (FE_INVALID);
  return __builtin_fpclassify (FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO,  pseudo_zero.value);
}
$ clang t.c -o t -D_GNU_SOURCE -lm
$ ./t
Floating point exception

The glibc math.h adds a fpclassify optimization to use the __builtin_fpclassify, but it is disabled when -fsignaling-nan is used (GCC bug 66462). It seems always enabled on clang/x86_64 because it does define __SUPPORT_SNAN__.

llvmbot avatar Dec 17 '25 01:12 llvmbot