iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

Implementing IEEE 754-2019 minimum, maximum, minimumNumber, and maximumNumber Operations

に公開
2026/01/27

As I mentioned in "Floating-Point min / max", there are several variations for min/max operations of floating-point numbers.

In this article, I will implement the following operations specified in IEEE 754-2019 using C:

  • minimum/maximum: Propagates NaNs
  • minimumNumber/maximumNumber: Treats NaNs as missing data

Compared to a naive implementation like x < y ? x : y, those specified in IEEE 754-2019 have desirable properties such as being "commutative" and "associative". Since these properties might be useful for parallelization, I will also include SIMD versions.

minimum/maximum

First, let's look at the minimum/maximum operations. We'll start by checking the specifications.

Specifications

Regarding the sign of zero, these operations treat -0 < +0. If at least one input is a (quiet or signaling) NaN, they return a quiet NaN (while raising an exception for signaling NaNs).

In other words, minimum returns x if x < y, and y if y < x. If one input is a NaN, it returns some quiet NaN (while raising an exception for signaling NaNs). If x = y including the sign, it returns either x or y.

On the other hand, maximum returns x if x > y, and y if y > x. If one input is a NaN, it returns some quiet NaN (while raising an exception for signaling NaNs). If x = y including the sign, it returns either x or y.

Let me add a note about "either x or y if x = y". The float and double types we usually use have identical bit patterns if they are numerically equal (and not NaN) and have the same sign bit, making them indistinguishable. However, in other floating-point formats defined by IEEE 754, bit patterns are not necessarily the same even if the values and sign bits are numerically equal. Specifically, because the decimal format does not use a "hidden bit" representation, the same numerical value could have a different "number of trailing zeros," such as 1. and 1.00. Therefore, IEEE 754 explicitly allows "returning either one" in such cases.

Regarding the canonicality of the resulting bit pattern, there is no specific mention, so I assume it is expected to be canonical, though I am not certain.

As for floating-point exceptions, the general principle is that "if a quiet NaN is included in the input, no exception is raised (unless another input was a signaling NaN)". Thus, it can be interpreted as:

  • At least one input was a signaling NaN -> Raise an invalid operation exception. By default, set the flag and return a quiet NaN.
  • No signaling NaN was present, but a quiet NaN was -> No exception is raised, and a quiet NaN is returned.

Implementation Example in C

Based on the above, here is an implementation example of the minimum operation in C:

#include <fenv.h>
#include <math.h>

float my_fminimumf(float x, float y)
{
#pragma STDC FENV_ACCESS ON
    if (isless(x, y)) {
        return x;
    } else if (isgreater(x, y)) {
        return y;
    } else if (isunordered(x, y)) {
        // If at least one input was a NaN
        return x + y;
    } else {
        // If inputs are numerically equal
        return signbit(x) ? x : y;
    }
}

The same applies to double.

In C23, you will be able to use IEEE 754-2019 minimum/maximum operations under names like fminimum/fmaximum, but at the time of writing, environments that support them are likely still few. However, I have followed the C23 naming conventions for the functions implemented here.

Regarding the behavior, first, to satisfy the requirement that "no exception is raised even if a quiet NaN is included in the input," we use isless and isgreater for comparison instead of operators like < or > (see also "About Comparison of Floating-Point Numbers").

Whether at least one input is a NaN can be determined using the isunordered macro. This also does not raise an exception if the input is a quiet NaN.

If at least one input is a NaN, we use an appropriate binary operation to propagate it to the output. Here, I used +. Since the input contains a NaN, no exceptions other than invalid operation will be raised. And the invalid operation exception will only occur when a signaling NaN is included.

Even if the inputs are numerically equal, we want to distinguish the sign of zero. Therefore, we use the signbit macro to return x if the sign bit of x is set, and y otherwise. This part could also be implemented using bitwise OR (assuming the bit patterns are identical except for the sign bit).

Since float is usually in a binary interchange format and all its bit patterns are canonical, consideration for non-canonical patterns is unnecessary.

