[master] 7e5213f9f vmin/vmax: Avoid typeof() where we can

Nils Goroll nils.goroll at uplex.de
Wed Nov 10 10:19:07 UTC 2021


commit 7e5213f9f7a35c664bf7486e6310794f74d9e6ea
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Nov 10 10:53:46 2021 +0100

    vmin/vmax: Avoid typeof() where we can
    
    Reviewing flexelint output for the recently added vmin/vmax macros
    made apparent that vmin_t/vmax_t basically used typeof((cast)var),
    which makes for an unnecessary de-tour via typeof.
    
    We change the min/max macros to explicitly pass the type for the
    temporary variables such that typeof() is only needed for vmin/vmax.
    
    Being at it, we also reduce the min/max case to just one take macro
    ("take the min/max of ...") as they only differed in a single
    character, the operator.

diff --git a/include/vdef.h b/include/vdef.h
index a810be196..9b1043669 100644
--- a/include/vdef.h
+++ b/include/vdef.h
@@ -182,29 +182,24 @@ int __llvm_gcov_flush(void);
 
 /* ref: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html */
 
-#define _vmin(a, b, _va, _vb)						\
-({									\
-	typeof (a) _va = (a);						\
-	typeof (b) _vb = (b);						\
+#define _vtake(op, ta, tb, a, b, _va, _vb)				\
+	({								\
+	ta _va = (a);							\
+	tb _vb = (b);							\
 	(void)(&_va == &_vb);						\
-	_va < _vb ? _va : _vb;						\
+	_va op _vb ? _va : _vb;						\
 })
 
-#define _vmax(a, b, _va, _vb)						\
-({									\
-	typeof (a) _va = (a);						\
-	typeof (b) _vb = (b);						\
-	(void)(&_va == &_vb);						\
-	_va > _vb ? _va : _vb;						\
-})
+#define opmin <
+#define opmax >
+#define vtake(n, ta, tb, a, b)	_vtake(op ## n, ta, tb, a, b,		\
+    VUNIQ_NAME(_v ## n ## A), VUNIQ_NAME(_v ## n ## B))
 
-#define vmin(a, b)		_vmin((a), (b), VUNIQ_NAME(_vmina),	\
-    VUNIQ_NAME(_vminb))
-#define vmax(a, b)		_vmax((a), (b), VUNIQ_NAME(_vmaxa),	\
-    VUNIQ_NAME(_vmaxb))
+#define vmin(a, b)		vtake(min, typeof(a), typeof(b), a, b)
+#define vmax(a, b)		vtake(max, typeof(a), typeof(b), a, b)
 
-#define vmin_t(type, a, b)	vmin((type)(a), (type)(b))
-#define vmax_t(type, a, b)	vmax((type)(a), (type)(b))
+#define vmin_t(type, a, b)	vtake(min, type, type, a, b)
+#define vmax_t(type, a, b)	vtake(max, type, type, a, b)
 
 /**********************************************************************
  * Clamp the value between two limits.


More information about the varnish-commit mailing list