master
 1// Wasm's `min` and `max` operators implement the IEEE 754-2019
 2// `minimum` and `maximum` operations, meaning that given a choice
 3// between NaN and a number, they return NaN. This differs from
 4// the C standard library's `fmin` and `fmax` functions, which
 5// return the number. However, we can still use wasm's builtins
 6// by handling the NaN cases explicitly, and it still turns out
 7// to be faster than doing the whole operation in
 8// target-independent C. And, it's smaller.
 9
10#include <math.h>
11
12float fminf(float x, float y) {
13    if (isnan(x)) return y;
14    if (isnan(y)) return x;
15    return __builtin_wasm_min_f32(x, y);
16}
17
18float fmaxf(float x, float y) {
19    if (isnan(x)) return y;
20    if (isnan(y)) return x;
21    return __builtin_wasm_max_f32(x, y);
22}
23
24double fmin(double x, double y) {
25    if (isnan(x)) return y;
26    if (isnan(y)) return x;
27    return __builtin_wasm_min_f64(x, y);
28}
29
30double fmax(double x, double y) {
31    if (isnan(x)) return y;
32    if (isnan(y)) return x;
33    return __builtin_wasm_max_f64(x, y);
34}