If a signaling NaN is included in the input, an invalid operation exception will be raised by isless, isgreater, isunordered, or +. Alternatively, in C, it is also possible that an invalid operation exception occurs during the passing of arguments to or returning values from a function, causing it to be converted to a quiet NaN.

SIMD Version: x86 SSE

x86 has instructions like min{s,p}{s,d}/max{s,p}{s,d} to return the minimum and maximum values, but these are identical (including behavior regarding exceptions) to the following C code:

float minss(float x, float y) { return x < y ? x : y; }
float maxss(float x, float y) { return x > y ? x : y; }

In other words:

  • If the input contains a NaN, an exception is raised even for quiet NaNs, and y is returned.
  • If both inputs are 0, y is returned regardless of the sign bits.

Therefore, these cannot be used directly for implementing the minimum operation. To use x86 min instructions, it is necessary to:

  • Convert NaNs to harmless values beforehand if the input contains them.
  • Handle the sign of zero.

Accordingly, we consider code like the following:

__m128 my_fminimum_floatx4(__m128 x, __m128 y)
{
    __m128 ord = _mm_cmpord_ps(x, y); // non-signaling compare
    __m128 x_ord = _mm_and_ps(x, ord); // convert possible NaN to zero
    __m128 y_ord = _mm_and_ps(y, ord); // convert possible NaN to zero
    __m128 result_ltgt = _mm_min_ps(x_ord, y_ord); // x_ord < y_ord ? x_ord : y_ord
    __m128 eq = _mm_cmpeq_ps(x, y); // non-signaling compare
    __m128 result_eq = _mm_and_ps(eq, x);
    __m128 result_ord = _mm_or_ps(result_eq, result_ltgt); // ordered
    __m128 x_unord = _mm_andnot_ps(ord, x); // ~ord & x
    __m128 y_unord = _mm_andnot_ps(ord, y); // ~ord & y
    __m128 result_unord = _mm_add_ps(x_unord, y_unord); // propagate NaN
    return _mm_or_ps(result_ord, result_unord);
}

This is a SIMD version of the following pseudo-code in C:

float x_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : x;
float y_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : y;
float result_ltgt = /* x86 min */ x_ord < y_ord ? x_ord : y_ord;
float eq = /* bitwise and */ x == y ? x : 0.0f;
float result_ord = bitwise_or(eq, result_ltgt);
float x_unord = /* bitwise and-not */ isunordered(x, y) ? x : 0.0f;
float y_unord = /* bitwise and-not */ isunordered(x, y) ? y : 0.0f;
float result_unord = x_unord + y_unord; // NaN propagation
return bitwise_or(result_ord, result_unord);

The maximum version can be implemented as follows:

__m128 my_fmaximum_floatx4(__m128 x, __m128 y)
{
    __m128 ord = _mm_cmpord_ps(x, y); // non-signaling compare
    __m128 x_ord = _mm_and_ps(x, ord); // convert possible NaN to zero
    __m128 y_ord = _mm_and_ps(y, ord); // convert possible NaN to zero
    __m128 result_ltgt = _mm_max_ps(x_ord, y_ord); // x_ord > y_ord ? x_ord : y_ord
    __m128 neq = _mm_cmpneq_ps(x, y); // non-signaling (LT || GT || UNORD)
    __m128 result_ord = _mm_and_ps(_mm_or_ps(neq, x), result_ltgt);
    __m128 x_unord = _mm_andnot_ps(ord, x); // ~ord & x
    __m128 y_unord = _mm_andnot_ps(ord, y); // ~ord & y
    __m128 result_unord = _mm_add_ps(x_unord, y_unord); // propagate NaN
    return _mm_or_ps(result_ord, result_unord);
}

This is a SIMD version of the following pseudo-code in C:

float x_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : x;
float y_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : y;
float result_ltgt = /* x86 max */ x_ord > y_ord ? x_ord : y_ord;
float mask = /* bitwise or */ x == y ? x : bitcast(~0)
float result_ord = bitwise_and(mask, result_ltgt);
float x_unord = /* bitwise and-not */ isunordered(x, y) ? x : 0.0f;
float y_unord = /* bitwise and-not */ isunordered(x, y) ? y : 0.0f;
float result_unord = x_unord + y_unord; // NaN propagation
return bitwise_or(result_ord, result_unord);

SIMD Version: x86 AVX

AVX has an instruction to check "whether all sign bits in a mask are 0". Using this, we can check "whether the input contains any NaNs" and perform the complex processing only when a NaN is present.

__m256 my_fminimum_floatx8(__m256 xx, __m256 yy)
{
    __m256 unord = _mm256_cmp_ps(xx, yy, 3); // non-signaling UNORD compare
    if (!_mm256_testz_ps(unord, unord)) {
        // Some NaN in input
        __m256 x_ord = _mm256_andnot_ps(unord, xx); // convert possible NaN to zero
        __m256 y_ord = _mm256_andnot_ps(unord, yy); // convert possible NaN to zero
        __m256 result_ltgt = _mm256_min_ps(x_ord, y_ord);
        __m256 eq = _mm256_cmp_ps(xx, yy, 0); // non-signaling EQ compare
        __m256 result_eq = _mm256_and_ps(eq, xx);
        __m256 result_ord = _mm256_or_ps(result_eq, result_ltgt); // ordered
        __m256 x_unord = _mm256_and_ps(unord, xx); // ~ord & x
        __m256 y_unord = _mm256_and_ps(unord, yy); // ~ord & y
        __m256 result_unord = _mm256_add_ps(x_unord, y_unord); // propagate NaN
        return _mm256_or_ps(result_ord, result_unord);
    } else {
        // No NaN in input
        __m256 eq = _mm256_cmp_ps(xx, yy, 0); // EQ, non-signaling
        __m256 zz_ltgt = _mm256_min_ps(xx, yy);
        __m256 zz_eq = _mm256_and_ps(eq, xx);
        return _mm256_or_ps(zz_eq, zz_ltgt);
    }
}

This is about the extent of the improvements possible with AVX.

SIMD Version: x86 AVX-512

In AVX-512DQ, a different line of min/max instructions from the SSE-era min{s,p}{s,d} is added: vrange{s,p}{s,d}. This treats the sign of zero as -0 < +0 and treats quiet NaNs in the input as "missing data". In other words, it complies with IEEE 754-2008 minNum/maxNum. Personally, I don't mind this, as it shows Intel's awareness of IEEE 754-2008 in AVX-512.

An implementation example of the minimum operation using this is as follows:

__m512 my_fminimum_floatx16(__m512 xx, __m512 yy)
{
    __m512 m = _mm512_range_ps(xx, yy, 4); // NaN is missing data
    __mmask16 unord = _mm512_cmp_ps_mask(xx, yy, 0x3); // UNORD (quiet)
    return _mm512_mask_add_ps(m, unord, xx, yy); // propagate NaN
}

The idea is to calculate the minimum using the vrange series of instructions and replace lanes containing NaNs with x + y.

x86 AVX10.2

As I mentioned in "New Features of AVX10.2", instructions corresponding to the IEEE 754-2019 minimum operation will be added in AVX10.2. That's good news.

AArch64

AArch64 has instructions corresponding to the IEEE 754-2019 compliant minimum operation: fmin. Therefore, it can be implemented using GCC inline assembly as follows:

float my_fminimumf(float x, float y)
{
    float result;
    asm("fmin %s0, %s1, %s2" : "=w"(result) : "w"(x), "w"(y));
    return result;
}

For double, there are ACLE intrinsic functions:

double my_fminimum(double x, double y)
{
    float64x1_t xx = vld1_f64(&x);
    float64x1_t yy = vld1_f64(&y);
    float64x1_t zz = vmin_f64(xx, yy);
    double result;
    vst1_f64(&result, zz);
    return result;
    /*
    double result;
    asm("fmin %d0, %d1, %d2" : "=w"(result) : "w"(x), "w"(y));
    return result;
    */
}

There is, of course, a SIMD version as well.

float32x4_t my_fminimum_floatx4(float32x4_t x, float32x4_t y)
{
    return vminq_f32(x, y);
}

It’s almost too simple compared to x86. Is this the power of a later ISA?

