.global poly_invntt
poly_invntt:
vmovdqa		_16xq(%rip),%ymm0
vmovdqa		_16xv(%rip),%ymm1
lea		zetas_inv(%rip),%rdx

xor		%rax,%rax
xor		%rcx,%rcx
.p2align 5
_looptop_start_76543:
#level7
#load
vmovdqa		(%rsi),%ymm4
vmovdqa		32(%rsi),%ymm5
vmovdqa		64(%rsi),%ymm6
vmovdqa		96(%rsi),%ymm7

#update
vpsubw		%ymm6,%ymm4,%ymm10
vpsubw		%ymm7,%ymm5,%ymm11
vpaddw		%ymm6,%ymm4,%ymm6
vpaddw		%ymm7,%ymm5,%ymm7

#zetas
vmovdqa		(%rdx,%rax),%ymm2
vmovdqa		32(%rdx,%rax),%ymm3

#mul
vpmullw		%ymm2,%ymm10,%ymm8
vpmullw		%ymm2,%ymm11,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11

#reduce
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm8,%ymm10,%ymm8
vpsubw		%ymm9,%ymm11,%ymm9

#shuffle
vpslld		$16,%ymm7,%ymm10
vpslld		$16,%ymm9,%ymm11
vpblendw	$0xAA,%ymm10,%ymm6,%ymm4
vpblendw	$0xAA,%ymm11,%ymm8,%ymm5
vpsrld		$16,%ymm6,%ymm12
vpsrld		$16,%ymm8,%ymm13
vpblendw	$0xAA,%ymm7,%ymm12,%ymm6
vpblendw	$0xAA,%ymm9,%ymm13,%ymm7

#level6
#update
vpsubw		%ymm6,%ymm4,%ymm10
vpsubw		%ymm7,%ymm5,%ymm11
vpaddw		%ymm6,%ymm4,%ymm6
vpaddw		%ymm7,%ymm5,%ymm7

#zetas
vmovdqa		1152(%rdx,%rax),%ymm2
vmovdqa		1184(%rdx,%rax),%ymm3

#mul
vpmullw		%ymm2,%ymm10,%ymm8
vpmullw		%ymm2,%ymm11,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11

#reduce
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm8,%ymm10,%ymm8
vpsubw		%ymm9,%ymm11,%ymm9

#reduce2
vpmulhw		%ymm1,%ymm6,%ymm10
vpmulhw		%ymm1,%ymm7,%ymm11
vpsraw		$10,%ymm10,%ymm10
vpsraw		$10,%ymm11,%ymm11
vpmullw		%ymm0,%ymm10,%ymm10
vpmullw		%ymm0,%ymm11,%ymm11
vpsubw		%ymm10,%ymm6,%ymm6
vpsubw		%ymm11,%ymm7,%ymm7

#shuffle
vpsllq		$32,%ymm7,%ymm10
vpsllq		$32,%ymm9,%ymm11
vpblendd	$0xAA,%ymm10,%ymm6,%ymm4
vpblendd	$0xAA,%ymm11,%ymm8,%ymm5
vpsrlq		$32,%ymm6,%ymm12
vpsrlq		$32,%ymm8,%ymm13
vpblendd	$0xAA,%ymm7,%ymm12,%ymm6
vpblendd	$0xAA,%ymm9,%ymm13,%ymm7

#level5
#update
vpsubw		%ymm6,%ymm4,%ymm10
vpsubw		%ymm7,%ymm5,%ymm11
vpaddw		%ymm6,%ymm4,%ymm6
vpaddw		%ymm7,%ymm5,%ymm7

#zetas
vmovdqa		2304(%rdx,%rax),%ymm2
vmovdqa		2336(%rdx,%rax),%ymm3

#mul
vpmullw		%ymm2,%ymm10,%ymm8
vpmullw		%ymm2,%ymm11,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11

#reduce
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm8,%ymm10,%ymm8
vpsubw		%ymm9,%ymm11,%ymm9

#shuffle
vpunpcklqdq	%ymm7,%ymm6,%ymm4
vpunpcklqdq	%ymm9,%ymm8,%ymm5
vpunpckhqdq	%ymm7,%ymm6,%ymm6
vpunpckhqdq	%ymm9,%ymm8,%ymm7

#level4
#update
vpsubw		%ymm6,%ymm4,%ymm10
vpsubw		%ymm7,%ymm5,%ymm11
vpaddw		%ymm6,%ymm4,%ymm6
vpaddw		%ymm7,%ymm5,%ymm7

