comparison lib/bitrotate.h @ 11640:66e1eeffb344

hash: make rotation more obvious * modules/hash (Depends-on): Add bitrotate and stdint. * lib/bitrotate.h (rotl_sz, rotr_sz): New functions. * lib/hash.c (headers): Drop limits.h. Add stdint.h. (SIZE_MAX): Rely on headers for definition. (hash_string) [USE_DIFF_HASH]: Use rotl_sz. (raw_hasher): Use rotr_sz. Suggested by Jim Meyering. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Thu, 18 Jun 2009 13:31:11 -0600
parents b59f0c45c546
children c2cbabec01dd
comparison
equal deleted inserted replaced
11639:b2e769838448 11640:66e1eeffb344
1 /* bitrotate.h - Rotate bits in integers 1 /* bitrotate.h - Rotate bits in integers
2 Copyright (C) 2008 Free Software Foundation, Inc. 2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3 3
4 This program is free software: you can redistribute it and/or modify 4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or 6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version. 7 (at your option) any later version.
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */ 17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
18 18
19 #ifndef _GL_BITROTATE_H 19 #ifndef _GL_BITROTATE_H
20 #define _GL_BITROTATE_H 20 #define _GL_BITROTATE_H
21 21
22 #include <limits.h>
22 #include <stdint.h> 23 #include <stdint.h>
24 #include <sys/types.h>
23 25
24 #ifdef UINT64_MAX 26 #ifdef UINT64_MAX
25 /* Given an unsigned 64-bit argument X, return the value corresponding 27 /* Given an unsigned 64-bit argument X, return the value corresponding
26 to rotating the bits N steps to the left. N must be between 1 and 28 to rotating the bits N steps to the left. N must be between 1 and
27 63 inclusive. */ 29 63 inclusive. */
55 31 inclusive.*/ 57 31 inclusive.*/
56 static inline uint32_t 58 static inline uint32_t
57 rotr32 (uint32_t x, int n) 59 rotr32 (uint32_t x, int n)
58 { 60 {
59 return ((x >> n) | (x << (32 - n))) & UINT32_MAX; 61 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
62 }
63
64 /* Given a size_t argument X, return the value corresponding
65 to rotating the bits N steps to the left. N must be between 1 and
66 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
67 static inline size_t
68 rotl_sz (size_t x, int n)
69 {
70 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
71 }
72
73 /* Given a size_t argument X, return the value corresponding
74 to rotating the bits N steps to the right. N must be between 1 to
75 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
76 static inline size_t
77 rotr_sz (size_t x, int n)
78 {
79 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
60 } 80 }
61 81
62 /* Given an unsigned 16-bit argument X, return the value corresponding 82 /* Given an unsigned 16-bit argument X, return the value corresponding
63 to rotating the bits N steps to the left. N must be between 1 to 83 to rotating the bits N steps to the left. N must be between 1 to
64 15 inclusive, but on most relevant targets N can also be 0 and 16 84 15 inclusive, but on most relevant targets N can also be 0 and 16