Commit 7c860f07 authored by Henrik Gramner's avatar Henrik Gramner Committed by Fiona Glaser

x86inc: Support arbitrary stack alignments

If the stack is known to be at least 32-byte aligned we can safely store ymm
registers on the stack without doing manual alignment.

Change ALLOC_STACK to always align the stack before allocating stack space for
consistency. Previously alignment would occur either before or after allocating
stack space depending on whether manual alignment was required or not.
parent 039fab92
......@@ -304,7 +304,7 @@ uint32_t x264_cpu_detect( void )
x264_log( NULL, X264_LOG_WARNING, "unable to determine cacheline size\n" );
}
#if BROKEN_STACK_ALIGNMENT
#if STACK_ALIGNMENT < 16
cpu |= X264_CPU_STACK_MOD4;
#endif
......
......@@ -57,7 +57,7 @@ void x264_safe_intel_cpu_indicator_init( void );
* alignment between functions (osdep.h handles manual alignment of arrays
* if it doesn't).
*/
#if (ARCH_X86 || HAVE_32B_STACK_ALIGNMENT) && HAVE_MMX
#if (ARCH_X86 || STACK_ALIGNMENT > 16) && HAVE_MMX
intptr_t x264_stack_align( void (*func)(), ... );
#define x264_stack_align(func,...) x264_stack_align((void (*)())func, __VA_ARGS__)
#else
......
......@@ -126,7 +126,7 @@ int x264_is_pipe( const char *path );
#define EXPAND(x) x
#if HAVE_32B_STACK_ALIGNMENT
#if STACK_ALIGNMENT >= 32
#define ALIGNED_ARRAY_32( type, name, sub1, ... )\
ALIGNED_32( type name sub1 __VA_ARGS__ )
#else
......
......@@ -1392,19 +1392,18 @@ cglobal deblock_%1_luma, 5,5,8,2*%2
;-----------------------------------------------------------------------------
; void deblock_h_luma( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 )
;-----------------------------------------------------------------------------
%if cpuflag(avx)
INIT_XMM cpuname
%else
INIT_MMX cpuname
%endif
cglobal deblock_h_luma, 0,5,8,0x60+HAVE_ALIGNED_STACK*12
mov r0, r0mp
cglobal deblock_h_luma, 1,5,8,0x60+12
mov r3, r1m
lea r4, [r3*3]
sub r0, 4
lea r1, [r0+r4]
%define pix_tmp esp+12*HAVE_ALIGNED_STACK
%define pix_tmp esp+12
; esp is intentionally misaligned to make it aligned after pushing the arguments for deblock_%1_luma.
; transpose 6x16 -> tmp space
TRANSPOSE6x8_MEM PASS8ROWS(r0, r1, r3, r4), pix_tmp
......
......@@ -42,6 +42,14 @@
%define public_prefix private_prefix
%endif
%ifndef STACK_ALIGNMENT
%if ARCH_X86_64
%define STACK_ALIGNMENT 16
%else
%define STACK_ALIGNMENT 4
%endif
%endif
%define WIN64 0
%define UNIX64 0
%if ARCH_X86_64
......@@ -94,8 +102,9 @@ CPU amdnop
; %1 = number of arguments. loads them from stack if needed.
; %2 = number of registers used. pushes callee-saved regs if needed.
; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
; %4 = (optional) stack size to be allocated. If not aligned (x86-32 ICC 10.x,
; MSVC or YMM), the stack will be manually aligned (to 16 or 32 bytes),
; %4 = (optional) stack size to be allocated. The stack will be aligned before
; allocating the specified stack size. If the required stack alignment is
; larger than the known stack alignment the stack will be manually aligned
; and an extra register will be allocated to hold the original stack
; pointer (to not invalidate r0m etc.). To prevent the use of an extra
; register as stack pointer, request a negative stack size.
......@@ -103,8 +112,10 @@ CPU amdnop
; PROLOGUE can also be invoked by adding the same options to cglobal
; e.g.
; cglobal foo, 2,3,0, dst, src, tmp
; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
; cglobal foo, 2,3,7,0x40, dst, src, tmp
; declares a function (foo) that automatically loads two arguments (dst and
; src) into registers, uses one additional register (tmp) plus 7 vector
; registers (m0-m6) and allocates 0x40 bytes of stack space.
; TODO Some functions can use some args directly from the stack. If they're the
; last args then you can just not declare them, but if they're in the middle
......@@ -304,26 +315,28 @@ DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
%assign n_arg_names %0
%endmacro
%define required_stack_alignment ((mmsize + 15) & ~15)
%macro ALLOC_STACK 1-2 0 ; stack_size, n_xmm_regs (for win64 only)
%ifnum %1
%if %1 != 0
%assign %%stack_alignment ((mmsize + 15) & ~15)
%assign %%pad 0
%assign stack_size %1
%if stack_size < 0
%assign stack_size -stack_size
%endif
%assign stack_size_padded stack_size
%if WIN64
%assign stack_size_padded stack_size_padded + 32 ; reserve 32 bytes for shadow space
%assign %%pad %%pad + 32 ; shadow space
%if mmsize != 8
%assign xmm_regs_used %2
%if xmm_regs_used > 8
%assign stack_size_padded stack_size_padded + (xmm_regs_used-8)*16
%assign %%pad %%pad + (xmm_regs_used-8)*16 ; callee-saved xmm registers
%endif
%endif
%endif
%if mmsize <= 16 && HAVE_ALIGNED_STACK
%assign stack_size_padded stack_size_padded + %%stack_alignment - gprsize - (stack_offset & (%%stack_alignment - 1))
%if required_stack_alignment <= STACK_ALIGNMENT
; maintain the current stack alignment
%assign stack_size_padded stack_size + %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
SUB rsp, stack_size_padded
%else
%assign %%reg_num (regs_used - 1)
......@@ -332,17 +345,17 @@ DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
; it, i.e. in [rsp+stack_size_padded], so we can restore the
; stack in a single instruction (i.e. mov rsp, rstk or mov
; rsp, [rsp+stack_size_padded])
mov rstk, rsp
%if %1 < 0 ; need to store rsp on stack
sub rsp, gprsize+stack_size_padded
and rsp, ~(%%stack_alignment-1)
%xdefine rstkm [rsp+stack_size_padded]
mov rstkm, rstk
%xdefine rstkm [rsp + stack_size + %%pad]
%assign %%pad %%pad + gprsize
%else ; can keep rsp in rstk during whole function
sub rsp, stack_size_padded
and rsp, ~(%%stack_alignment-1)
%xdefine rstkm rstk
%endif
%assign stack_size_padded stack_size + ((%%pad + required_stack_alignment-1) & ~(required_stack_alignment-1))
mov rstk, rsp
and rsp, ~(required_stack_alignment-1)
sub rsp, stack_size_padded
movifnidn rstkm, rstk
%endif
WIN64_PUSH_XMM
%endif
......@@ -351,7 +364,7 @@ DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
%macro SETUP_STACK_POINTER 1
%ifnum %1
%if %1 != 0 && (HAVE_ALIGNED_STACK == 0 || mmsize == 32)
%if %1 != 0 && required_stack_alignment > STACK_ALIGNMENT
%if %1 > 0
%assign regs_used (regs_used + 1)
%elif ARCH_X86_64 && regs_used == num_args && num_args <= 4 + UNIX64 * 2
......@@ -425,7 +438,9 @@ DECLARE_REG 14, R15, 120
%assign xmm_regs_used %1
ASSERT xmm_regs_used <= 16
%if xmm_regs_used > 8
%assign stack_size_padded (xmm_regs_used-8)*16 + (~stack_offset&8) + 32
; Allocate stack space for callee-saved xmm registers plus shadow space and align the stack.
%assign %%pad (xmm_regs_used-8)*16 + 32
%assign stack_size_padded %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
SUB rsp, stack_size_padded
%endif
WIN64_PUSH_XMM
......@@ -441,7 +456,7 @@ DECLARE_REG 14, R15, 120
%endrep
%endif
%if stack_size_padded > 0
%if stack_size > 0 && (mmsize == 32 || HAVE_ALIGNED_STACK == 0)
%if stack_size > 0 && required_stack_alignment > STACK_ALIGNMENT
mov rsp, rstkm
%else
add %1, stack_size_padded
......@@ -507,7 +522,7 @@ DECLARE_REG 14, R15, 72
%macro RET 0
%if stack_size_padded > 0
%if mmsize == 32 || HAVE_ALIGNED_STACK == 0
%if required_stack_alignment > STACK_ALIGNMENT
mov rsp, rstkm
%else
add rsp, stack_size_padded
......@@ -563,7 +578,7 @@ DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
%macro RET 0
%if stack_size_padded > 0
%if mmsize == 32 || HAVE_ALIGNED_STACK == 0
%if required_stack_alignment > STACK_ALIGNMENT
mov rsp, rstkm
%else
add rsp, stack_size_padded
......
......@@ -557,7 +557,7 @@ esac
LDFLAGS="$LDFLAGS $libm"
aligned_stack=1
stack_alignment=16
case $host_cpu in
i*86)
ARCH="X86"
......@@ -577,8 +577,7 @@ case $host_cpu in
if [ $SYS = LINUX ]; then
# < 11 is completely incapable of keeping a mod16 stack
if cpp_check "" "" "__INTEL_COMPILER < 1100" ; then
define BROKEN_STACK_ALIGNMENT
aligned_stack=0
stack_alignment=4
# 11 <= x < 12 is capable of keeping a mod16 stack, but defaults to not doing so.
elif cpp_check "" "" "__INTEL_COMPILER < 1200" ; then
CFLAGS="$CFLAGS -falign-stack=assume-16-byte"
......@@ -586,7 +585,7 @@ case $host_cpu in
# >= 12 defaults to a mod16 stack
fi
# icl on windows has no mod16 stack support
[ $SYS = WINDOWS ] && define BROKEN_STACK_ALIGNMENT && aligned_stack=0
[ $SYS = WINDOWS ] && stack_alignment=4
fi
if [ "$SYS" = MACOSX ]; then
ASFLAGS="$ASFLAGS -f macho -DPREFIX"
......@@ -681,7 +680,6 @@ case $host_cpu in
ARCH="$(echo $host_cpu | tr a-z A-Z)"
;;
esac
ASFLAGS="$ASFLAGS -DHAVE_ALIGNED_STACK=${aligned_stack}"
if [ $SYS = WINDOWS ]; then
if ! rc_check "0 RCDATA {0}" ; then
......@@ -735,9 +733,9 @@ if [ $asm = auto -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
fi
ASFLAGS="$ASFLAGS -Worphan-labels"
define HAVE_MMX
if cc_check '' -mpreferred-stack-boundary=5 ; then
if [ $compiler = GNU ] && cc_check '' -mpreferred-stack-boundary=5 ; then
CFLAGS="$CFLAGS -mpreferred-stack-boundary=5"
define HAVE_32B_STACK_ALIGNMENT
stack_alignment=32
fi
fi
......@@ -762,6 +760,9 @@ fi
define ARCH_$ARCH
define SYS_$SYS
define STACK_ALIGNMENT $stack_alignment
ASFLAGS="$ASFLAGS -DSTACK_ALIGNMENT=$stack_alignment"
# skip endianness check for Intel Compiler, as all supported platforms are little. the -ipo flag will also cause the check to fail
if [ $compiler = GNU ]; then
echo "int i[2] = {0x42494745,0}; double f[2] = {0x1.0656e6469616ep+102,0};" > conftest.c
......
......@@ -2530,7 +2530,7 @@ static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
{
*cpu_ref = *cpu_new;
*cpu_new |= flags;
#if BROKEN_STACK_ALIGNMENT
#if STACK_ALIGNMENT < 16
*cpu_new |= X264_CPU_STACK_MOD4;
#endif
if( *cpu_new & X264_CPU_SSE2_IS_FAST )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment