-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.asm
More file actions
206 lines (192 loc) · 3.29 KB
/
base.asm
File metadata and controls
206 lines (192 loc) · 3.29 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
format PE64 DLL
include 'win64ax.inc'
section '.edata' export readable
export 'base.dll', \
FastZeroMemory, 'FastZeroMemory', FastSort, 'FastSort', \
RdSeed64, 'RdSeed64', RdSeed32, 'RdSeed32', RdSeed16, 'RdSeed16', \
RandomInt64, 'RandomInt64', RandomInt32, 'RandomInt32', RandomInt16, 'RandomInt16', RandomByte, 'RandomByte', RandomSByte, 'RandomSByte', \
RandomDouble, 'RandomDouble', RandomSingle, 'RandomSingle'
section '.data' data readable
one64 dq 1.0
one32 dd 1.0
_x64value dq 1.8446744073709552e30
_x32value dd 1000000000
section '.text' code readable executable
proc FastZeroMemory
xor cl, cl
mov rdi, rcx
mov rcx, rdx
rep stosb
ret
endp
proc RdSeed64
rdrand rax
ret
endp
proc RdSeed32
rdrand eax
ret
endp
proc RdSeed16
rdrand ax
ret
endp
proc RandomInt64
cmp rdx, rcx
jg .impl
xor rax, rax
ret
.impl:
rdrand rax
mov r8, rcx
mov r9, rdx
sub r9, r8
inc r9
xor rdx, rdx
div r9
add rdx, rcx
mov rax, rdx
ret
endp
proc RandomInt32
cmp edx, ecx
jg .impl
xor eax, eax
ret
.impl:
rdrand eax
mov esi, ecx
mov edi, edx
sub edi, esi
inc edi
xor edx, edx
div edi
add edx, ecx
mov eax, edx
ret
endp
proc RandomInt16
cmp dx, cx
jg .gen
xor ax, ax
ret
.gen:
rdrand ax
test ax, ax
jns .pos
neg ax
.pos:
mov bx, dx
sub bx, cx
inc bx
xor dx, dx
div bx
mov ax, cx
add ax, dx
ret
endp
proc RandomByte
cmp dx, cx
jg .impl
xor ax, ax
ret
.impl:
rdrand ax
movzx eax, ax
mov bx, dx
sub bx, cx
inc bx
xor dx, dx
div bx
mov ax, cx
add ax, dx
ret
endp
proc RandomSByte
cmp dx, cx
jg .impl
xor ax, ax
ret
.impl:
rdrand ax
movsx eax, ax
mov bx, dx
sub bx, cx
inc bx
xor dx, dx
div bx
mov ax, cx
add ax, dx
ret
endp
proc RandomDouble
.begin:
rdrand r15
test r15, r15
cmp r15, 0
js .negate
jmp .impl
.negate:
neg r15
.impl:
cvtsi2sd xmm2, r15
cvtsi2sd xmm3, [_x64value]
divsd xmm2, xmm3
movq rax, xmm2
test rax, rax
cmp rax, [one64]
jge .begin
movsd xmm7, xmm0
movsd xmm6, xmm1
subsd xmm6, xmm7
mulsd xmm2, xmm6
addsd xmm2, xmm7
movsd xmm0, xmm2
ret
endp
proc RandomSingle
.begin:
rdrand esi
test esi, esi
cmp esi, 0
js .negate
jmp .impl
.negate:
neg esi
.impl:
cvtsi2ss xmm2, esi
cvtsi2ss xmm3, [_x32value]
divss xmm2, xmm3
movd eax, xmm2
test eax, eax
cmp eax, [one32]
jge .begin
movss xmm7, xmm0
movss xmm6, xmm1
subss xmm6, xmm7
mulss xmm2, xmm6
addss xmm2, xmm7
movss xmm0, xmm2
ret
endp
FastSort:
cmp rdx, 1
jbe .ret
lea r8, [rdx - 1]
.outer:
xor r9, r9
.inner:
mov r10, [rcx + r9*8]
mov r11, [rcx + r9*8 + 8]
cmp r11, r10
jge .no_swap
mov [rcx + r9*8], r11
mov [rcx + r9*8 + 8], r10
.no_swap:
inc r9
cmp r9, r8
jl .inner
dec r8
jg .outer
.ret:
ret