[libc-commits] [PATCH] D143732: [libc] Fix exp2f and prevent misuse of likely/unlikely

Guillaume Chatelet via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Feb 10 06:02:06 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4a96893fc5ca: [libc] Fix exp2f and prevent misuse of likely/unlikely (authored by gchatelet).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D143732

Files:
  libc/src/__support/macros/attributes.h
  libc/src/math/generic/exp2f.cpp


Index: libc/src/math/generic/exp2f.cpp
===================================================================
--- libc/src/math/generic/exp2f.cpp
+++ libc/src/math/generic/exp2f.cpp
@@ -69,7 +69,7 @@
   }
 
   // Check exceptional values.
-  if (LIBC_UNLIKELY(x_u & exval_mask) == exval_mask) {
+  if (LIBC_UNLIKELY((x_u & exval_mask) == exval_mask)) {
     if (LIBC_UNLIKELY(x_u == exval1)) { // x = 0x1.853a6ep-9f
       if (fputil::get_round() == FE_TONEAREST)
         return 0x1.00870ap+0f;
Index: libc/src/__support/macros/attributes.h
===================================================================
--- libc/src/__support/macros/attributes.h
+++ libc/src/__support/macros/attributes.h
@@ -19,8 +19,18 @@
 
 #define LIBC_INLINE inline
 #define LIBC_INLINE_ASM __asm__ __volatile__
-#define LIBC_LIKELY(x) __builtin_expect(!!(x), 1)
-#define LIBC_UNLIKELY(x) __builtin_expect(x, 0)
+
+// We use a template to implement likely/unlikely to make sure that we don't
+// accidentally pass an integer.
+namespace __llvm_libc::details {
+template <typename T>
+constexpr LIBC_INLINE bool expects_bool_condition(T value, T expected) {
+  return __builtin_expect(value, expected);
+}
+} // namespace __llvm_libc::details
+#define LIBC_LIKELY(x) __llvm_libc::details::expects_bool_condition(x, true)
+#define LIBC_UNLIKELY(x) __llvm_libc::details::expects_bool_condition(x, false)
+
 #define LIBC_UNUSED __attribute__((unused))
 
 #endif // LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143732.496449.patch
Type: text/x-patch
Size: 1475 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230210/2283d5f2/attachment.bin>


More information about the libc-commits mailing list