简体   繁体   中英

FLT_HAS_SUBNORM is 0: subnormal <op> normal can produce subnormal?

ISO/IEC 9899:202x (E) working draft — February 5, 2020 C17..C2x N2479:

The presence or absence of subnormal numbers is characterized by the implementation-defined values of FLT_HAS_SUBNORM, DBL_HAS_SUBNORM, and LDBL_HAS_SUBNORM:

−1 indeterminable 27)

0 absent (type does not support subnormal numbers) 28)

1 present (type does support subnormal numbers)

28) Characterization as absent is intended if no floating-point operations produce subnormal results from non-subnormal inputs, even if the type format includes representations of subnormal numbers.

In other words 28) says: if FLT_HAS_SUBNORM is 0, then any_normal <op> any_normal never produces subnormal .

However, due to the presence of from non-subnormal inputs we can deduce that if FLT_HAS_SUBNORM is 0, then some_subnormal <op> some_normal may produce subnormal . Which is weird, because:

  1. It is compatible with the characterization as absent (ie FLT_HAS_SUBNORM is 0 ).
  2. It may produce subnormal even if type does not support subnormal numbers .

Can someone explain / resolve the weirdness?

FLT_HAS_SUBNORM is 0: subnormal <op> normal can produce subnormal ?

Yes, but it will be under UB.

=== Why it will be under UB ===

The C standard is quite restrained about FLT_HAS_SUBNORM is 0 case. All that is says about FLT_HAS_SUBNORM is 0 is:

absent (type does not support subnormal numbers)

Characterization as absent is intended if no floating-point operations produce subnormal results from non-subnormal inputs, even if the type format includes representations of subnormal numbers.

Which can be interpreted as:

If FLT_HAS_SUBNORM is 0, then any_normal <op> any_normal never produces subnormal .

The C standard seems to omit explicit definition of behavior for the following case (formulated as a question):

In case if FLT_HAS_SUBNORM is 0 : does execution of FP operations with inputs represented by manually constructed subnormals (using type punning via union, using memcpy(), reading from file, etc.) lead to undefined behavior (UB)?

If the explicit definition of behavior is omitted, then according to the C standard:

… Undefined behavior is otherwise indicated in this document… by the omission of any explicit definition of behavior…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM