xref: /reactos/sdk/lib/crt/stdlib/rot.c (revision 37b2c145)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/sdk/crt/stdlib/rot.c
5  * PURPOSE:     Performs a bitwise rotation
6  * PROGRAMER:   Ariadne
7  * UPDATE HISTORY:
8  *              03/04/99: Created
9  */
10 
11 #include <stdlib.h>
12 
13 #ifdef __clang__
14 #define _rotl __function_rotl
15 #define _rotr __function_rotr
16 #define _lrotl __function_lrotl
17 #define _lrotr __function_lrotr
18 #elif defined(_MSC_VER)
19 #pragma function(_rotr, _rotl, _rotr, _lrotl, _lrotr)
20 #endif
21 
22 unsigned int _rotr( unsigned int value, int shift );
23 unsigned long _lrotr(unsigned long value, int shift);
24 
25 /*
26  * @implemented
27  */
28 unsigned int _rotl( unsigned int value, int shift )
29 {
30 	int max_bits = sizeof(unsigned int)<<3;
31 	if ( shift < 0 )
32 		return _rotr(value,-shift);
33 
34 	if ( shift > max_bits )
35 		shift = shift % max_bits;
36 	return (value << shift) | (value >> (max_bits-shift));
37 }
38 
39 /*
40  * @implemented
41  */
42 unsigned int _rotr( unsigned int value, int shift )
43 {
44 	int max_bits = sizeof(unsigned int)<<3;
45 	if ( shift < 0 )
46 		return _rotl(value,-shift);
47 
48 	if ( shift > max_bits )
49 		shift = shift % max_bits;
50 	return (value >> shift) | (value <<  (max_bits-shift));
51 }
52 
53 /*
54  * @implemented
55  */
56 unsigned long _lrotl( unsigned long value, int shift )
57 {
58 	int max_bits = sizeof(unsigned long)<<3;
59 	if ( shift < 0 )
60 		return _lrotr(value,-shift);
61 
62 	if ( shift > max_bits )
63 		shift = shift % max_bits;
64 	return (value << shift) | (value >> (max_bits-shift));
65 }
66 
67 /*
68  * @implemented
69  */
70 unsigned long _lrotr( unsigned long value, int shift )
71 {
72 	int max_bits = sizeof(unsigned long)<<3;
73 	if ( shift < 0 )
74 		return _lrotl(value,-shift);
75 
76 	if ( shift > max_bits )
77 		shift = shift % max_bits;
78 	return (value >> shift) | (value << (max_bits-shift));
79 }
80