xref: /reactos/sdk/lib/crt/stdlib/rot.c (revision 5100859e)
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 _MSC_VER
14 #pragma function(_rotr, _rotl, _rotr, _lrotl, _lrotr)
15 #endif
16 
17 unsigned int _rotr( unsigned int value, int shift );
18 /*
19  * @implemented
20  */
21 unsigned int _rotl( unsigned int value, int shift )
22 {
23 	int max_bits = sizeof(unsigned int)<<3;
24 	if ( shift < 0 )
25 		return _rotr(value,-shift);
26 
27 	if ( shift > max_bits )
28 		shift = shift % max_bits;
29 	return (value << shift) | (value >> (max_bits-shift));
30 }
31 
32 /*
33  * @implemented
34  */
35 unsigned int _rotr( unsigned int value, int shift )
36 {
37 	int max_bits = sizeof(unsigned int)<<3;
38 	if ( shift < 0 )
39 		return _rotl(value,-shift);
40 
41 	if ( shift > max_bits )
42 		shift = shift % max_bits;
43 	return (value >> shift) | (value <<  (max_bits-shift));
44 }
45 
46 
47 /*
48  * @implemented
49  */
50 unsigned long _lrotl( unsigned long value, int shift )
51 {
52 	int max_bits = sizeof(unsigned long)<<3;
53 	if ( shift < 0 )
54 		return _lrotr(value,-shift);
55 
56 	if ( shift > max_bits )
57 		shift = shift % max_bits;
58 	return (value << shift) | (value >> (max_bits-shift));
59 }
60 
61 /*
62  * @implemented
63  */
64 unsigned long _lrotr( unsigned long value, int shift )
65 {
66 	int max_bits = sizeof(unsigned long)<<3;
67 	if ( shift < 0 )
68 		return _lrotl(value,-shift);
69 
70 	if ( shift > max_bits )
71 		shift = shift % max_bits;
72 	return (value >> shift) | (value << (max_bits-shift));
73 }
74