import vanilla x86_64 code as x32

This commit is contained in:
rofl0r 2014-01-07 22:43:34 +01:00
commit 323272db17
75 changed files with 2677 additions and 0 deletions

97
src/fenv/x32/fenv.s Normal file
View file

@ -0,0 +1,97 @@
.global feclearexcept
.type feclearexcept,@function
feclearexcept:
# maintain exceptions in the sse mxcsr, clear x87 exceptions
mov %edi,%ecx
and $0x3f,%ecx
fnstsw %ax
test %eax,%ecx
jz 1f
fnclex
1: stmxcsr -8(%rsp)
and $0x3f,%eax
or %eax,-8(%rsp)
test %ecx,-8(%rsp)
jz 1f
not %ecx
and %ecx,-8(%rsp)
ldmxcsr -8(%rsp)
1: xor %eax,%eax
ret
.global feraiseexcept
.type feraiseexcept,@function
feraiseexcept:
and $0x3f,%edi
stmxcsr -8(%rsp)
or %edi,-8(%rsp)
ldmxcsr -8(%rsp)
xor %eax,%eax
ret
.global __fesetround
.type __fesetround,@function
__fesetround:
push %rax
xor %eax,%eax
mov %edi,%ecx
fnstcw (%rsp)
andb $0xf3,1(%rsp)
or %ch,1(%rsp)
fldcw (%rsp)
stmxcsr (%rsp)
shl $3,%ch
andb $0x9f,1(%rsp)
or %ch,1(%rsp)
ldmxcsr (%rsp)
pop %rcx
ret
.global fegetround
.type fegetround,@function
fegetround:
push %rax
stmxcsr (%rsp)
pop %rax
shr $3,%eax
and $0xc00,%eax
ret
.global fegetenv
.type fegetenv,@function
fegetenv:
xor %eax,%eax
fnstenv (%rdi)
stmxcsr 28(%rdi)
ret
.global fesetenv
.type fesetenv,@function
fesetenv:
xor %eax,%eax
inc %rdi
jz 1f
fldenv -1(%rdi)
ldmxcsr 27(%rdi)
ret
1: push %rax
push %rax
pushq $0xffff
pushq $0x37f
fldenv (%rsp)
pushq $0x1f80
ldmxcsr (%rsp)
add $40,%rsp
ret
.global fetestexcept
.type fetestexcept,@function
fetestexcept:
and $0x3f,%edi
push %rax
stmxcsr (%rsp)
pop %rsi
fnstsw %ax
or %esi,%eax
and %edi,%eax
ret

View file

@ -0,0 +1,12 @@
.global __syscall
.type __syscall,@function
__syscall:
movq %rdi,%rax
movq %rsi,%rdi
movq %rdx,%rsi
movq %rcx,%rdx
movq %r8,%r10
movq %r9,%r8
movq 8(%rsp),%r9
syscall
ret

6
src/ldso/x32/dlsym.s Normal file
View file

@ -0,0 +1,6 @@
.text
.global dlsym
.type dlsym,@function
dlsym:
mov (%rsp),%rdx
jmp __dlsym

16
src/ldso/x32/start.s Normal file
View file

@ -0,0 +1,16 @@
.text
.global _start
_start:
mov (%rsp),%rdi
lea 8(%rsp),%rsi
call __dynlink
pop %rdi
1: dec %edi
pop %rsi
cmp $-1,%rsi
jz 1b
inc %edi
push %rsi
push %rdi
xor %edx,%edx
jmp *%rax

View file

16
src/math/x32/acosl.s Normal file
View file

@ -0,0 +1,16 @@
# see ../i386/acos.s
.global acosl
.type acosl,@function
acosl:
fldt 8(%rsp)
1: fld %st(0)
fld1
fsub %st(0),%st(1)
fadd %st(2)
fmulp
fsqrt
fabs
fxch %st(1)
fpatan
ret

12
src/math/x32/asinl.s Normal file
View file