minimumNumber/maximumNumber

Now let's look at the minimumNumber/maximumNumber operations. First, we will check the specifications.

Specifications

Regarding the sign of zero, these also treat -0 < +0. If at least one input is a (quiet or signaling) NaN, they treat the NaN as "missing data". That is, if both inputs are NaN, they return a quiet NaN; if only one input is a NaN, they return the other one (even if it is a signaling NaN).

In other words, minimumNumber returns x if x < y, and y if y < x. If only one input is a NaN, it returns the other one (while raising an exception for signaling NaNs), and if both are NaNs, it returns some quiet NaN. If x = y including the sign, it returns either x or y.

On the other hand, maximumNumber returns x if x > y, and y if y > x. If only one input is a NaN, it returns the other one (while raising an exception for signaling NaNs), and if both are NaNs, it returns some quiet NaN. If x = y including the sign, it returns either x or y.

Regarding the canonicality of the resulting bit pattern, there is no specific mention, so I assume it is expected to be canonical, though I am not certain.

As for floating-point exceptions, the general principle is that "if a quiet NaN is included in the input, no exception is raised (unless another input was a signaling NaN)". Thus, it can be interpreted as:

  • At least one input was a signaling NaN -> Raise an invalid operation exception. In the default processing, the flag is set, but this operation returns the non-NaN input if one exists.
  • No signaling NaN was present, but a quiet NaN was -> No exception is raised.

Implementation Example in C

Based on the above, here is an implementation example of the minimumNumber operation in C:

#include <fenv.h>
#include <math.h>

static inline
float my_canonicalizef(float x)
{
    volatile float one = 1.0f;
    return x * one;
}

float my_fminimum_numf(float x, float y)
{
#pragma STDC FENV_ACCESS ON
    if (isless(x, y)) {
        return x;
    } else if (isgreater(x, y)) {
        return y;
    } else if (isunordered(x, y)) {
        x = my_canonicalizef(x);
        y = my_canonicalizef(y);
        return isnan(x) ? y : x; // discard NaN
    } else {
        // equal or both zero
        return signbit(x) ? x : y;
    }
}

The same applies to double.

In C23, you will be able to use IEEE 754-2019 minimumNumber/maximumNumber operations under names like fminimum_num/fmaximum_num, but at the time of writing, environments that support them are likely still few. However, I have followed the C23 naming conventions for the functions implemented here.

NaNs in the input are treated as "missing data". If we only consider quiet NaNs, this behavior could be implemented with:

return isnan(x) ? y : x;

However, this is insufficient when considering signaling NaNs. We cannot return a signaling NaN when both inputs are signaling NaNs.

Here, we convert signaling NaNs to quiet NaNs by applying some floating-point operation that mathematically acts as an identity map. x * 1.0f is suitable, but I used volatile to prevent it from being removed by compiler optimization.

A function that "does not change the numerical value of the input but converts non-canonical bit patterns to canonical ones and signaling NaNs to quiet NaNs" is called canonicalize in C23. I have imitated that naming convention here.

my_canonicalizef can also be implemented as follows:

#if defined(__SSE2__)
__attribute__((always_inline)) static inline
float my_canonicalizef(float x)
{
    asm volatile("mulss %1, %0" : "+x"(x) : "x"(1.0f));
    return x;
}
#elif defined(__aarch64__)
__attribute__((always_inline)) static inline
float my_canonicalizef(float x)
{
    float result;
    asm volatile("fmul %s0, %s1, %s2" : "=w"(result) : "w"(x), "w"(1.0f));
    return result;
}
#else
__attribute__((always_inline)) static inline
float my_canonicalizef(float x)
{
    volatile float one = 1.0f;
    return x * one;
}
#endif

SIMD Version: x86 SSE

Consider the following code:

