add macros for branch-tuning optimization
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 21 Jun 2021 00:40:20 +0000 (17:40 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 26 Jun 2021 17:34:40 +0000 (10:34 -0700)
lib/cgraph/CMakeLists.txt
lib/cgraph/Makefile.am
lib/cgraph/cgraph.vcxproj
lib/cgraph/cgraph.vcxproj.filters
lib/cgraph/likely.h [new file with mode: 0644]

index b1466d2d7e8015723c73d4787698e49d47874501..990f79291f43873adc2ba5202146ccebe820a13e 100644 (file)
@@ -10,6 +10,7 @@ add_library(cgraph SHARED
     cghdr.h
     cgraph.h
     itos.h
+    likely.h
     sprint.h
     strcasecmp.h
     unreachable.h
index 04317540b99cab16c8485fe3d6ff639d8239a11c..f5c78e4297660a6584dbe2385449b45102e04b98 100644 (file)
@@ -8,7 +8,8 @@ pkgconfigdir = $(libdir)/pkgconfig
 AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)/lib/cdt
 
 pkginclude_HEADERS = cgraph.h
-noinst_HEADERS = agxbuf.h cghdr.h itos.h sprint.h strcasecmp.h unreachable.h
+noinst_HEADERS = agxbuf.h cghdr.h itos.h likely.h sprint.h strcasecmp.h \
+       unreachable.h
 noinst_LTLIBRARIES = libcgraph_C.la
 lib_LTLIBRARIES = libcgraph.la
 pkgconfig_DATA = libcgraph.pc
index 03f0d3d3aca643ec9cf833cc85d20c01b4c8b945..789ab9b93fdd290286210f9d8b9c7a0af69f235f 100644 (file)
@@ -102,6 +102,7 @@ win_flex -oscan.c scan.l</Command>
     <ClInclude Include="cghdr.h" />
     <ClInclude Include="cgraph.h" />
     <ClInclude Include="itos.h" />
+    <ClInclude Include="likely.h" />
     <ClInclude Include="sprint.h" />
     <ClInclude Include="strcasecmp.h" />
     <ClInclude Include="unreachable.h" />
index 7d59114125ae01465c7c401ab59004cfb9a6023b..86f2596279033c3f12bd488e274cc63f46099101 100644 (file)
@@ -27,6 +27,9 @@
     <ClInclude Include="itos.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="likely.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
     <ClInclude Include="sprint.h">
       <Filter>Header Files</Filter>
     </ClInclude>
diff --git a/lib/cgraph/likely.h b/lib/cgraph/likely.h
new file mode 100644 (file)
index 0000000..39c1d83
--- /dev/null
@@ -0,0 +1,29 @@
+#pragma once
+
+/** Markers to indicate a branch condition is predictably true or false.
+ *
+ * These hints can be used to signal to the compiler it should bias its
+ * optimization towards a particular branch outcome. For example:
+ *
+ *   p = malloc(20);
+ *   if (UNLIKELY(p == NULL)) {
+ *     // error path
+ *     …
+ *   }
+ *
+ * Despite their names, there are also uses for them on branches that should be
+ * biased a certain way even though they may *not* be likely-taken or
+ * unlikely-taken. For example, optimizing for the case when a user has not
+ * passed extra verbosity flags:
+ *
+ *   if (UNLIKELY(verbose)) {
+ *     …some logging code…
+ *   }
+ */
+#ifdef __GNUC__
+#define LIKELY(expr) __builtin_expect(!!(expr), 1)
+#define UNLIKELY(expr) __builtin_expect((expr), 0)
+#else
+#define LIKELY(expr) (expr)
+#define UNLIKELY(expr) (expr)
+#endif