@ -0,0 +1,12 @@
.global asinl
.type asinl,@function
asinl:
fldt 8(%rsp)
1: fld %st(0)
fld1
fsub %st(0),%st(1)
fadd %st(2)
fmulp
fsqrt
fpatan
ret

7
src/math/x32/atan2l.s Normal file
View file

@ -0,0 +1,7 @@
.global atan2l
.type atan2l,@function
atan2l:
fldt 8(%rsp)
fldt 24(%rsp)
fpatan
ret

7
src/math/x32/atanl.s Normal file
View file

@ -0,0 +1,7 @@
.global atanl
.type atanl,@function
atanl:
fldt 8(%rsp)
fld1
fpatan
ret

1
src/math/x32/ceill.s Normal file
View file

@ -0,0 +1 @@
# see floorl.s

90
src/math/x32/exp2l.s Normal file
View file

@ -0,0 +1,90 @@
.global expm1l
.type expm1l,@function
expm1l:
fldt 8(%rsp)
fldl2e
fmulp
movl $0xc2820000,-4(%rsp)
flds -4(%rsp)
fucomp %st(1)
fnstsw %ax
sahf
fld1
jb 1f
# x*log2e <= -65, return -1 without underflow
fstp %st(1)
fchs
ret
1: fld %st(1)
fabs
fucom %st(1)
fnstsw %ax
fstp %st(0)
fstp %st(0)
sahf
ja 1f
f2xm1
ret
1: push %rax
call 1f
pop %rax
fld1
fsubrp
ret
.global exp2l
.type exp2l,@function
exp2l:
fldt 8(%rsp)
1: fld %st(0)
sub $16,%rsp
fstpt (%rsp)
mov 8(%rsp),%ax
and $0x7fff,%ax
cmp $0x3fff+13,%ax
jb 4f # |x| < 8192
cmp $0x3fff+15,%ax
jae 3f # |x| >= 32768
fsts (%rsp)
cmpl $0xc67ff800,(%rsp)
jb 2f # x > -16382
movl $0x5f000000,(%rsp)
flds (%rsp) # 0x1p63
fld %st(1)
fsub %st(1)
faddp
fucomp %st(1)
fnstsw
sahf
je 2f # x - 0x1p63 + 0x1p63 == x
movl $1,(%rsp)
flds (%rsp) # 0x1p-149
fdiv %st(1)
fstps (%rsp) # raise underflow
2: fld1
fld %st(1)
frndint
fxch %st(2)
fsub %st(2) # st(0)=x-rint(x), st(1)=1, st(2)=rint(x)
f2xm1
faddp # 2^(x-rint(x))
1: fscale
fstp %st(1)
add $16,%rsp
ret
3: xor %eax,%eax
4: cmp $0x3fff-64,%ax
fld1
jb 1b # |x| < 0x1p-64
fstpt (%rsp)
fistl 8(%rsp)
fildl 8(%rsp)
fsubrp %st(1)
addl $0x3fff,8(%rsp)
f2xm1
fld1
faddp # 2^(x-rint(x))
fldt (%rsp) # 2^rint(x)
fmulp
add $16,%rsp
ret

101
src/math/x32/expl.s Normal file
View file

