-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGNUAsmUtility_x64.h
143 lines (116 loc) · 3.69 KB
/
GNUAsmUtility_x64.h
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (C) 2012-2016
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNUASMUTILITY_X64_H_
#define _GNUASMUTILITY_X64_H_
#define GCC_OR_CLANG_COMPILER (defined(__GNUC__) || defined(__clang__))
namespace x64Asm
{
namespace Utilities
{
// use these for modifying variables that are thread sensitive
#if GCC_OR_CLANG_COMPILER
inline unsigned long POPCNT ( long Op1 )
{
unsigned long ReturnValue;
asm volatile ( "popcnt %1, %0\n"
: "=q" (ReturnValue) // output to register a
: "q" (Op1) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
return ReturnValue;
}
inline unsigned long BSR ( long Op1 )
{
unsigned long ReturnValue;
asm volatile ( "bsr %1, %0\n"
: "+q" (ReturnValue) // output to register a
: "q" (Op1) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
return ReturnValue;
}
/////////////////////////////////////////////
// saturating decrement
inline long sdec32 ( long Op1 )
{
asm volatile ( "sub $1, %0\n"
"adc $0, %0\n"
: "+q" (Op1) // output to register a
//: "q" (Op2) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
return Op1;
}
//////////////////////////////////////////////////
// exchange
static inline void Exchange32 ( unsigned long &Op1, unsigned long &Op2 )
{
asm volatile ( "xchg %0, %1\n"
: "+q" (Op1), "+q" (Op2) // output to register a
//: "q" (Op2) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
}
static inline void Exchange32 ( signed long &Op1, signed long &Op2 )
{
asm volatile ( "xchg %0, %1\n"
: "+q" (Op1), "+q" (Op2) // output to register a
//: "q" (Op2) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
}
static inline void Exchange64 ( long long &Op1, long long &Op2 )
{
asm volatile ( "xchg %0, %1\n"
: "+q" (Op1), "+q" (Op2) // output to register a
//: "q" (Op2) // input registers
//: "0" // clobbered register is same as output, so don't specify
);
}
#else
inline unsigned long POPCNT(long Op1)
{
unsigned long ReturnValue;
ReturnValue = _mm_popcnt_u32(Op1);
return ReturnValue;
}
//inline unsigned long BSR(long Op1)
//{
// unsigned long ReturnValue = 0;
// return ReturnValue;
//}
/////////////////////////////////////////////
// saturating decrement
//inline long sdec32(long Op1)
//{
// return Op1;
//}
//////////////////////////////////////////////////
// exchange
static inline void Exchange32(unsigned long& Value, unsigned long& Op2)
{
_InterlockedExchange(&Value, Op2);
}
static inline void Exchange32(signed long& Value, signed long& Op2)
{
_InterlockedExchange(&Value, Op2);
}
static inline void Exchange64(long long& Value , long long& Op2)
{
_InterlockedExchange64(&Value, Op2);
}
#endif
} // end namespace Utilities
} // end namespace x64Asm
#endif