#zetas
vmovdqa		3456(%rdx,%rax),%ymm2
vmovdqa		3488(%rdx,%rax),%ymm3

#mul
vpmullw		%ymm2,%ymm10,%ymm8
vpmullw		%ymm2,%ymm11,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11

#reduce
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm8,%ymm10,%ymm8
vpsubw		%ymm9,%ymm11,%ymm9

#reduce2
vpmulhw		%ymm1,%ymm6,%ymm10
vpmulhw		%ymm1,%ymm7,%ymm11
vpsraw		$10,%ymm10,%ymm10
vpsraw		$10,%ymm11,%ymm11
vpmullw		%ymm0,%ymm10,%ymm10
vpmullw		%ymm0,%ymm11,%ymm11
vpsubw		%ymm10,%ymm6,%ymm6
vpsubw		%ymm11,%ymm7,%ymm7

#shuffle
vperm2i128	$0x20,%ymm7,%ymm6,%ymm4
vperm2i128	$0x31,%ymm7,%ymm6,%ymm6
vperm2i128	$0x20,%ymm9,%ymm8,%ymm5
vperm2i128	$0x31,%ymm9,%ymm8,%ymm7

#level3
#update
vpsubw		%ymm6,%ymm4,%ymm10
vpsubw		%ymm7,%ymm5,%ymm11
vpaddw		%ymm6,%ymm4,%ymm6
vpaddw		%ymm7,%ymm5,%ymm7

#zetas
vpbroadcastd		4608(%rdx,%rcx),%ymm2
vpbroadcastd		4612(%rdx,%rcx),%ymm3

#mul
vpmullw		%ymm2,%ymm10,%ymm8
vpmullw		%ymm2,%ymm11,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11

#reduce
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm8,%ymm10,%ymm8
vpsubw		%ymm9,%ymm11,%ymm9

#store
vmovdqa		%ymm6,(%rdi)
vmovdqa		%ymm7,32(%rdi)
vmovdqa		%ymm8,64(%rdi)
vmovdqa		%ymm9,96(%rdi)

add		$128,%rsi
add		$128,%rdi
add		$8,%rcx
add		$64,%rax
cmp		$1152,%rax
jb		_looptop_start_76543

sub		$2304,%rdi

#level2
vmovdqa _16xwinvqinv(%rip),%ymm2 #w^-1qinv
vmovdqa _16xwinv(%rip),%ymm3     #w^-1

xor		%rax,%rax
.p2align 5
_looptop_start_2:
#load
vpbroadcastd 4752(%rdx,%rax),%ymm4 #z^-1qinv
vpbroadcastd 4760(%rdx,%rax),%ymm5 #z^-2qinv
vpbroadcastd 4756(%rdx,%rax),%ymm6 #z^-1
vpbroadcastd 4764(%rdx,%rax),%ymm7 #z^-2

xor		%rcx,%rcx
.p2align 5
_looptop_j_2:
vmovdqa		(%rdi),%ymm8   #X
vmovdqa		128(%rdi),%ymm9 #Y
vmovdqa		256(%rdi),%ymm10 #Z

#add
vpaddw      %ymm9,%ymm10,%ymm11   #Y+Z

#mul
vpmullw		%ymm2,%ymm10,%ymm12  #Zw^-1
vpmulhw		%ymm3,%ymm10,%ymm13  #Zw^-1

#reduce
vpmulhw		%ymm0,%ymm12,%ymm12  #Zw^-1
vpsubw		%ymm12,%ymm13,%ymm12 #Zw^-1

#add
vpaddw      %ymm12,%ymm9,%ymm10 #Y + Zw^-1

#mul
vpmullw		%ymm2,%ymm10,%ymm12  #w^-1(Y + Zw^-1)
vpmulhw		%ymm3,%ymm10,%ymm13  #w^-1(Y + Zw^-1)

#reduce
vpmulhw		%ymm0,%ymm12,%ymm12  #w^-1(Y + Zw^-1)
vpsubw		%ymm12,%ymm13,%ymm12 #w^-1(Y + Zw^-1)

#add
vpaddw      %ymm12,%ymm11,%ymm9  #Y + Z + Yw^-1 + Zw^-2
vpsubw      %ymm9,%ymm8,%ymm10   #X + Yw^-2 + Zw^-4
vpaddw      %ymm12,%ymm8,%ymm9   #X + Yw^-1 + Zw^-2
vpaddw      %ymm11,%ymm8,%ymm8   #X + Y + Z