__m128 my_fminimum_num_floatx4(__m128 x, __m128 y)
{
    // Convert (possible) signaling NaN to quiet one
    __m128 one = _mm_set1_ps(1.0f);
    asm volatile("mulps %x1, %x0" : "+x"(x) : "x"(one));
    asm volatile("mulps %x1, %x0" : "+x"(y) : "x"(one));
    __m128 ord = _mm_cmpord_ps(x, y); // non-signaling compare
    __m128 x_ord = _mm_and_ps(x, ord); // convert possible NaN to zero (avoid undue INVALID)
    __m128 y_ord = _mm_and_ps(y, ord); // convert possible NaN to zero (avoid undue INVALID)
    __m128 result_ltgt = _mm_min_ps(x_ord, y_ord); // x_ord < y_ord ? x_ord : y_ord
    __m128 eq = _mm_cmpeq_ps(x, y); // non-signaling compare
    __m128 result_eq = _mm_and_ps(eq, x);
    __m128 result_ord = _mm_or_ps(result_eq, result_ltgt); // ordered
    __m128 x_nan_mask = _mm_cmpunord_ps(x, x);
    __m128 result_unord = _mm_or_ps(_mm_and_ps(x_nan_mask, y), _mm_andnot_ps(x_nan_mask, x)); // isnan(x) ? y : x
    return _mm_or_ps(result_ord, _mm_andnot_ps(ord, result_unord));
}

This is a SIMD version of the following pseudo-code in C:

x = x * 1.0f; // Convert signaling NaNs to quiet NaNs
y = y * 1.0f; // Convert signaling NaNs to quiet NaNs
float x_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : x;
float y_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : y;
float result_ltgt = /* x86 min */ x_ord < y_ord ? x_ord : y_ord;
float eq = /* bitwise and */ x == y ? x : 0.0f;
float result_ord = bitwise_or(eq, result_ltgt);
float result_unord = isunordered(x, x) ? y : x;
return bitwise_or(result_or, isunordered(x, y) ? result_unord : 0.0f);

For maximumNumber:

__m128 my_fmaximum_num_floatx4(__m128 x, __m128 y)
{
    // Convert (possible) signaling NaN to quiet one
    __m128 one = _mm_set1_ps(1.0f);
    asm volatile("mulps %x1, %x0" : "+x"(x) : "x"(one));
    asm volatile("mulps %x1, %x0" : "+x"(y) : "x"(one));
    __m128 ord = _mm_cmpord_ps(x, y); // non-signaling compare
    __m128 x_ord = _mm_and_ps(x, ord); // convert possible NaN to zero
    __m128 y_ord = _mm_and_ps(y, ord); // convert possible NaN to zero
    __m128 result_ltgt = _mm_max_ps(x_ord, y_ord); // x_ord > y_ord ? x_ord : y_ord
    __m128 neq = _mm_cmpneq_ps(x, y); // non-signaling (LT || GT || UNORD)
    __m128 result_ord = _mm_and_ps(_mm_or_ps(neq, x), result_ltgt);
    __m128 x_nan_mask = _mm_cmpunord_ps(x, x);
    __m128 result_unord = _mm_or_ps(_mm_and_ps(x_nan_mask, y), _mm_andnot_ps(x_nan_mask, x)); // isnan(x) ? y : x
    return _mm_or_ps(result_ord, _mm_andnot_ps(ord, result_unord));
}

This is a SIMD version of the following pseudo-code in C:

x = x * 1.0f; // Convert signaling NaNs to quiet NaNs
y = y * 1.0f; // Convert signaling NaNs to quiet NaNs
float x_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : x;
float y_ord = /* bitwise and */ isunordered(x, y) ? 0.0f : y;
float result_ltgt = /* x86 min */ x_ord < y_ord ? x_ord : y_ord;
float mask = /* bitwise or */ x == y ? x : bitcast(~0)
float result_ord = bitwise_and(mask, result_ltgt);
float result_unord = isunordered(x, x) ? y : x;
return bitwise_or(result_or, isunordered(x, y) ? result_unord : 0.0f);

SIMD Version: x86 AVX

We can also branch based on "whether the input contains any NaNs".

