This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
forked from hashcat/hashcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitops.c
81 lines (73 loc) · 1.61 KB
/
bitops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "bitops.h"
u32 rotl32 (const u32 a, const int n)
{
#if defined (_MSC_VER)
return _rotl (a, n);
#else
return ((a << n) | (a >> (32 - n)));
#endif
}
u32 rotr32 (const u32 a, const int n)
{
#if defined (_MSC_VER)
return _rotr (a, n);
#else
return ((a >> n) | (a << (32 - n)));
#endif
}
u64 rotl64 (const u64 a, const int n)
{
#if defined (_MSC_VER)
return _rotl64 (a, n);
#else
return ((a << n) | (a >> (64 - n)));
#endif
}
u64 rotr64 (const u64 a, const int n)
{
#if defined (_MSC_VER)
return _rotr64 (a, n);
#else
return ((a >> n) | (a << (64 - n)));
#endif
}
u16 byte_swap_16 (const u16 n)
{
return (u16) ((n >> 8) | (n << 8));
}
u32 byte_swap_32 (const u32 n)
{
#if defined (_MSC_VER)
return _byteswap_ulong (n);
#elif defined (__clang__) || defined (__GNUC__)
return __builtin_bswap32 (n);
#else
return (n & 0xff000000) >> 24
| (n & 0x00ff0000) >> 8
| (n & 0x0000ff00) << 8
| (n & 0x000000ff) << 24;
#endif
}
u64 byte_swap_64 (const u64 n)
{
#if defined (_MSC_VER)
return _byteswap_uint64 (n);
#elif defined (__clang__) || defined (__GNUC__)
return __builtin_bswap64 (n);
#else
return (n & 0xff00000000000000ULL) >> 56
| (n & 0x00ff000000000000ULL) >> 40
| (n & 0x0000ff0000000000ULL) >> 24
| (n & 0x000000ff00000000ULL) >> 8
| (n & 0x00000000ff000000ULL) << 8
| (n & 0x0000000000ff0000ULL) << 24
| (n & 0x000000000000ff00ULL) << 40
| (n & 0x00000000000000ffULL) << 56;
#endif
}