commit 6a1d4d7e95abd70342dd8614076b1b951576020c
parent a667b4da84d777755adbcecbdc2e88cd75395c79
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Wed, 18 Nov 2026 20:34:27 +0100
mhd_predict.h: improved compatibility and performance
Diffstat:
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/src/mhd2/mhd_predict.h b/src/mhd2/mhd_predict.h
@@ -66,7 +66,7 @@
* @def MHD_NO_PREDICT_COND
*
* When defined, disables all prediction hints regardless of compiler
- * support, turning the macros into plain boolean expressions.
+ * support, turning the macros into plain conditions.
*
* This can be useful for benchmarking or debugging to assess the impact of
* branch prediction hints on a given workload and compiler.
@@ -102,7 +102,10 @@
* Hint that @p cond is expected to be true with probability @p prob.
*
* Expands to compiler-specific intrinsics when available; otherwise to
- * a boolean normalisation of @p cond.
+ * @p cond.
+ *
+ * The result must be used only as a condition: its value is not always
+ * normalised to 0 or 1.
*
* @param cond Boolean-like expression; evaluated exactly once.
* @param prob Anticipated probability in the range (0.5, 1.0].
@@ -119,7 +122,10 @@
* Hint that @p cond is expected to be false with probability @p prob.
*
* Expands to compiler-specific intrinsics when available; otherwise to
- * a boolean normalisation of @p cond.
+ * @p cond.
+ *
+ * The result must be used only as a condition: its value is not always
+ * normalised to 0 or 1.
*
* @param cond Boolean-like expression; evaluated exactly once.
* @param prob Anticipated probability in the range (0.5, 1.0].
@@ -142,8 +148,13 @@
# define mhd_COND_PRED_FALSE_P(cond, prob) \
(((prob) < 0.85) ? (! ! (cond)) : (__builtin_expect (! ! (cond), 0)))
#else
-# define mhd_COND_PRED_TRUE_P(cond, prob) (! ! (cond))
-# define mhd_COND_PRED_FALSE_P(cond, prob) (! ! (cond))
+/* No hint support: use the condition directly.
+ Note: the condition is not normalised with "! !" here, as some compilers
+ (namely MSVC) computes the 0/1 value of a compound condition (with "&&")
+ and tests that value again instead of using the comparison results
+ directly, which adds instructions and code size. */
+# define mhd_COND_PRED_TRUE_P(cond, prob) (cond)
+# define mhd_COND_PRED_FALSE_P(cond, prob) (cond)
#endif
/*