]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add 64-bit rotate operators
authorWilly Tarreau <w@1wt.eu>
Sat, 7 Mar 2020 23:41:00 +0000 (00:41 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 7 Mar 2020 23:42:18 +0000 (00:42 +0100)
This adds rotl64/rotr64 to rotate a 64-bit word by an arbitrary number
of bits. It's mainly aimed at being used with constants.

include/common/standard.h

index a78083318d16110b2cd1a96e1106a37e7104c84f..f9eed961fdaccc041bdc75177647bc2ea962161b 100644 (file)
  * power of 2, and 0 otherwise */
 #define POWEROF2(x) (((x) & ((x)-1)) == 0)
 
+/* rotate left a 64-bit integer by <bits:[0-5]> bits */
+static inline uint64_t rotl64(uint64_t v, uint8_t bits)
+{
+#if !defined(__ARM_ARCH_8A) && !defined(__x86_64__)
+       bits &= 63;
+#endif
+       v = (v << bits) | (v >> (-bits & 63));
+       return v;
+}
+
+/* rotate right a 64-bit integer by <bits:[0-5]> bits */
+static inline uint64_t rotr64(uint64_t v, uint8_t bits)
+{
+#if !defined(__ARM_ARCH_8A) && !defined(__x86_64__)
+       bits &= 63;
+#endif
+       v = (v >> bits) | (v << (-bits & 63));
+       return v;
+}
+
 /* DEFNULL() returns either the argument as-is, or NULL if absent. This is for
  * use in macros arguments.
  */