xref: /reactos/sdk/lib/crt/stdlib/rot.c (revision 1734f297)
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 #  ifdef _M_IX86
24 unsigned int _rotr( unsigned int value, int shift ) __asm__("__rotr");
25 unsigned long _lrotr(unsigned long value, int shift) __asm__("__lrotr");
26 unsigned int _rotl( unsigned int value, int shift ) __asm__("__rotl");
27 unsigned long _lrotl( unsigned long value, int shift ) __asm__("__lrotl");
28 #  else
29 unsigned int _rotr( unsigned int value, int shift ) __asm__("_rotr");
30 unsigned long _lrotr(unsigned long value, int shift) __asm__("_lrotr");
31 unsigned int _rotl( unsigned int value, int shift ) __asm__("_rotl");
32 unsigned long _lrotl( unsigned long value, int shift ) __asm__("_lrotl");
33 #  endif
34 #else
35 unsigned int _rotr( unsigned int value, int shift );
36 unsigned long _lrotr(unsigned long value, int shift);
37 unsigned int _rotl( unsigned int value, int shift );
38 unsigned long _lrotl( unsigned long value, int shift );
39 #endif
40 /*
41  * @implemented
42  */
43 unsigned int _rotl( unsigned int value, int shift )
44 {
45 	int max_bits = sizeof(unsigned int)<<3;
46 	if ( shift < 0 )
47 		return _rotr(value,-shift);
48 
49 	if ( shift > max_bits )
50 		shift = shift % max_bits;
51 	return (value << shift) | (value >> (max_bits-shift));
52 }
53 
54 /*
55  * @implemented
56  */
57 unsigned int _rotr( unsigned int value, int shift )
58 {
59 	int max_bits = sizeof(unsigned int)<<3;
60 	if ( shift < 0 )
61 		return _rotl(value,-shift);
62 
63 	if ( shift > max_bits )
64 		shift = shift % max_bits;
65 	return (value >> shift) | (value <<  (max_bits-shift));
66 }
67 
68 /*
69  * @implemented
70  */
71 unsigned long _lrotl( unsigned long value, int shift )
72 {
73 	int max_bits = sizeof(unsigned long)<<3;
74 	if ( shift < 0 )
75 		return _lrotr(value,-shift);
76 
77 	if ( shift > max_bits )
78 		shift = shift % max_bits;
79 	return (value << shift) | (value >> (max_bits-shift));
80 }
81 
82 /*
83  * @implemented
84  */
85 unsigned long _lrotr( unsigned long value, int shift )
86 {
87 	int max_bits = sizeof(unsigned long)<<3;
88 	if ( shift < 0 )
89 		return _lrotl(value,-shift);
90 
91 	if ( shift > max_bits )
92 		shift = shift % max_bits;
93 	return (value >> shift) | (value << (max_bits-shift));
94 }
95