chibipub

experimental activitypub node in C
git clone git://jb55.com/chibipub
Log | Files | Refs | README | LICENSE

rotate-bits.h (1017B)


      1 
      2 
      3 #ifndef __ROTATE_DEFS_H
      4 #define __ROTATE_DEFS_H
      5 
      6 #ifdef _MSC_VER
      7 
      8 #include <stdlib.h>
      9 
     10 #define ROTL32(v, n) _rotl((v), (n))
     11 #define ROTL64(v, n) _rotl64((v), (n))
     12 
     13 #define ROTR32(v, n) _rotr((v), (n))
     14 #define ROTR64(v, n) _rotr64((v), (n))
     15 
     16 #else
     17 
     18 #include <stdint.h>
     19 
     20 #define U8V(v) ((uint8_t)(v) & 0xFFU)
     21 #define U16V(v) ((uint16_t)(v) & 0xFFFFU)
     22 #define U32V(v) ((uint32_t)(v) & 0xFFFFFFFFU)
     23 #define U64V(v) ((uint64_t)(v) & 0xFFFFFFFFFFFFFFFFU)
     24 
     25 #define ROTL32(v, n) \
     26   (U32V((uint32_t)(v) << (n)) | ((uint32_t)(v) >> (32 - (n))))
     27 
     28 // tests fail if we don't have this cast...
     29 #define ROTL64(v, n) \
     30   (U64V((uint64_t)(v) << (n)) | ((uint64_t)(v) >> (64 - (n))))
     31 
     32 #define ROTR32(v, n) ROTL32(v, 32 - (n))
     33 #define ROTR64(v, n) ROTL64(v, 64 - (n))
     34 
     35 #endif
     36 
     37 #define ROTL8(v, n) \
     38   (U8V((uint8_t)(v) << (n)) | ((uint8_t)(v) >> (8 - (n))))
     39 
     40 #define ROTL16(v, n) \
     41   (U16V((uint16_t)(v) << (n)) | ((uint16_t)(v) >> (16 - (n))))
     42 
     43 #define ROTR8(v, n) ROTL8(v, 8 - (n))
     44 #define ROTR16(v, n) ROTL16(v, 16 - (n))
     45 
     46 #endif