0

If the presence of subnormal numbers is optional (HAS_SUBNORM feature macros), then why the presence of FP_SUBNORMAL classification macro is mandatory?

Reason of the question: in this (still unanswered) question it was concluded / hypothesized that:

In case if HAS_SUBNORM is 0 the execution of any FP operation with inputs represented by manually constructed subnormal numbers (constructed using type punning via union, using memcpy, reading from file, etc.) leads to undefined behavior (UB).

This conclusion / hypothesis leads to multiple questions:

  1. Does it mean that in case of HAS_SUBNORM is 0 it is not possible using C standard library to even detect the presence of subnormal numbers provided, for example, by the user?

Example:

float value = get_value();       // user input
#if FLT_HAS_SUBNORM == 0
int class = fpclassify( value ); // leads to UB if value is subnormal number
if ( class == FP_SUBNORMAL  )
{
    error( "subnormal numbers are not supported" );
}
#endif
  1. Should the presence of FP_SUBNORMAL depend on disjunction of HAS_SUBNORM macros set to 1? I.e.:
#if FLT_HAS_SUBNORM == 1 || DBL_HAS_SUBNORM == 1 || LDBL_HAS_SUBNORM == 1
// FP_SUBNORMAL is present
#else
// FP_SUBNORMAL is absent
#endif
  1. Does the fact that the FP_SUBNORMAL classification macro is mandatory signify that in case of HAS_SUBNORM is 0 the execution of fpclassify macro with inputs represented by manually constructed subnormal numbers shall lead to well-defined behavior?

UPD 20210527. In comparison with rounding modes (C11, 7.6.8) (emphasis added):

Each of the macros FE_DOWNWARD ... is defined if and only if the implementation supports getting and setting the represented rounding direction by means of the fegetround and fesetround functions.

pmor
  • 5,392
  • 4
  • 17
  • 36
  • 2
    What if I have cross-platform code that tests for `FP_SUBNORMAL` and try to run it on a system that doesn't support `HAS_SUBNORMAL`? I would still want `fpclassify` to work, and to indicate that my test value is *not* subnormal. – Adrian Mole Feb 26 '21 at 12:19
  • 1
    3. How could you manually construct a subnormal number if they are not supported? – Ian Abbott Feb 26 '21 at 13:00
  • 1
    @IanAbbott: Write to the bytes that represent the floating-point object through pointers to a character type. – Eric Postpischil Feb 26 '21 at 14:20
  • Read a float from a binary file created on a system that does support subnormals. – stark Feb 26 '21 at 14:35
  • @EricPostpischil Well yes, but you can write any old junk value using that method. You cannot write a well-defined subnormal value if well-defined subnormal values do not exist. – Ian Abbott Feb 26 '21 at 14:41
  • 2
    @IanAbbott: A floating-point specification can have defined subnormal values without defining operations upon those values. The C standard defines a floating-point number as a sign multiplied by a base raised to a power multiplied by a numeral with a fixed number of digits in the base, and it categorizes normalized numbers to be those that start with a non-zero digit other than when they are all zeros, and subnormal numbers to be those starting with a zero digit (but not all zeros) and the minimum exponent. So the model contains subnormal numbers but does not require supporting them. – Eric Postpischil Feb 26 '21 at 14:47
  • @EricPostpischil If the implementation does not support subnormal values but the binary representation format supports them, the implementation will be interpreting the bit pattern of the intended subnormal number according to its own rules. It could treat all intended subnormal numbers as a representation of the number 0, for example. – Ian Abbott Feb 26 '21 at 15:35
  • @IanAbbott: Sure. – Eric Postpischil Feb 26 '21 at 15:41
  • @AdrianMole _What if I have cross-platform code that tests for FP_SUBNORMAL_: prior to testing for `FP_SUBNORMAL` at run time such cross-platform code should check whether subnormal numbers are supported by checking `HAS_SUBNORMAL` at compile time. – pmor May 27 '21 at 16:00
  • @pmor [code should check whether subnormal numbers are supported](https://stackoverflow.com/questions/66385668/if-the-presence-of-subnormal-numbers-is-optional-has-subnorm-then-why-the-pre#comment119709063_66385668) is more like "_could_ check" (yes that's possible) versus _should_. By having FP_SUBNORMAL classification macro, even when subnormal numbers is optional makes coding easier. To your overall point, the language could have been defined as you imply, but it was not. – chux - Reinstate Monica May 27 '21 at 16:22
  • @chux-ReinstateMonica What confuses me is the absence of consistency with, for example, rounding mode macros, which are _defined if and only if the implementation supports getting and setting the represented rounding direction_ (C11, 7.6.8). Which is clear and logical: why require some feature related definitions if the feature is not supported in the first place? – pmor May 27 '21 at 17:03
  • Rounding modes are used for input - to change functionality. They direct a mode. `FP_SUBNORMAL` is used solely to test a state. It does not cause sub-normal support, simply tests for it. – chux - Reinstate Monica May 27 '21 at 17:22

0 Answers0