[PATCH] D146436: [clang][Interp][NFC] Add tests for __fp16

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 23 02:09:06 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG0691bcb18024: [clang][Interp][NFC] Add tests for __fp16 (authored by tbaeder).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146436/new/

https://reviews.llvm.org/D146436

Files:
  clang/test/AST/Interp/floats.cpp


Index: clang/test/AST/Interp/floats.cpp
===================================================================
--- clang/test/AST/Interp/floats.cpp
+++ clang/test/AST/Interp/floats.cpp
@@ -78,3 +78,94 @@
   }
   static_assert(f2() == __FLT_MAX__, "");
 }
+
+
+namespace FP16 {
+  constexpr int i = 2;
+  constexpr __fp16 f = 1.0f;
+  static_assert(f == 1.0f, "");
+
+  constexpr __fp16 f2 = 1u * f;
+  static_assert(f2 == 1.0f, "");
+
+  constexpr __fp16 f3 = 1.5;
+  constexpr int i3 = f3;
+  static_assert(i3 == 1, "");
+
+  constexpr bool b3 = f3;
+  static_assert(b3, "");
+
+
+  static_assert(1.0f16 + 3u == 4, "");
+  static_assert(4.0f16 / 1.0f16 == 4, "");
+  static_assert(10.0f16 * false == 0, "");
+
+  constexpr __fp16 __fp16s[] = {1.0f16, 2.0f16, 3.0f16, 4.0f16};
+
+  constexpr __fp16 m = 5.0f16 / 0.0f16; // ref-error {{must be initialized by a constant expression}} \
+                                   // ref-note {{division by zero}} \
+                                   // expected-error {{must be initialized by a constant expression}} \
+                                   // expected-note {{division by zero}}
+
+  static_assert(~2.0f16 == 3, ""); // ref-error {{invalid argument type '_Float16' to unary expression}} \
+                                 // expected-error {{invalid argument type '_Float16' to unary expression}}
+
+  /// Initialized by a double.
+  constexpr __fp16 df = 0.0;
+  /// The other way around.
+  constexpr double fd = 0.0f16;
+
+  static_assert(0.0f == -0.0f, "");
+
+  const int k = 3 * (1.0f16 / 3.0f16);
+  static_assert(k == 1, "");
+
+  constexpr bool b = 1.0f16;
+  static_assert(b, "");
+
+  constexpr double db = true;
+  static_assert(db == 1.0f16, "");
+
+  constexpr __fp16 fa[] = {1.0f, 2.0, 1, false};
+  constexpr double da[] = {1.0f, 2.0, 1, false};
+
+  constexpr __fp16 fm = __FLT16_MAX__;
+  constexpr int someInt = fm;
+
+  constexpr float SomeFloat = __FLT_MAX__;
+  constexpr __fp16 halfFloat = SomeFloat;
+
+  constexpr float fp16ptr() {
+    __fp16 f1 = 1.0f16;
+    __fp16 *f2 = &f1;
+
+    *f2 = 3.0;
+    return f1;
+  }
+  static_assert(fp16ptr() == 3.0, "");
+
+  namespace compound {
+    constexpr float f1() {
+      __fp16 f = 0;
+      f += 3.0;
+      f -= 3.0f;
+
+      f += 1;
+      f /= 1;
+      f /= 1.0;
+      f *= f;
+
+      f *= 2.0;
+      return f;
+    }
+    static_assert(f1() == 2, "");
+
+    constexpr float f2() {
+      __fp16 f = __FLT16_MAX__;
+      f += 1.0;
+      return f;
+    }
+    static_assert(f2() == __FLT16_MAX__, "");
+  }
+
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146436.507655.patch
Type: text/x-patch
Size: 2550 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230323/c4791dde/attachment.bin>


More information about the cfe-commits mailing list