aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2020-07-09 13:51:30 +0100
committerMichael Brown <mcb30@ipxe.org>2020-07-09 13:52:31 +0100
commitf7ddda435cf0d116098351986f89517e3d88af13 (patch)
treee40efe1007703bb3f1dedf79229f166b8cb5e714
parent98d49e460a27f74212aab2d6b5eef1143c7a8e37 (diff)
downloadipxe-f7ddda435cf0d116098351986f89517e3d88af13.zip
ipxe-f7ddda435cf0d116098351986f89517e3d88af13.tar.gz
ipxe-f7ddda435cf0d116098351986f89517e3d88af13.tar.bz2
[libc] Add bit-rotation functions for unsigned long values
Generalise the bit rotation implementations to use a common macro, and add roll() and rorl() to handle unsigned long values. Each function will still compile down to a single instruction. Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/include/ipxe/rotate.h34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/include/ipxe/rotate.h b/src/include/ipxe/rotate.h
index b5693e3..4dea09a 100644
--- a/src/include/ipxe/rotate.h
+++ b/src/include/ipxe/rotate.h
@@ -10,44 +10,62 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
+#define ROLx( data, rotation ) \
+ ( ( (data) << (rotation) ) | \
+ ( (data) >> ( ( 8 * sizeof (data) ) - (rotation) ) ) );
+
+#define RORx( data, rotation ) \
+ ( ( (data) >> (rotation) ) | \
+ ( (data) << ( ( 8 * sizeof (data) ) - (rotation) ) ) );
+
static inline __attribute__ (( always_inline )) uint8_t
rol8 ( uint8_t data, unsigned int rotation ) {
- return ( ( data << rotation ) | ( data >> ( 8 - rotation ) ) );
+ return ROLx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint8_t
ror8 ( uint8_t data, unsigned int rotation ) {
- return ( ( data >> rotation ) | ( data << ( 8 - rotation ) ) );
+ return RORx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint16_t
rol16 ( uint16_t data, unsigned int rotation ) {
- return ( ( data << rotation ) | ( data >> ( 16 - rotation ) ) );
+ return ROLx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint16_t
ror16 ( uint16_t data, unsigned int rotation ) {
- return ( ( data >> rotation ) | ( data << ( 16 - rotation ) ) );
+ return RORx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint32_t
rol32 ( uint32_t data, unsigned int rotation ) {
- return ( ( data << rotation ) | ( data >> ( 32 - rotation ) ) );
+ return ROLx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint32_t
ror32 ( uint32_t data, unsigned int rotation ) {
- return ( ( data >> rotation ) | ( data << ( 32 - rotation ) ) );
+ return RORx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint64_t
rol64 ( uint64_t data, unsigned int rotation ) {
- return ( ( data << rotation ) | ( data >> ( 64 - rotation ) ) );
+ return ROLx ( data, rotation );
}
static inline __attribute__ (( always_inline )) uint64_t
ror64 ( uint64_t data, unsigned int rotation ) {
- return ( ( data >> rotation ) | ( data << ( 64 - rotation ) ) );
+ return RORx ( data, rotation );
+}
+
+static inline __attribute__ (( always_inline )) unsigned long
+roll ( unsigned long data, unsigned int rotation ) {
+ return ROLx ( data, rotation );
+}
+
+static inline __attribute__ (( always_inline )) unsigned long
+rorl ( unsigned long data, unsigned int rotation ) {
+ return RORx ( data, rotation );
}
#endif /* _IPXE_ROTATE_H */