xref: /reactos/sdk/lib/crt/stdlib/rot.c (revision 2b933529)
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 #if defined (__clang__) && !defined(_MSC_VER)
23 #define ASM_ALIAS __asm__
24 #else
25 #define ASM_ALIAS(x)
26 #endif
27 
28 unsigned int _rotr( unsigned int value, int shift ) ASM_ALIAS("__rotr");
29 unsigned long _lrotr(unsigned long value, int shift) ASM_ALIAS("__lrotr");
30 unsigned int _rotl( unsigned int value, int shift ) ASM_ALIAS("__rotl");
31 unsigned long _lrotl( unsigned long value, int shift ) ASM_ALIAS("__lrotl");
32 
33 /*
34  * @implemented
35  */
36 unsigned int _rotl( unsigned int value, int shift )
37 {
38 	int max_bits = sizeof(unsigned int)<<3;
39 	if ( shift < 0 )
40 		return _rotr(value,-shift);
41 
42 	if ( shift > max_bits )
43 		shift = shift % max_bits;
44 	return (value << shift) | (value >> (max_bits-shift));
45 }
46 
47 /*
48  * @implemented
49  */
50 unsigned int _rotr( unsigned int value, int shift )
51 {
52 	int max_bits = sizeof(unsigned int)<<3;
53 	if ( shift < 0 )
54 		return _rotl(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 _lrotl( unsigned long value, int shift )
65 {
66 	int max_bits = sizeof(unsigned long)<<3;
67 	if ( shift < 0 )
68 		return _lrotr(value,-shift);
69 
70 	if ( shift > max_bits )
71 		shift = shift % max_bits;
72 	return (value << shift) | (value >> (max_bits-shift));
73 }
74 
75 /*
76  * @implemented
77  */
78 unsigned long _lrotr( unsigned long value, int shift )
79 {
80 	int max_bits = sizeof(unsigned long)<<3;
81 	if ( shift < 0 )
82 		return _lrotl(value,-shift);
83 
84 	if ( shift > max_bits )
85 		shift = shift % max_bits;
86 	return (value >> shift) | (value << (max_bits-shift));
87 }
88