Skip to content

Instantly share code, notes, and snippets.

@3outeille
Last active September 6, 2022 14:51
Show Gist options
  • Save 3outeille/c65a1a81635d1caf0a8072e179bcb869 to your computer and use it in GitHub Desktop.
Save 3outeille/c65a1a81635d1caf0a8072e179bcb869 to your computer and use it in GitHub Desktop.
Macro expansion to understand how C function calls x86inc function
%define ARCH_X86_64 1
; sad_16x16, 4, 7, 5, src, src_stride, dst, dst_stride, src_stride3, dst_stride3, cnt
; function_name, #args (%1), #regs (%2), #xmm_regs (%3), [stack_size,] (%4) arg_names... (%5-*)
%macro PUSH_IF_USED 1-*
%rep %0
%if %1 < regs_used
push r%1
%endif
%rotate 1
%endrep
%endmacro
%macro LOAD_IF_USED 1-*
%rep %0
%if %1 < num_args
mov r%1, r %+ %1 %+ mp
%endif
%rotate 1
%endrep
%endmacro
%macro CAT_UNDEF 2
%undef %1%2
%endmacro
%macro CAT_XDEFINE 3
%xdefine %1%2 %3
%endmacro
%macro DEFINE_ARGS 0-*
%ifdef n_arg_names
> "%ifdef n_arg_names" cond
%assign %%i 0
%rep n_arg_names
CAT_UNDEF arg_name %+ %%i, q
CAT_UNDEF arg_name %+ %%i, d
CAT_UNDEF arg_name %+ %%i, w
CAT_UNDEF arg_name %+ %%i, h
CAT_UNDEF arg_name %+ %%i, b
CAT_UNDEF arg_name %+ %%i, m
CAT_UNDEF arg_name %+ %%i, mp
CAT_UNDEF arg_name, %%i
%assign %%i %%i+1
%endrep
%endif
%xdefine %%stack_offset stack_offset
%undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
%assign %%i 0
%rep %0
%1q r %+ %%i %+ q
%1d r %+ %%i %+ d
%1w r %+ %%i %+ w
%1h r %+ %%i %+ h
%1b r %+ %%i %+ b
%1m r %+ %%i %+ m
%1mp r %+ %%i %+ mp
CAT_XDEFINE arg_name, %%i, %1
%assign %%i %%i+1
%rotate 1
%endrep
%xdefine stack_offset %%stack_offset
%assign n_arg_names %0
%endmacro
%macro PROLOGUE 2-5+ 0, 0
> "begin macro"
args:
"%1" => %1
"%2" => %2
"%3" => %3
"%4" => %4
"%5" => %5
%assign num_args %1
%assign regs_used %2
%assign xmm_regs_used %3
; SETUP_STACK_POINTER %4 ; [stack_size] optional in sad_16x16
PUSH_IF_USED 9, 10, 11, 12, 13, 14 ; on unix64/win64, we have 7 or more caller-save GPRs available, so no GPR contents are pushed to the stack.
; ALLOC_STACK %4 ; [stack_size] optional in sad_16x16
; LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14 ; not used here
%if %0 > 4
%ifnum %4
> "%ifnum %4" cond
DEFINE_ARGS %5
%else
> "else" cond
DEFINE_ARGS %4, %5
%endif
%elifnnum %4
> "%elifnnum %4" cond
DEFINE_ARGS %4
%endif
> "endmacro"
%endmacro
PROLOGUE 4, 7, 5, src, src_stride, dst, dst_stride, src_stride3, dst_stride3, cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment