// Code generated by command: go run gen_fse.go -out ../fse_decoder.s -arch amd64,arm64 -pkg=zstd. DO NOT EDIT.
// EXPERIMENTAL arm64 output lowered from an amd64 avo program.

//go:build arm64 && !appengine && !noasm && gc && !noasm

// func buildDtable_asm(s *fseDecoder, ctx *buildDtableAsmContext) int
TEXT ·buildDtable_asm(SB), $0-24
	MOVD ctx+8(FP), R1
	MOVD s+0(FP), R6

	// Load values
	MOVBU 4098(R6), R2
	MOVD  $0, R0
	MOVD  $1, R16
	LSL   R2, R16, R16
	ORR   R16, R0, R0
	MOVD  (R1), R3
	MOVD  16(R1), R5
	SUB   $1, R0, R7
	MOVD  8(R1), R1
	MOVHU 4096(R6), R6

	// End load values
	// Init, lay down lowprob symbols
	MOVD $0, R8
	JMP  init_main_loop_condition

init_main_loop:
	ADD  R8<<1, R1, R15
	MOVH (R15), R9
	CMN  $1, R9
	BNE  do_not_update_high_threshold
	ADD  R7<<3, R5, R15
	MOVB R8, 1(R15)
	SUB  $1, R7, R7
	MOVD $0x0000000000000001, R9

do_not_update_high_threshold:
	ADD  R8<<1, R3, R15
	MOVH R9, (R15)
	ADD  $1, R8, R8

init_main_loop_condition:
	CMP R6, R8
	BLT init_main_loop

	// Spread symbols
	// Calculate table step
	MOVD R0, R8
	LSR  $0x01, R8, R8
	MOVD R0, R9
	LSR  $0x03, R9, R9
	ADD  R9, R8, R8
	ADD  $3, R8, R8

	// Fill add bits values
	SUB  $1, R0, R9
	MOVD $0, R10
	MOVD $0, R11
	JMP  spread_main_loop_condition

spread_main_loop:
	MOVD $0, R12
	ADD  R11<<1, R1, R15
	MOVH (R15), R13
	JMP  spread_inner_loop_condition

spread_inner_loop:
	ADD  R10<<3, R5, R15
	MOVB R11, 1(R15)

adjust_position:
	ADD R8, R10, R10
	AND R9, R10, R10
	CMP R7, R10
	BGT adjust_position
	ADD $1, R12, R12

spread_inner_loop_condition:
	CMP R13, R12
	BLT spread_inner_loop
	ADD $1, R11, R11

spread_main_loop_condition:
	CMP  R6, R11
	BLT  spread_main_loop
	TST  R10, R10
	BEQ  spread_check_ok
	MOVD ctx+8(FP), R0
	MOVD R10, 24(R0)
	MOVD $+1, R16
	MOVD R16, ret+16(FP)
	RET

spread_check_ok:
	// Build Decoding table
	MOVD $0, R6

build_table_main_table:
	ADD   R6<<3, R5, R15
	MOVBU 1(R15), R1
	ADD   R1<<1, R3, R15
	MOVHU (R15), R7
	ADD   $1, R7, R8
	ADD   R1<<1, R3, R15
	MOVH  R8, (R15)
	MOVD  R7, R8
	CLZ   R8, R16
	MOVD  $63, R8
	SUB   R16, R8, R8
	MOVD  R2, R1
	SUB   R8, R1, R1
	LSL   R1, R7, R7
	SUB   R0, R7, R7
	ADD   R6<<3, R5, R15
	MOVB  R1, (R15)
	ADD   R6<<3, R5, R15
	MOVH  R7, 2(R15)
	CMP   R0, R7
	BLE   build_table_check1_ok
	MOVD  ctx+8(FP), R1
	MOVD  R7, 24(R1)
	MOVD  R0, 32(R1)
	MOVD  $+2, R16
	MOVD  R16, ret+16(FP)
	RET

build_table_check1_ok:
	TST  R1, R1
	BNE  build_table_check2_ok
	CMP  R6, R7
	BNE  build_table_check2_ok
	MOVD ctx+8(FP), R0
	MOVD R7, 24(R0)
	MOVD R6, 32(R0)
	MOVD $+3, R16
	MOVD R16, ret+16(FP)
	RET

build_table_check2_ok:
	ADD  $1, R6, R6
	CMP  R0, R6
	BLT  build_table_main_table
	MOVD $+0, R16
	MOVD R16, ret+16(FP)
	RET