@ -0,0 +1,101 @@
# exp(x) = 2^hi + 2^hi (2^lo - 1)
# where hi+lo = log2e*x with 128bit precision
# exact log2e*x calculation depends on nearest rounding mode
# using the exact multiplication method of Dekker and Veltkamp
.global expl
.type expl,@function
expl:
fldt 8(%rsp)
# interesting case: 0x1p-32 <= |x| < 16384
# check if (exponent|0x8000) is in [0xbfff-32, 0xbfff+13]
mov 16(%rsp), %ax
or $0x8000, %ax
sub $0xbfdf, %ax
cmp $45, %ax
jbe 2f
test %ax, %ax
fld1
js 1f
# if |x|>=0x1p14 or nan return 2^trunc(x)
fscale
fstp %st(1)
ret
# if |x|<0x1p-32 return 1+x
1: faddp
ret
# should be 0x1.71547652b82fe178p0L == 0x3fff b8aa3b29 5c17f0bc
# it will be wrong on non-nearest rounding mode
2: fldl2e
subq $48, %rsp
# hi = log2e_hi*x
# 2^hi = exp2l(hi)
fmul %st(1),%st
fld %st(0)
fstpt (%rsp)
fstpt 16(%rsp)
fstpt 32(%rsp)
call exp2l
# if 2^hi == inf return 2^hi
fld %st(0)
fstpt (%rsp)
cmpw $0x7fff, 8(%rsp)
je 1f
fldt 32(%rsp)
fldt 16(%rsp)
# fpu stack: 2^hi x hi
# exact mult: x*log2e
fld %st(1)
# c = 0x1p32+1
movq $0x41f0000000100000,%rax
pushq %rax
fldl (%rsp)
# xh = x - c*x + c*x
# xl = x - xh
fmulp
fld %st(2)
fsub %st(1), %st
faddp
fld %st(2)
fsub %st(1), %st
# yh = log2e_hi - c*log2e_hi + c*log2e_hi
movq $0x3ff7154765200000,%rax
pushq %rax
fldl (%rsp)
# fpu stack: 2^hi x hi xh xl yh
# lo = hi - xh*yh + xl*yh
fld %st(2)
fmul %st(1), %st
fsubp %st, %st(4)
fmul %st(1), %st
faddp %st, %st(3)
# yl = log2e_hi - yh
movq $0x3de705fc2f000000,%rax
pushq %rax
fldl (%rsp)
# fpu stack: 2^hi x lo xh xl yl
# lo += xh*yl + xl*yl
fmul %st, %st(2)
fmulp %st, %st(1)
fxch %st(2)
faddp
faddp
# log2e_lo
movq $0xbfbe,%rax
pushq %rax
movq $0x82f0025f2dc582ee,%rax
pushq %rax
fldt (%rsp)
addq $40,%rsp
# fpu stack: 2^hi x lo log2e_lo
# lo += log2e_lo*x
# return 2^hi + 2^hi (2^lo - 1)
fmulp %st, %st(2)
faddp
f2xm1
fmul %st(1), %st
faddp
1: addq $48, %rsp
ret

1
src/math/x32/expm1l.s Normal file
View file

@ -0,0 +1 @@
# see exp2l.s

9
src/math/x32/fabs.s Normal file
View file

@ -0,0 +1,9 @@
.global fabs
.type fabs,@function
fabs:
xor %eax,%eax
dec %rax
shr %rax
movq %rax,%xmm1
andpd %xmm1,%xmm0
ret

7
src/math/x32/fabsf.s Normal file
View file

@ -0,0 +1,7 @@
.global fabsf
.type fabsf,@function
fabsf:
mov $0x7fffffff,%eax
movq %rax,%xmm1
andps %xmm1,%xmm0
ret

6
src/math/x32/fabsl.s Normal file
View file

@ -0,0 +1,6 @@
.global fabsl
.type fabsl,@function
fabsl:
fldt 8(%rsp)
fabs
ret

27
src/math/x32/floorl.s Normal file
View file

@ -0,0 +1,27 @@
.global floorl
.type floorl,@function
floorl:
fldt 8(%rsp)
1: mov $0x7,%al
1: fstcw 8(%rsp)
mov 9(%rsp),%ah
mov %al,9(%rsp)
fldcw 8(%rsp)
frndint
mov %ah,9(%rsp)
fldcw 8(%rsp)
ret
.global ceill
.type ceill,@function
ceill:
fldt 8(%rsp)
mov $0xb,%al
jmp 1b
.global truncl
.type truncl,@function
truncl:
fldt 8(%rsp)
mov $0xf,%al
jmp 1b

11
src/math/x32/fmodl.s Normal file
View file

@ -0,0 +1,11 @@
.global fmodl
.type fmodl,@function
fmodl:
fldt 24(%rsp)
fldt 8(%rsp)
1: fprem
fstsw %ax
sahf
jp 1b
fstp %st(1)
ret

5
src/math/x32/llrint.s Normal file
View file

@ -0,0 +1,5 @@
.global llrint
.type llrint,@function
llrint:
cvtsd2si %xmm0,%rax
ret