__m256 my_fminimum_num_floatx8(__m256 xx, __m256 yy)
{
    __m256 unord = _mm256_cmp_ps(xx, yy, 3); // non-signaling UNORD compare
    if (!_mm256_testz_ps(unord, unord)) {
        // Convert (possible) signaling NaN to quiet one
        __m256 one = _mm256_set1_ps(1.0f);
        asm volatile("vmulps %t1, %t0, %t0" : "+x"(xx) : "x"(one));
        asm volatile("vmulps %t1, %t0, %t0" : "+x"(yy) : "x"(one));
        __m256 x_ord = _mm256_andnot_ps(unord, xx); // convert possible NaN to zero (avoid undue INVALID)
        __m256 y_ord = _mm256_andnot_ps(unord, yy); // convert possible NaN to zero (avoid undue INVALID)
        __m256 result_ltgt = _mm256_min_ps(x_ord, y_ord); // x_ord < y_ord ? x_ord : y_ord
        __m256 eq = _mm256_cmp_ps(xx, yy, 0); // non-signaling EQ compare
        __m256 result_eq = _mm256_and_ps(eq, xx);
        __m256 result_ord = _mm256_or_ps(result_eq, result_ltgt); // ordered
        __m256 x_nan_mask = _mm256_cmp_ps(xx, xx, 3); // non-signaling UNORD
        __m256 result_unord = _mm256_blendv_ps(xx, yy, x_nan_mask); // isnan(x) ? y : x
        return _mm256_blendv_ps(result_ord, result_unord, unord);
    } else {
        // No NaN in input
        __m256 eq = _mm256_cmp_ps(xx, yy, 0); // EQ, non-signaling
        __m256 zz_ltgt = _mm256_min_ps(xx, yy);
        __m256 zz_eq = _mm256_and_ps(eq, xx);
        return _mm256_or_ps(zz_eq, zz_ltgt);
    }
}

This is about the extent of the improvements possible with AVX.

SIMD Version: x86 AVX-512

vrange{s,p}{s,d} behaves the same as IEEE 754-2019 minimumNumber, except for its handling of signaling NaNs in the input. Therefore, if we convert signaling NaNs in the input to quiet NaNs beforehand, we can use vrange{s,p}{s,d}.

__m512 my_fminimum_num_floatx16(__m512 xx, __m512 yy)
{
    // Convert (possible) signaling NaN to quiet one
    __m512 one = _mm512_set1_ps(1.0f);
    asm volatile("vmulps %g1, %g0, %g0" : "+v"(xx) : "v"(one));
    asm volatile("vmulps %g1, %g0, %g0" : "+v"(yy) : "v"(one));
    return _mm512_range_ps(xx, yy, 4); // NaN is missing data
}

x86 AVX10.2

As I mentioned in "New Features of AVX10.2", instructions corresponding to the IEEE 754-2019 minimumNumber operation will be added in AVX10.2. That's good news.

AArch64

AArch64 has the fminnm instruction, which complies with IEEE 754-2008 minNum and treats -0 < +0. By converting signaling NaNs in the input to quiet NaNs beforehand, we can achieve our objective.

float my_fminimum_numf(float x, float y)
{
    float result;
    x = canonicalize_float(x);
    y = canonicalize_float(y);
    asm("fminnm %s0, %s1, %s2" : "=w"(result) : "w"(x), "w"(y));
    return result;
}

The SIMD version is as follows:

float32x4_t my_fminimum_num_floatx4(float32x4_t x, float32x4_t y)
{
    // Convert (possible) signaling NaN to quiet one
    float32x4_t one = vdupq_n_f32(1.0f);
    asm volatile("fmul.4s %0, %0, %1" : "+w"(x) : "w"(one));
    asm volatile("fmul.4s %0, %0, %1" : "+w"(y) : "w"(one));
    // x = vmulq_f32(x, one);
    // y = vmulq_f32(y, one);
    return vminnmq_f32(x, y);
}

Conclusion

I have implemented minimum/maximum/minimumNumber/maximumNumber from IEEE 754-2019. Please refer to this if you want to implement a C23-compliant libc or if you need well-behaved min/max in your SIMD code.

[Update] I have written "Thinking about the Relationship Between IEEE 754-2019 min/max Instructions and x86 min/max Instructions" as a topic that could be useful for compiler optimization.

Discussion