#mul
vpmullw		%ymm4,%ymm9,%ymm11 #z^-1(X + Yw^-1 + Zw^-2)
vpmullw		%ymm5,%ymm10,%ymm12 #z^-2(X + Yw^-2 + Zw^-4)
vpmulhw		%ymm6,%ymm9,%ymm13 #z^-1(X + Yw^-1 + Zw^-2)
vpmulhw		%ymm7,%ymm10,%ymm14 #z^-2(X + Yw^-2 + Zw^-4)

#reduce
vpmulhw		%ymm0,%ymm11,%ymm11
vpmulhw		%ymm0,%ymm12,%ymm12
vpsubw		%ymm11,%ymm13,%ymm9
vpsubw		%ymm12,%ymm14,%ymm10

#reduce2
vpmulhw		%ymm1,%ymm8,%ymm11
vpsraw		$10,%ymm11,%ymm11
vpmullw		%ymm0,%ymm11,%ymm11
vpsubw		%ymm11,%ymm8,%ymm8

#store
vmovdqa		%ymm8,(%rdi)
vmovdqa		%ymm9,128(%rdi)
vmovdqa		%ymm10,256(%rdi)

add		$32,%rdi
add		$32,%rcx
cmp		$128,%rcx
jb		_looptop_j_2

add		$256,%rdi
add     $16,%rax
cmp		$96,%rax
jb		_looptop_start_2

sub		$2304,%rdi

#level1
xor		%rax,%rax
.p2align 5
_looptop_start_1:
#load
vpbroadcastd 4848(%rdx,%rax),%ymm4 #z^-1qinv
vpbroadcastd 4856(%rdx,%rax),%ymm5 #z^-2qinv
vpbroadcastd 4852(%rdx,%rax),%ymm6 #z^-1
vpbroadcastd 4860(%rdx,%rax),%ymm7 #z^-2

xor		%rcx,%rcx
.p2align 5
_looptop_j_1:
vmovdqa		(%rdi),%ymm8   #X
vmovdqa		384(%rdi),%ymm9 #Y
vmovdqa		768(%rdi),%ymm10 #Z

#add
vpaddw %ymm9,%ymm10,%ymm11   #Y+Z

#mul
vpmullw		%ymm2,%ymm10,%ymm12  #Zw^-1
vpmulhw		%ymm3,%ymm10,%ymm13  #Zw^-1

#reduce
vpmulhw		%ymm0,%ymm12,%ymm12  #Zw^-1
vpsubw		%ymm12,%ymm13,%ymm12 #Zw^-1

#add
vpaddw      %ymm12,%ymm9,%ymm10 #Y + Zw^-1

#mul
vpmullw		%ymm2,%ymm10,%ymm12  #w^-1(Y + Zw^-1)
vpmulhw		%ymm3,%ymm10,%ymm13  #w^-1(Y + Zw^-1)

#reduce
vpmulhw		%ymm0,%ymm12,%ymm12  #w^-1(Y + Zw^-1)
vpsubw		%ymm12,%ymm13,%ymm12 #w^-1(Y + Zw^-1)

#add
vpaddw      %ymm12,%ymm11,%ymm9  #Y + Z + Yw^-1 + Zw^-2
vpsubw      %ymm9,%ymm8,%ymm10   #X + Yw^-2 + Zw^-4
vpaddw      %ymm12,%ymm8,%ymm9   #X + Yw^-1 + Zw^-2
vpaddw      %ymm11,%ymm8,%ymm8   #X + Y + Z

#mul
vpmullw		%ymm4,%ymm9,%ymm11 #z^-1(X + Yw^-1 + Zw^-2)
vpmullw		%ymm5,%ymm10,%ymm12 #z^-2(X + Yw^-2 + Zw^-4)
vpmulhw		%ymm6,%ymm9,%ymm13 #z^-1(X + Yw^-1 + Zw^-2)
vpmulhw		%ymm7,%ymm10,%ymm14 #z^-2(X + Yw^-2 + Zw^-4)

#reduce
vpmulhw		%ymm0,%ymm11,%ymm11
vpmulhw		%ymm0,%ymm12,%ymm12
vpsubw		%ymm11,%ymm13,%ymm9
vpsubw		%ymm12,%ymm14,%ymm10