5
src/math/x32/llrintf.s Normal file
View file

@ -0,0 +1,5 @@
.global llrintf
.type llrintf,@function
llrintf:
cvtss2si %xmm0,%rax
ret

7
src/math/x32/llrintl.s Normal file
View file

@ -0,0 +1,7 @@
.global llrintl
.type llrintl,@function
llrintl:
fldt 8(%rsp)
fistpll 8(%rsp)
mov 8(%rsp),%rax
ret

7
src/math/x32/log10l.s Normal file
View file

@ -0,0 +1,7 @@
.global log10l
.type log10l,@function
log10l:
fldlg2
fldt 8(%rsp)
fyl2x
ret

15
src/math/x32/log1pl.s Normal file
View file

@ -0,0 +1,15 @@
.global log1pl
.type log1pl,@function
log1pl:
mov 14(%rsp),%eax
fldln2
and $0x7fffffff,%eax
fldt 8(%rsp)
cmp $0x3ffd9400,%eax
ja 1f
fyl2xp1
ret
1: fld1
faddp
fyl2x
ret

7
src/math/x32/log2l.s Normal file
View file

@ -0,0 +1,7 @@
.global log2l
.type log2l,@function
log2l:
fld1
fldt 8(%rsp)
fyl2x
ret

7
src/math/x32/logl.s Normal file
View file

@ -0,0 +1,7 @@
.global logl
.type logl,@function
logl:
fldln2
fldt 8(%rsp)
fyl2x
ret

5
src/math/x32/lrint.s Normal file
View file

@ -0,0 +1,5 @@
.global lrint
.type lrint,@function
lrint:
cvtsd2si %xmm0,%rax
ret

5
src/math/x32/lrintf.s Normal file
View file

@ -0,0 +1,5 @@
.global lrintf
.type lrintf,@function
lrintf:
cvtss2si %xmm0,%rax
ret

7
src/math/x32/lrintl.s Normal file
View file

@ -0,0 +1,7 @@
.global lrintl
.type lrintl,@function
lrintl:
fldt 8(%rsp)
fistpll 8(%rsp)
mov 8(%rsp),%rax
ret

11
src/math/x32/remainderl.s Normal file
View file

@ -0,0 +1,11 @@
.global remainderl
.type remainderl,@function
remainderl:
fldt 24(%rsp)
fldt 8(%rsp)
1: fprem1
fstsw %ax
sahf
jp 1b
fstp %st(1)
ret

6
src/math/x32/rintl.s Normal file
View file

@ -0,0 +1,6 @@
.global rintl
.type rintl,@function
rintl:
fldt 8(%rsp)
frndint
ret

4
src/math/x32/sqrt.s Normal file
View file

@ -0,0 +1,4 @@
.global sqrt
.type sqrt,@function
sqrt: sqrtsd %xmm0, %xmm0
ret

4
src/math/x32/sqrtf.s Normal file
View file

@ -0,0 +1,4 @@
.global sqrtf
.type sqrtf,@function
sqrtf: sqrtss %xmm0, %xmm0
ret

5
src/math/x32/sqrtl.s Normal file
View file

@ -0,0 +1,5 @@
.global sqrtl
.type sqrtl,@function
sqrtl: fldt 8(%rsp)
fsqrt
ret

1
src/math/x32/truncl.s Normal file
View file

@ -0,0 +1 @@
# see floorl.s

12
src/process/x32/vfork.s Normal file
View file

@ -0,0 +1,12 @@
.global __vfork
.weak vfork
.type __vfork,@function
.type vfork,@function
__vfork:
vfork:
pop %rdx
mov $58,%eax
syscall
push %rdx
mov %rax,%rdi
jmp __syscall_ret

22
src/setjmp/x32/longjmp.s Normal file
View file

