[flang-commits] [clang] [flang] [llvm] [C++23] [CLANG] Adding C++23 constexpr math functions: fmin, fmax and frexp. (PR #88978)

Hubert Tong via flang-commits flang-commits at lists.llvm.org
Mon Jul 29 19:57:27 PDT 2024


================
@@ -0,0 +1,106 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 4
+
+// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -std=c++23 \
+// RUN: -emit-llvm -o - %s | FileCheck %s
+
+#define NAN (__builtin_nanf(""))
+#define INFINITY (__builtin_inff())
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z4funcv(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    [[MIN1:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MIN2:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MIN3:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MIN4:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MIN5:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MIN6:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MIN7:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MIN8:%.*]] = alloca x86_fp80, align 16
+// CHECK-NEXT:    [[MAX1:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MAX2:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MAX3:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[MAX4:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MAX5:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MAX6:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MAX7:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[MAX8:%.*]] = alloca x86_fp80, align 16
+// CHECK-NEXT:    [[FREXP1:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[FREXP2:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[FREXP3:%.*]] = alloca double, align 8
+// CHECK-NEXT:    [[FREXP4:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[FREXP5:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[FREXP6:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[FREXP7:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[FREXP8:%.*]] = alloca x86_fp80, align 16
+// CHECK-NEXT:    [[FREXP9:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[FREXP10:%.*]] = alloca x86_fp80, align 16
+// CHECK-NEXT:    store i32 0, ptr [[I]], align 4
+// CHECK-NEXT:    store double 1.300000e+00, ptr [[MIN1]], align 8
+// CHECK-NEXT:    store double -0.000000e+00, ptr [[MIN2]], align 8
+// CHECK-NEXT:    store double -0.000000e+00, ptr [[MIN3]], align 8
+// CHECK-NEXT:    store float 0x7FF8000000000000, ptr [[MIN4]], align 4
+// CHECK-NEXT:    store float -1.000000e+00, ptr [[MIN5]], align 4
+// CHECK-NEXT:    store float 0xFFF0000000000000, ptr [[MIN6]], align 4
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[MIN7]], align 4
+// CHECK-NEXT:    store x86_fp80 0xK4005F6E978D4FDF3B646, ptr [[MIN8]], align 16
+// CHECK-NEXT:    store double 1.524000e+01, ptr [[MAX1]], align 8
+// CHECK-NEXT:    store double 0.000000e+00, ptr [[MAX2]], align 8
+// CHECK-NEXT:    store double 0.000000e+00, ptr [[MAX3]], align 8
+// CHECK-NEXT:    store float -1.000000e+00, ptr [[MAX4]], align 4
+// CHECK-NEXT:    store float 0x7FF0000000000000, ptr [[MAX5]], align 4
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[MAX6]], align 4
+// CHECK-NEXT:    store float 0x7FF8000000000000, ptr [[MAX7]], align 4
+// CHECK-NEXT:    store x86_fp80 0xK4008C540C49BA5E353F8, ptr [[MAX8]], align 16
+// CHECK-NEXT:    store double 0x3FEEDCCCCCCCCCCD, ptr [[FREXP1]], align 8
+// CHECK-NEXT:    store double 0.000000e+00, ptr [[FREXP2]], align 8
+// CHECK-NEXT:    store double -0.000000e+00, ptr [[FREXP3]], align 8
+// CHECK-NEXT:    store float 0x7FF8000000000000, ptr [[FREXP4]], align 4
+// CHECK-NEXT:    store float 0xFFF8000000000000, ptr [[FREXP5]], align 4
+// CHECK-NEXT:    store float 0x7FF0000000000000, ptr [[FREXP6]], align 4
+// CHECK-NEXT:    store float 0xFFF0000000000000, ptr [[FREXP7]], align 4
+// CHECK-NEXT:    store x86_fp80 0xK3FFE81A9FBE76C8B4396, ptr [[FREXP8]], align 16
+// CHECK-NEXT:    store float 8.750000e-01, ptr [[FREXP9]], align 4
+// CHECK-NEXT:    store x86_fp80 0xK3FFEEAC7AE147AE14800, ptr [[FREXP10]], align 16
+// CHECK-NEXT:    ret i32 0
+//
+int func()
+{
+  int i;
+
+  // fmin
+  constexpr double min1 = __builtin_fmin(15.24, 1.3);
+  constexpr double min2 = __builtin_fmin(-0.0, +0.0);
+  constexpr double min3 = __builtin_fmin(+0.0, -0.0);
+  constexpr float min4 = __builtin_fminf(NAN, NAN);
+  constexpr float min5 = __builtin_fminf(NAN, -1);
+  constexpr float min6 = __builtin_fminf(-INFINITY, 0);
+  constexpr float min7 = __builtin_fminf(INFINITY, 0);
+  constexpr long double min8 = __builtin_fminl(123.456L, 789.012L);
+
+  // fmax
+  constexpr double max1 =  __builtin_fmax(15.24, 1.3);
+  constexpr double max2 = __builtin_fmax(-0.0, +0.0);
+  constexpr double max3 = __builtin_fmax(+0.0, -0.0);
+  constexpr float max4 = __builtin_fmaxf(NAN, -1);
+  constexpr float max5 = __builtin_fmaxf(+INFINITY, 0);
+  constexpr float max6 = __builtin_fmaxf(-INFINITY, 0);
+  constexpr float max7 = __builtin_fmaxf(NAN, NAN);
+  constexpr long double max8 = __builtin_fmaxl(123.456L, 789.012L);
+
+  // frexp
+  constexpr double frexp1 = __builtin_frexp(123.45, &i);
+  constexpr double frexp2 = __builtin_frexp(0.0, &i);
+  constexpr double frexp3 = __builtin_frexp(-0.0, &i);
+  constexpr float frexp4 = __builtin_frexpf(NAN, &i);
+  constexpr float frexp5 = __builtin_frexpf(-NAN, &i);
+  constexpr float frexp6 = __builtin_frexpf(+INFINITY, &i);
+  constexpr float frexp7 = __builtin_frexpf(-INFINITY, &i);
+  constexpr long double frexp8 = __builtin_frexpl(259.328L, &i);
+  constexpr float frexp9 = __builtin_frexpf16(3.5, &i);
+  constexpr long double frexp10 = __builtin_frexpf128(234.78, &i);
----------------
hubert-reinterpretcast wrote:

```suggestion
  constexpr __fp16 frexp9 = __builtin_frexpf16(3.5, &i);
  constexpr __float128 frexp10 = __builtin_frexpf128(234.78, &i);
```

https://github.com/llvm/llvm-project/pull/88978


More information about the flang-commits mailing list