#store
vmovdqa		%ymm8,(%rdi)
vmovdqa		%ymm9,384(%rdi)
vmovdqa		%ymm10,768(%rdi)

add		$32,%rdi
add		$32,%rcx
cmp		$384,%rcx
jb		_looptop_j_1

add		$768,%rdi
add     $16,%rax
cmp		$32,%rax
jb		_looptop_start_1

sub		$2304,%rdi

#level 0
#zetas
vpbroadcastd	4880(%rdx),%ymm2    #(z-z^5)^-1
vpbroadcastd	4884(%rdx),%ymm3   #(z-z^5)^-1

vpbroadcastd	4888(%rdx),%ymm13 
vpbroadcastd	4892(%rdx),%ymm14

vpsllw			$1,%ymm13,%ymm15
vpsllw			$1,%ymm14,%ymm1

xor			%rax,%rax
.p2align 5
_looptop_start_0:
#load
vmovdqa		(%rdi),%ymm4
vmovdqa		32(%rdi),%ymm5
vmovdqa		64(%rdi),%ymm6
vmovdqa		1152(%rdi),%ymm7
vmovdqa		1184(%rdi),%ymm8
vmovdqa		1216(%rdi),%ymm9

#update
vpsubw		%ymm7,%ymm4,%ymm10
vpsubw		%ymm8,%ymm5,%ymm11
vpsubw		%ymm9,%ymm6,%ymm12
vpaddw		%ymm7,%ymm4,%ymm4
vpaddw		%ymm8,%ymm5,%ymm5
vpaddw		%ymm9,%ymm6,%ymm6

#mul
vpmullw		%ymm2,%ymm10,%ymm7
vpmullw		%ymm2,%ymm11,%ymm8
vpmullw		%ymm2,%ymm12,%ymm9
vpmulhw		%ymm3,%ymm10,%ymm10
vpmulhw		%ymm3,%ymm11,%ymm11
vpmulhw		%ymm3,%ymm12,%ymm12

#reduce
vpmulhw		%ymm0,%ymm7,%ymm7
vpmulhw		%ymm0,%ymm8,%ymm8
vpmulhw		%ymm0,%ymm9,%ymm9
vpsubw		%ymm7,%ymm10,%ymm7
vpsubw		%ymm8,%ymm11,%ymm8
vpsubw		%ymm9,%ymm12,%ymm9

#update
vpsubw		%ymm7,%ymm4,%ymm4
vpsubw		%ymm8,%ymm5,%ymm5
vpsubw		%ymm9,%ymm6,%ymm6

#mul
vpmullw		%ymm13,%ymm4,%ymm10
vpmullw		%ymm13,%ymm5,%ymm11
vpmullw		%ymm13,%ymm6,%ymm12
vpmulhw		%ymm14,%ymm4,%ymm4
vpmulhw		%ymm14,%ymm5,%ymm5
vpmulhw		%ymm14,%ymm6,%ymm6

#reduce
vpmulhw		%ymm0,%ymm10,%ymm10
vpmulhw		%ymm0,%ymm11,%ymm11
vpmulhw		%ymm0,%ymm12,%ymm12
vpsubw		%ymm10,%ymm4,%ymm4
vpsubw		%ymm11,%ymm5,%ymm5
vpsubw		%ymm12,%ymm6,%ymm6

#mul
vpmullw		%ymm15,%ymm7,%ymm10
vpmullw		%ymm15,%ymm8,%ymm11
vpmullw		%ymm15,%ymm9,%ymm12
vpmulhw		%ymm1,%ymm7,%ymm7
vpmulhw		%ymm1,%ymm8,%ymm8
vpmulhw		%ymm1,%ymm9,%ymm9

#reduce
vpmulhw		%ymm0,%ymm10,%ymm10
vpmulhw		%ymm0,%ymm11,%ymm11
vpmulhw		%ymm0,%ymm12,%ymm12
vpsubw		%ymm10,%ymm7,%ymm7
vpsubw		%ymm11,%ymm8,%ymm8
vpsubw		%ymm12,%ymm9,%ymm9

#store
vmovdqa		%ymm4,(%rdi)
vmovdqa		%ymm5,32(%rdi)
vmovdqa		%ymm6,64(%rdi)
vmovdqa		%ymm7,1152(%rdi)
vmovdqa		%ymm8,1184(%rdi)
vmovdqa		%ymm9,1216(%rdi)

add		$96,%rdi
add		$96,%rax
cmp		$1152,%rax
jb		_looptop_start_0

ret