@ -0,0 +1,22 @@
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
.global _longjmp
.global longjmp
.type _longjmp,@function
.type longjmp,@function
_longjmp:
longjmp:
mov %rsi,%rax /* val will be longjmp return */
test %rax,%rax
jnz 1f
inc %rax /* if val==0, val=1 per longjmp semantics */
1:
mov (%rdi),%rbx /* rdi is the jmp_buf, restore regs from it */
mov 8(%rdi),%rbp
mov 16(%rdi),%r12
mov 24(%rdi),%r13
mov 32(%rdi),%r14
mov 40(%rdi),%r15
mov 48(%rdi),%rdx /* this ends up being the stack pointer */
mov %rdx,%rsp
mov 56(%rdi),%rdx /* this is the instruction pointer */
jmp *%rdx /* goto saved address without altering rsp */

22
src/setjmp/x32/setjmp.s Normal file
View file

@ -0,0 +1,22 @@
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
.global __setjmp
.global _setjmp
.global setjmp
.type __setjmp,@function
.type _setjmp,@function
.type setjmp,@function
__setjmp:
_setjmp:
setjmp:
mov %rbx,(%rdi) /* rdi is jmp_buf, move registers onto it */
mov %rbp,8(%rdi)
mov %r12,16(%rdi)
mov %r13,24(%rdi)
mov %r14,32(%rdi)
mov %r15,40(%rdi)
lea 8(%rsp),%rdx /* this is our rsp WITHOUT current ret addr */
mov %rdx,48(%rdi)
mov (%rsp),%rdx /* save return addr ptr for new rip */
mov %rdx,56(%rdi)
xor %rax,%rax /* always return 0 */
ret

8
src/signal/x32/restore.s Normal file
View file

@ -0,0 +1,8 @@
.global __restore_rt
.global __restore
.type __restore_rt,@function
.type __restore,@function
__restore_rt:
__restore:
movl $15, %eax
syscall

View file

@ -0,0 +1,14 @@
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
.global sigsetjmp
.type sigsetjmp,@function
sigsetjmp:
andl %esi,%esi
movq %rsi,64(%rdi)
jz 1f
pushq %rdi
leaq 72(%rdi),%rdx
xorl %esi,%esi
movl $2,%edi
call sigprocmask
popq %rdi
1: jmp setjmp

View file

@ -0,0 +1,10 @@
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
.text
.global __set_thread_area
.type __set_thread_area,@function
__set_thread_area:
mov %rdi,%rsi /* shift for syscall */
movl $0x1002,%edi /* SET_FS register */
movl $158,%eax /* set fs segment to */
syscall /* arch_prctl(SET_FS, arg)*/
ret

View file

@ -0,0 +1,10 @@
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
.text
.global __unmapself
.type __unmapself,@function
__unmapself:
movl $11,%eax /* SYS_munmap */
syscall /* munmap(arg2,arg3) */
xor %rdi,%rdi /* exit() args: always return success */
movl $60,%eax /* SYS_exit */
syscall /* exit(0) */

27
src/thread/x32/clone.s Normal file
View file

@ -0,0 +1,27 @@
.text
.global __clone
.type __clone,@function
__clone:
xor %eax,%eax
mov $56,%al
mov %rdi,%r11
mov %rdx,%rdi
mov %r8,%rdx
mov %r9,%r8
mov 8(%rsp),%r10
mov %r11,%r9
and $-16,%rsi
sub $8,%rsi
mov %rcx,(%rsi)
syscall
test %eax,%eax
jnz 1f
xor %ebp,%ebp
pop %rdi
call *%r9
mov %eax,%edi
xor %eax,%eax
mov $60,%al
syscall
hlt
1: ret

View file

@ -0,0 +1,23 @@
.text
.global __syscall_cp_asm
.type __syscall_cp_asm,@function
__syscall_cp_asm:
.global __cp_begin
__cp_begin:
mov (%rdi),%eax
test %eax,%eax
jnz __cancel
mov %rdi,%r11
mov %rsi,%rax
mov %rdx,%rdi
mov %rcx,%rsi
mov %r8,%rdx
mov %r9,%r10
mov 8(%rsp),%r8
mov 16(%rsp),%r9
mov %r11,8(%rsp)
syscall
.global __cp_end
__cp_end:
ret