forked from ocaml/Zarith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_z_x86_64_mingw64.S
381 lines (328 loc) · 7.47 KB
/
caml_z_x86_64_mingw64.S
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
Assembly version for the fast path of some functions in Z:
- x86_64 target
- Win64 ABI
- GNU as
This file is part of the Zarith library
http://forge.ocamlcore.org/projects/zarith .
It is distributed under LGPL 2 licensing, with static linking exception.
See the LICENSE file included in the distribution.
Copyright (c) 2010-2011 Antoine Miné, Abstraction project.
Abstraction is part of the LIENS (Laboratoire d'Informatique de l'ENS),
a joint laboratory by:
CNRS (Centre national de la recherche scientifique, France),
ENS (École normale supérieure, Paris, France),
INRIA Rocquencourt (Institut national de recherche en informatique, France).
*/
/* helper functions */
/* **************** */
#define SYMB(x) x
#define FUNCTION_ALIGN 16
#define PROLOG(proc) \
.text; \
.globl SYMB(ml_as_z_##proc); \
.align FUNCTION_ALIGN; \
SYMB(ml_as_z_##proc):\
#define EPILOG(proc)
#define C_JMP(proc) \
jmp SYMB(ml_z_##proc)
/* operation counter */
#ifndef Z_PERF_COUNTER
#define OP
#else
#define OP \
mov SYMB(ml_z_ops_as(%rip)), %rcx; \
addq $1, (%rcx)
#endif
/* unary arithmetics */
/* ***************** */
/* neg */
PROLOG(neg)
test $1, %rcx
jz .Lneg
mov %rcx, %rax
not %rax
add $3, %rax
jo .Lneg
OP
ret
.Lneg:
C_JMP(neg)
EPILOG(neg)
/* abs */
PROLOG(abs)
test $1, %rcx
jz .Labs
mov %rcx, %rax
test %rcx, %rcx
jns .Labs2
not %rax
add $3, %rax
jo .Lneg
.Labs2:
OP
ret
.Labs:
C_JMP(abs)
EPILOG(abs)
/* succ */
PROLOG(succ)
test $1, %rcx
jz .Lsucc
mov %rcx, %rax
add $2, %rax
jo .Lsucc
OP
ret
.Lsucc:
C_JMP(succ)
EPILOG(succ)
/* pred */
PROLOG(pred)
test $1, %rcx
jz .Lpred
mov %rcx, %rax
sub $2, %rax
jo .Lpred
OP
ret
.Lpred:
C_JMP(pred)
EPILOG(pred)
/* binary arithmetics */
/* ****************** */
/* add */
PROLOG(add)
test $1, %rcx
jz .Ladd
test $1, %rdx
jz .Ladd
lea -1(%rcx), %rax
add %rdx, %rax
jo .Ladd
OP
ret
.Ladd:
C_JMP(add)
EPILOG(add)
/* sub */
PROLOG(sub)
test $1, %rcx
jz .Lsub
test $1, %rdx
jz .Lsub
mov %rcx, %rax
sub %rdx, %rax
jo .Lsub
inc %rax
OP
ret
.Lsub:
C_JMP(sub)
EPILOG(sub)
/* mul */
PROLOG(mul)
test $1, %rcx
jz .Lmul
test $1, %rdx
jz .Lmul
lea -1(%rdx), %rax
mov %rcx, %r8
sar %r8
imul %r8, %rax
jo .Lmul
inc %rax
OP
ret
.Lmul:
C_JMP(mul)
EPILOG(mul)
/* div */
PROLOG(div)
test $1, %rcx
jz .Ldiv
test $1, %rdx
jz .Ldiv
mov %rdx, %r8
mov %rcx, %rax
sar %r8
jz .Ldiv /* division by zero */
cmp $-1, %r8
je .Ldivneg
sar %rax
cqo
idiv %r8
sal %rax
inc %rax
OP
ret
.Ldivneg:
/* division by -1, the only one that can overflow */
not %rax
add $3, %rax
jo .Ldiv
OP
ret
.Ldiv:
C_JMP(div)
EPILOG(div)
/* divexact */
PROLOG(divexact)
test $1, %rcx
jz .Ldivexact
test $1, %rdx
jz .Ldivexact
mov %rdx, %r8
mov %rcx, %rax
sar %r8
jz .Ldivexact /* division by zero */
cmp $-1, %r8
je .Ldivexactneg
sar %rax
cqo
idiv %r8
sal %rax
inc %rax
OP
ret
.Ldivexactneg:
/* division by -1, the only one that can overflow */
not %rax
add $3, %rax
jo .Ldivexact
OP
ret
.Ldivexact:
C_JMP(divexact)
EPILOG(divexact)
/* rem */
PROLOG(rem)
test $1, %rcx
jz .Lrem
test $1, %rdx
jz .Lrem
mov %rdx, %r8
mov %rcx, %rax
sar %r8
jz .Lrem /* division by zero */
cmp $-1, %r8
je .Lremneg
sar %rax
cqo
idiv %r8
sal %rdx
lea 1(%rdx), %rax
OP
ret
.Lremneg:
/* division by -1 */
mov $1, %rax
OP
ret
.Lrem:
C_JMP(rem)
EPILOG(rem)
/* bit operations */
/* ************** */
/* not */
PROLOG(lognot)
test $1, %rcx
jz .Llognot
lea -1(%rcx), %rax
not %rax
OP
ret
.Llognot:
C_JMP(lognot)
EPILOG(lognot)
/* and */
PROLOG(logand)
mov %rcx, %rax
and %rdx, %rax
test $1, %rax
jz .Llogand
OP
ret
.Llogand:
C_JMP(logand)
EPILOG(logand)
/* or */
PROLOG(logor)
test $1, %rcx
jz .Llogor
test $1, %rdx
jz .Llogor
mov %rcx, %rax
or %rdx, %rax
OP
ret
.Llogor:
C_JMP(logor)
EPILOG(logor)
/* xor */
PROLOG(logxor)
test $1, %rcx
jz .Llogxor
test $1, %rdx
jz .Llogxor
lea -1(%rcx), %rax
xor %rdx, %rax
OP
ret
.Llogxor:
C_JMP(logxor)
EPILOG(logxor)
/* shift_left */
PROLOG(shift_left)
test $1, %rcx
jz .Lshift_left2
lea -1(%rcx), %rax
mov %rcx, %r9
mov %rdx, %r10
sar %rdx
cmp $63, %rdx
jae .Lshift_left
mov %rdx, %rcx
mov %rax, %r8
sal %cl, %rax
mov %rax, %rdx
sar %cl, %rdx
cmp %r8, %rdx
jne .Lshift_left /* overflow */
inc %rax
OP
ret
.Lshift_left:
mov %r9, %rcx
mov %r10, %rdx
.Lshift_left2:
C_JMP(shift_left)
EPILOG(shift_left)
/* shift_right */
PROLOG(shift_right)
test $1, %rcx
jz .Lshift_right
mov %rcx, %rax
mov %rdx, %rcx
sar %rcx
js .Lshift_right
cmp $63, %rcx
jae .Lshift_right2
sar %cl, %rax
or $1, %rax
OP
ret
.Lshift_right2:
/* shift by 63 or more */
test %rax, %rax
js .Lshift_right3
mov $1, %rax
OP
ret
.Lshift_right3:
mov $-1, %rax
OP
ret
.Lshift_right:
C_JMP(shift_right)
EPILOG(shift_right)