18c2ecf20Sopenharmony_ci########################################################################
28c2ecf20Sopenharmony_ci# Implement fast SHA-512 with AVX2 instructions. (x86_64)
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# Copyright (C) 2013 Intel Corporation.
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Authors:
78c2ecf20Sopenharmony_ci#     James Guilford <james.guilford@intel.com>
88c2ecf20Sopenharmony_ci#     Kirk Yap <kirk.s.yap@intel.com>
98c2ecf20Sopenharmony_ci#     David Cote <david.m.cote@intel.com>
108c2ecf20Sopenharmony_ci#     Tim Chen <tim.c.chen@linux.intel.com>
118c2ecf20Sopenharmony_ci#
128c2ecf20Sopenharmony_ci# This software is available to you under a choice of one of two
138c2ecf20Sopenharmony_ci# licenses.  You may choose to be licensed under the terms of the GNU
148c2ecf20Sopenharmony_ci# General Public License (GPL) Version 2, available from the file
158c2ecf20Sopenharmony_ci# COPYING in the main directory of this source tree, or the
168c2ecf20Sopenharmony_ci# OpenIB.org BSD license below:
178c2ecf20Sopenharmony_ci#
188c2ecf20Sopenharmony_ci#     Redistribution and use in source and binary forms, with or
198c2ecf20Sopenharmony_ci#     without modification, are permitted provided that the following
208c2ecf20Sopenharmony_ci#     conditions are met:
218c2ecf20Sopenharmony_ci#
228c2ecf20Sopenharmony_ci#      - Redistributions of source code must retain the above
238c2ecf20Sopenharmony_ci#        copyright notice, this list of conditions and the following
248c2ecf20Sopenharmony_ci#        disclaimer.
258c2ecf20Sopenharmony_ci#
268c2ecf20Sopenharmony_ci#      - Redistributions in binary form must reproduce the above
278c2ecf20Sopenharmony_ci#        copyright notice, this list of conditions and the following
288c2ecf20Sopenharmony_ci#        disclaimer in the documentation and/or other materials
298c2ecf20Sopenharmony_ci#        provided with the distribution.
308c2ecf20Sopenharmony_ci#
318c2ecf20Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
328c2ecf20Sopenharmony_ci# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
338c2ecf20Sopenharmony_ci# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
348c2ecf20Sopenharmony_ci# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
358c2ecf20Sopenharmony_ci# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
368c2ecf20Sopenharmony_ci# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
378c2ecf20Sopenharmony_ci# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
388c2ecf20Sopenharmony_ci# SOFTWARE.
398c2ecf20Sopenharmony_ci#
408c2ecf20Sopenharmony_ci########################################################################
418c2ecf20Sopenharmony_ci#
428c2ecf20Sopenharmony_ci# This code is described in an Intel White-Paper:
438c2ecf20Sopenharmony_ci# "Fast SHA-512 Implementations on Intel Architecture Processors"
448c2ecf20Sopenharmony_ci#
458c2ecf20Sopenharmony_ci# To find it, surf to http://www.intel.com/p/en_US/embedded
468c2ecf20Sopenharmony_ci# and search for that title.
478c2ecf20Sopenharmony_ci#
488c2ecf20Sopenharmony_ci########################################################################
498c2ecf20Sopenharmony_ci# This code schedules 1 blocks at a time, with 4 lanes per block
508c2ecf20Sopenharmony_ci########################################################################
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#include <linux/linkage.h>
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci.text
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci# Virtual Registers
578c2ecf20Sopenharmony_ciY_0 = %ymm4
588c2ecf20Sopenharmony_ciY_1 = %ymm5
598c2ecf20Sopenharmony_ciY_2 = %ymm6
608c2ecf20Sopenharmony_ciY_3 = %ymm7
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciYTMP0 = %ymm0
638c2ecf20Sopenharmony_ciYTMP1 = %ymm1
648c2ecf20Sopenharmony_ciYTMP2 = %ymm2
658c2ecf20Sopenharmony_ciYTMP3 = %ymm3
668c2ecf20Sopenharmony_ciYTMP4 = %ymm8
678c2ecf20Sopenharmony_ciXFER  = YTMP0
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciBYTE_FLIP_MASK  = %ymm9
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci# 1st arg is %rdi, which is saved to the stack and accessed later via %r12
728c2ecf20Sopenharmony_ciCTX1        = %rdi
738c2ecf20Sopenharmony_ciCTX2        = %r12
748c2ecf20Sopenharmony_ci# 2nd arg
758c2ecf20Sopenharmony_ciINP         = %rsi
768c2ecf20Sopenharmony_ci# 3rd arg
778c2ecf20Sopenharmony_ciNUM_BLKS    = %rdx
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cic           = %rcx
808c2ecf20Sopenharmony_cid           = %r8
818c2ecf20Sopenharmony_cie           = %rdx
828c2ecf20Sopenharmony_ciy3          = %rsi
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciTBL   = %rdi # clobbers CTX1
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cia     = %rax
878c2ecf20Sopenharmony_cib     = %rbx
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cif     = %r9
908c2ecf20Sopenharmony_cig     = %r10
918c2ecf20Sopenharmony_cih     = %r11
928c2ecf20Sopenharmony_ciold_h = %r11
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciT1    = %r12 # clobbers CTX2
958c2ecf20Sopenharmony_ciy0    = %r13
968c2ecf20Sopenharmony_ciy1    = %r14
978c2ecf20Sopenharmony_ciy2    = %r15
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci# Local variables (stack frame)
1008c2ecf20Sopenharmony_ciXFER_SIZE = 4*8
1018c2ecf20Sopenharmony_ciSRND_SIZE = 1*8
1028c2ecf20Sopenharmony_ciINP_SIZE = 1*8
1038c2ecf20Sopenharmony_ciINPEND_SIZE = 1*8
1048c2ecf20Sopenharmony_ciCTX_SIZE = 1*8
1058c2ecf20Sopenharmony_ciRSPSAVE_SIZE = 1*8
1068c2ecf20Sopenharmony_ciGPRSAVE_SIZE = 5*8
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciframe_XFER = 0
1098c2ecf20Sopenharmony_ciframe_SRND = frame_XFER + XFER_SIZE
1108c2ecf20Sopenharmony_ciframe_INP = frame_SRND + SRND_SIZE
1118c2ecf20Sopenharmony_ciframe_INPEND = frame_INP + INP_SIZE
1128c2ecf20Sopenharmony_ciframe_CTX = frame_INPEND + INPEND_SIZE
1138c2ecf20Sopenharmony_ciframe_RSPSAVE = frame_CTX + CTX_SIZE
1148c2ecf20Sopenharmony_ciframe_GPRSAVE = frame_RSPSAVE + RSPSAVE_SIZE
1158c2ecf20Sopenharmony_ciframe_size = frame_GPRSAVE + GPRSAVE_SIZE
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci## assume buffers not aligned
1188c2ecf20Sopenharmony_ci#define	VMOVDQ vmovdqu
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci# addm [mem], reg
1218c2ecf20Sopenharmony_ci# Add reg to mem using reg-mem add and store
1228c2ecf20Sopenharmony_ci.macro addm p1 p2
1238c2ecf20Sopenharmony_ci	add	\p1, \p2
1248c2ecf20Sopenharmony_ci	mov	\p2, \p1
1258c2ecf20Sopenharmony_ci.endm
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci# COPY_YMM_AND_BSWAP ymm, [mem], byte_flip_mask
1298c2ecf20Sopenharmony_ci# Load ymm with mem and byte swap each dword
1308c2ecf20Sopenharmony_ci.macro COPY_YMM_AND_BSWAP p1 p2 p3
1318c2ecf20Sopenharmony_ci	VMOVDQ \p2, \p1
1328c2ecf20Sopenharmony_ci	vpshufb \p3, \p1, \p1
1338c2ecf20Sopenharmony_ci.endm
1348c2ecf20Sopenharmony_ci# rotate_Ys
1358c2ecf20Sopenharmony_ci# Rotate values of symbols Y0...Y3
1368c2ecf20Sopenharmony_ci.macro rotate_Ys
1378c2ecf20Sopenharmony_ci	Y_ = Y_0
1388c2ecf20Sopenharmony_ci	Y_0 = Y_1
1398c2ecf20Sopenharmony_ci	Y_1 = Y_2
1408c2ecf20Sopenharmony_ci	Y_2 = Y_3
1418c2ecf20Sopenharmony_ci	Y_3 = Y_
1428c2ecf20Sopenharmony_ci.endm
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci# RotateState
1458c2ecf20Sopenharmony_ci.macro RotateState
1468c2ecf20Sopenharmony_ci	# Rotate symbols a..h right
1478c2ecf20Sopenharmony_ci	old_h  = h
1488c2ecf20Sopenharmony_ci	TMP_   = h
1498c2ecf20Sopenharmony_ci	h      = g
1508c2ecf20Sopenharmony_ci	g      = f
1518c2ecf20Sopenharmony_ci	f      = e
1528c2ecf20Sopenharmony_ci	e      = d
1538c2ecf20Sopenharmony_ci	d      = c
1548c2ecf20Sopenharmony_ci	c      = b
1558c2ecf20Sopenharmony_ci	b      = a
1568c2ecf20Sopenharmony_ci	a      = TMP_
1578c2ecf20Sopenharmony_ci.endm
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci# macro MY_VPALIGNR	YDST, YSRC1, YSRC2, RVAL
1608c2ecf20Sopenharmony_ci# YDST = {YSRC1, YSRC2} >> RVAL*8
1618c2ecf20Sopenharmony_ci.macro MY_VPALIGNR YDST YSRC1 YSRC2 RVAL
1628c2ecf20Sopenharmony_ci	vperm2f128      $0x3, \YSRC2, \YSRC1, \YDST     # YDST = {YS1_LO, YS2_HI}
1638c2ecf20Sopenharmony_ci	vpalignr        $\RVAL, \YSRC2, \YDST, \YDST    # YDST = {YDS1, YS2} >> RVAL*8
1648c2ecf20Sopenharmony_ci.endm
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci.macro FOUR_ROUNDS_AND_SCHED
1678c2ecf20Sopenharmony_ci################################### RND N + 0 #########################################
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	# Extract w[t-7]
1708c2ecf20Sopenharmony_ci	MY_VPALIGNR	YTMP0, Y_3, Y_2, 8		# YTMP0 = W[-7]
1718c2ecf20Sopenharmony_ci	# Calculate w[t-16] + w[t-7]
1728c2ecf20Sopenharmony_ci	vpaddq		Y_0, YTMP0, YTMP0		# YTMP0 = W[-7] + W[-16]
1738c2ecf20Sopenharmony_ci	# Extract w[t-15]
1748c2ecf20Sopenharmony_ci	MY_VPALIGNR	YTMP1, Y_1, Y_0, 8		# YTMP1 = W[-15]
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	# Calculate sigma0
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	# Calculate w[t-15] ror 1
1798c2ecf20Sopenharmony_ci	vpsrlq		$1, YTMP1, YTMP2
1808c2ecf20Sopenharmony_ci	vpsllq		$(64-1), YTMP1, YTMP3
1818c2ecf20Sopenharmony_ci	vpor		YTMP2, YTMP3, YTMP3		# YTMP3 = W[-15] ror 1
1828c2ecf20Sopenharmony_ci	# Calculate w[t-15] shr 7
1838c2ecf20Sopenharmony_ci	vpsrlq		$7, YTMP1, YTMP4		# YTMP4 = W[-15] >> 7
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
1868c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
1878c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
1888c2ecf20Sopenharmony_ci	add	frame_XFER(%rsp),h		# h = k + w + h         # --
1898c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
1908c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
1918c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
1948c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
1958c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
1988c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
1998c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
2008c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
2038c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
2048c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
2078c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
2088c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
2098c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
2128c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
2138c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
2188c2ecf20Sopenharmony_ci	add	y3, h		# h = t1 + S0 + MAJ                     # --
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	RotateState
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci################################### RND N + 1 #########################################
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	# Calculate w[t-15] ror 8
2258c2ecf20Sopenharmony_ci	vpsrlq		$8, YTMP1, YTMP2
2268c2ecf20Sopenharmony_ci	vpsllq		$(64-8), YTMP1, YTMP1
2278c2ecf20Sopenharmony_ci	vpor		YTMP2, YTMP1, YTMP1		# YTMP1 = W[-15] ror 8
2288c2ecf20Sopenharmony_ci	# XOR the three components
2298c2ecf20Sopenharmony_ci	vpxor		YTMP4, YTMP3, YTMP3		# YTMP3 = W[-15] ror 1 ^ W[-15] >> 7
2308c2ecf20Sopenharmony_ci	vpxor		YTMP1, YTMP3, YTMP1		# YTMP1 = s0
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	# Add three components, w[t-16], w[t-7] and sigma0
2348c2ecf20Sopenharmony_ci	vpaddq		YTMP1, YTMP0, YTMP0		# YTMP0 = W[-16] + W[-7] + s0
2358c2ecf20Sopenharmony_ci	# Move to appropriate lanes for calculating w[16] and w[17]
2368c2ecf20Sopenharmony_ci	vperm2f128	$0x0, YTMP0, YTMP0, Y_0		# Y_0 = W[-16] + W[-7] + s0 {BABA}
2378c2ecf20Sopenharmony_ci	# Move to appropriate lanes for calculating w[18] and w[19]
2388c2ecf20Sopenharmony_ci	vpand		MASK_YMM_LO(%rip), YTMP0, YTMP0	# YTMP0 = W[-16] + W[-7] + s0 {DC00}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	# Calculate w[16] and w[17] in both 128 bit lanes
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	# Calculate sigma1 for w[16] and w[17] on both 128 bit lanes
2438c2ecf20Sopenharmony_ci	vperm2f128	$0x11, Y_3, Y_3, YTMP2		# YTMP2 = W[-2] {BABA}
2448c2ecf20Sopenharmony_ci	vpsrlq		$6, YTMP2, YTMP4		# YTMP4 = W[-2] >> 6 {BABA}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
2488c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
2498c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
2508c2ecf20Sopenharmony_ci	add	1*8+frame_XFER(%rsp), h		# h = k + w + h         # --
2518c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
2558c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
2568c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
2578c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
2618c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
2628c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
2638c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
2648c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
2678c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
2708c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
2738c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
2748c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
2758c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
2788c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
2818c2ecf20Sopenharmony_ci	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
2828c2ecf20Sopenharmony_ci	add	y3, h		# h = t1 + S0 + MAJ                     # --
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	RotateState
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci################################### RND N + 2 #########################################
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	vpsrlq		$19, YTMP2, YTMP3		# YTMP3 = W[-2] >> 19 {BABA}
2908c2ecf20Sopenharmony_ci	vpsllq		$(64-19), YTMP2, YTMP1		# YTMP1 = W[-2] << 19 {BABA}
2918c2ecf20Sopenharmony_ci	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 19 {BABA}
2928c2ecf20Sopenharmony_ci	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {BABA}
2938c2ecf20Sopenharmony_ci	vpsrlq		$61, YTMP2, YTMP3		# YTMP3 = W[-2] >> 61 {BABA}
2948c2ecf20Sopenharmony_ci	vpsllq		$(64-61), YTMP2, YTMP1		# YTMP1 = W[-2] << 61 {BABA}
2958c2ecf20Sopenharmony_ci	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 61 {BABA}
2968c2ecf20Sopenharmony_ci	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = s1 = (W[-2] ror 19) ^
2978c2ecf20Sopenharmony_ci							#  (W[-2] ror 61) ^ (W[-2] >> 6) {BABA}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	# Add sigma1 to the other compunents to get w[16] and w[17]
3008c2ecf20Sopenharmony_ci	vpaddq		YTMP4, Y_0, Y_0			# Y_0 = {W[1], W[0], W[1], W[0]}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	# Calculate sigma1 for w[18] and w[19] for upper 128 bit lane
3038c2ecf20Sopenharmony_ci	vpsrlq		$6, Y_0, YTMP4			# YTMP4 = W[-2] >> 6 {DC--}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
3068c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
3078c2ecf20Sopenharmony_ci	add	2*8+frame_XFER(%rsp), h		# h = k + w + h         # --
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
3108c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
3118c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
3128c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
3158c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
3168c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
3198c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
3208c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
3238c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
3248c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
3278c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
3308c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
3318c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
3328c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
3358c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
3368c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
3378c2ecf20Sopenharmony_ci	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	add	y3, h		# h = t1 + S0 + MAJ                     # --
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	RotateState
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci################################### RND N + 3 #########################################
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	vpsrlq		$19, Y_0, YTMP3			# YTMP3 = W[-2] >> 19 {DC--}
3468c2ecf20Sopenharmony_ci	vpsllq		$(64-19), Y_0, YTMP1		# YTMP1 = W[-2] << 19 {DC--}
3478c2ecf20Sopenharmony_ci	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 19 {DC--}
3488c2ecf20Sopenharmony_ci	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {DC--}
3498c2ecf20Sopenharmony_ci	vpsrlq		$61, Y_0, YTMP3			# YTMP3 = W[-2] >> 61 {DC--}
3508c2ecf20Sopenharmony_ci	vpsllq		$(64-61), Y_0, YTMP1		# YTMP1 = W[-2] << 61 {DC--}
3518c2ecf20Sopenharmony_ci	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 61 {DC--}
3528c2ecf20Sopenharmony_ci	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = s1 = (W[-2] ror 19) ^
3538c2ecf20Sopenharmony_ci							#  (W[-2] ror 61) ^ (W[-2] >> 6) {DC--}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	# Add the sigma0 + w[t-7] + w[t-16] for w[18] and w[19]
3568c2ecf20Sopenharmony_ci	# to newly calculated sigma1 to get w[18] and w[19]
3578c2ecf20Sopenharmony_ci	vpaddq		YTMP4, YTMP0, YTMP2		# YTMP2 = {W[3], W[2], --, --}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	# Form w[19, w[18], w17], w[16]
3608c2ecf20Sopenharmony_ci	vpblendd		$0xF0, YTMP2, Y_0, Y_0		# Y_0 = {W[3], W[2], W[1], W[0]}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
3638c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
3648c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
3658c2ecf20Sopenharmony_ci	add	3*8+frame_XFER(%rsp), h		# h = k + w + h         # --
3668c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
3708c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
3718c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
3728c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
3768c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
3778c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
3788c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
3818c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
3848c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
3878c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
3928c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
3938c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
3948c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
3978c2ecf20Sopenharmony_ci	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
3988c2ecf20Sopenharmony_ci	add	y3, h		# h = t1 + S0 + MAJ                     # --
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	RotateState
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	rotate_Ys
4038c2ecf20Sopenharmony_ci.endm
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci.macro DO_4ROUNDS
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci################################### RND N + 0 #########################################
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
4108c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
4118c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
4128c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
4158c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
4168c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
4198c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
4208c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
4218c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
4228c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
4258c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
4268c2ecf20Sopenharmony_ci	add	frame_XFER(%rsp), h		# h = k + w + h         # --
4278c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
4308c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
4318c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
4328c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
4338c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
4368c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
4378c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	RotateState
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci################################### RND N + 1 #########################################
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
4468c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
4478c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
4488c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
4498c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
4528c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
4538c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
4548c2ecf20Sopenharmony_ci	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
4578c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
4588c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
4598c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
4608c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
4638c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
4648c2ecf20Sopenharmony_ci	add	8*1+frame_XFER(%rsp), h		# h = k + w + h         # --
4658c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
4688c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
4698c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
4708c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
4718c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
4748c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
4758c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	RotateState
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci################################### RND N + 2 #########################################
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
4848c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
4858c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
4868c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
4878c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
4908c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
4918c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
4928c2ecf20Sopenharmony_ci	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
4958c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
4968c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
4978c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
4988c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
5018c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
5028c2ecf20Sopenharmony_ci	add	8*2+frame_XFER(%rsp), h		# h = k + w + h         # --
5038c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
5068c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
5078c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
5088c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
5098c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
5128c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
5138c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	RotateState
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci################################### RND N + 3 #########################################
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
5228c2ecf20Sopenharmony_ci	mov	f, y2		# y2 = f                                # CH
5238c2ecf20Sopenharmony_ci	rorx	$41, e, y0	# y0 = e >> 41				# S1A
5248c2ecf20Sopenharmony_ci	rorx	$18, e, y1	# y1 = e >> 18				# S1B
5258c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = f^g                              # CH
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
5288c2ecf20Sopenharmony_ci	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
5298c2ecf20Sopenharmony_ci	and	e, y2		# y2 = (f^g)&e                          # CH
5308c2ecf20Sopenharmony_ci	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
5338c2ecf20Sopenharmony_ci	rorx	$34, a, T1	# T1 = a >> 34				# S0B
5348c2ecf20Sopenharmony_ci	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
5358c2ecf20Sopenharmony_ci	rorx	$39, a, y1	# y1 = a >> 39				# S0A
5368c2ecf20Sopenharmony_ci	mov	a, y3		# y3 = a                                # MAJA
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
5398c2ecf20Sopenharmony_ci	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
5408c2ecf20Sopenharmony_ci	add	8*3+frame_XFER(%rsp), h		# h = k + w + h         # --
5418c2ecf20Sopenharmony_ci	or	c, y3		# y3 = a|c                              # MAJA
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
5448c2ecf20Sopenharmony_ci	mov	a, T1		# T1 = a                                # MAJB
5458c2ecf20Sopenharmony_ci	and	b, y3		# y3 = (a|c)&b                          # MAJA
5468c2ecf20Sopenharmony_ci	and	c, T1		# T1 = a&c                              # MAJB
5478c2ecf20Sopenharmony_ci	add	y0, y2		# y2 = S1 + CH                          # --
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	add	h, d		# d = k + w + h + d                     # --
5518c2ecf20Sopenharmony_ci	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
5528c2ecf20Sopenharmony_ci	add	y1, h		# h = k + w + h + S0                    # --
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	add	y3, h		# h = t1 + S0 + MAJ                     # --
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	RotateState
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci.endm
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci########################################################################
5658c2ecf20Sopenharmony_ci# void sha512_transform_rorx(sha512_state *state, const u8 *data, int blocks)
5668c2ecf20Sopenharmony_ci# Purpose: Updates the SHA512 digest stored at "state" with the message
5678c2ecf20Sopenharmony_ci# stored in "data".
5688c2ecf20Sopenharmony_ci# The size of the message pointed to by "data" must be an integer multiple
5698c2ecf20Sopenharmony_ci# of SHA512 message blocks.
5708c2ecf20Sopenharmony_ci# "blocks" is the message length in SHA512 blocks
5718c2ecf20Sopenharmony_ci########################################################################
5728c2ecf20Sopenharmony_ciSYM_FUNC_START(sha512_transform_rorx)
5738c2ecf20Sopenharmony_ci	# Allocate Stack Space
5748c2ecf20Sopenharmony_ci	mov	%rsp, %rax
5758c2ecf20Sopenharmony_ci	sub	$frame_size, %rsp
5768c2ecf20Sopenharmony_ci	and	$~(0x20 - 1), %rsp
5778c2ecf20Sopenharmony_ci	mov	%rax, frame_RSPSAVE(%rsp)
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	# Save GPRs
5808c2ecf20Sopenharmony_ci	mov	%rbx, 8*0+frame_GPRSAVE(%rsp)
5818c2ecf20Sopenharmony_ci	mov	%r12, 8*1+frame_GPRSAVE(%rsp)
5828c2ecf20Sopenharmony_ci	mov	%r13, 8*2+frame_GPRSAVE(%rsp)
5838c2ecf20Sopenharmony_ci	mov	%r14, 8*3+frame_GPRSAVE(%rsp)
5848c2ecf20Sopenharmony_ci	mov	%r15, 8*4+frame_GPRSAVE(%rsp)
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	shl	$7, NUM_BLKS	# convert to bytes
5878c2ecf20Sopenharmony_ci	jz	done_hash
5888c2ecf20Sopenharmony_ci	add	INP, NUM_BLKS	# pointer to end of data
5898c2ecf20Sopenharmony_ci	mov	NUM_BLKS, frame_INPEND(%rsp)
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	## load initial digest
5928c2ecf20Sopenharmony_ci	mov	8*0(CTX1), a
5938c2ecf20Sopenharmony_ci	mov	8*1(CTX1), b
5948c2ecf20Sopenharmony_ci	mov	8*2(CTX1), c
5958c2ecf20Sopenharmony_ci	mov	8*3(CTX1), d
5968c2ecf20Sopenharmony_ci	mov	8*4(CTX1), e
5978c2ecf20Sopenharmony_ci	mov	8*5(CTX1), f
5988c2ecf20Sopenharmony_ci	mov	8*6(CTX1), g
5998c2ecf20Sopenharmony_ci	mov	8*7(CTX1), h
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	# save %rdi (CTX) before it gets clobbered
6028c2ecf20Sopenharmony_ci	mov	%rdi, frame_CTX(%rsp)
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	vmovdqa	PSHUFFLE_BYTE_FLIP_MASK(%rip), BYTE_FLIP_MASK
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ciloop0:
6078c2ecf20Sopenharmony_ci	lea	K512(%rip), TBL
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	## byte swap first 16 dwords
6108c2ecf20Sopenharmony_ci	COPY_YMM_AND_BSWAP	Y_0, (INP), BYTE_FLIP_MASK
6118c2ecf20Sopenharmony_ci	COPY_YMM_AND_BSWAP	Y_1, 1*32(INP), BYTE_FLIP_MASK
6128c2ecf20Sopenharmony_ci	COPY_YMM_AND_BSWAP	Y_2, 2*32(INP), BYTE_FLIP_MASK
6138c2ecf20Sopenharmony_ci	COPY_YMM_AND_BSWAP	Y_3, 3*32(INP), BYTE_FLIP_MASK
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	mov	INP, frame_INP(%rsp)
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	## schedule 64 input dwords, by doing 12 rounds of 4 each
6188c2ecf20Sopenharmony_ci	movq	$4, frame_SRND(%rsp)
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci.align 16
6218c2ecf20Sopenharmony_ciloop1:
6228c2ecf20Sopenharmony_ci	vpaddq	(TBL), Y_0, XFER
6238c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6248c2ecf20Sopenharmony_ci	FOUR_ROUNDS_AND_SCHED
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	vpaddq	1*32(TBL), Y_0, XFER
6278c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6288c2ecf20Sopenharmony_ci	FOUR_ROUNDS_AND_SCHED
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	vpaddq	2*32(TBL), Y_0, XFER
6318c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6328c2ecf20Sopenharmony_ci	FOUR_ROUNDS_AND_SCHED
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	vpaddq	3*32(TBL), Y_0, XFER
6358c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6368c2ecf20Sopenharmony_ci	add	$(4*32), TBL
6378c2ecf20Sopenharmony_ci	FOUR_ROUNDS_AND_SCHED
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	subq	$1, frame_SRND(%rsp)
6408c2ecf20Sopenharmony_ci	jne	loop1
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	movq	$2, frame_SRND(%rsp)
6438c2ecf20Sopenharmony_ciloop2:
6448c2ecf20Sopenharmony_ci	vpaddq	(TBL), Y_0, XFER
6458c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6468c2ecf20Sopenharmony_ci	DO_4ROUNDS
6478c2ecf20Sopenharmony_ci	vpaddq	1*32(TBL), Y_1, XFER
6488c2ecf20Sopenharmony_ci	vmovdqa XFER, frame_XFER(%rsp)
6498c2ecf20Sopenharmony_ci	add	$(2*32), TBL
6508c2ecf20Sopenharmony_ci	DO_4ROUNDS
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	vmovdqa	Y_2, Y_0
6538c2ecf20Sopenharmony_ci	vmovdqa	Y_3, Y_1
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	subq	$1, frame_SRND(%rsp)
6568c2ecf20Sopenharmony_ci	jne	loop2
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	mov	frame_CTX(%rsp), CTX2
6598c2ecf20Sopenharmony_ci	addm	8*0(CTX2), a
6608c2ecf20Sopenharmony_ci	addm	8*1(CTX2), b
6618c2ecf20Sopenharmony_ci	addm	8*2(CTX2), c
6628c2ecf20Sopenharmony_ci	addm	8*3(CTX2), d
6638c2ecf20Sopenharmony_ci	addm	8*4(CTX2), e
6648c2ecf20Sopenharmony_ci	addm	8*5(CTX2), f
6658c2ecf20Sopenharmony_ci	addm	8*6(CTX2), g
6668c2ecf20Sopenharmony_ci	addm	8*7(CTX2), h
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	mov	frame_INP(%rsp), INP
6698c2ecf20Sopenharmony_ci	add	$128, INP
6708c2ecf20Sopenharmony_ci	cmp	frame_INPEND(%rsp), INP
6718c2ecf20Sopenharmony_ci	jne	loop0
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cidone_hash:
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci# Restore GPRs
6768c2ecf20Sopenharmony_ci	mov	8*0+frame_GPRSAVE(%rsp), %rbx
6778c2ecf20Sopenharmony_ci	mov	8*1+frame_GPRSAVE(%rsp), %r12
6788c2ecf20Sopenharmony_ci	mov	8*2+frame_GPRSAVE(%rsp), %r13
6798c2ecf20Sopenharmony_ci	mov	8*3+frame_GPRSAVE(%rsp), %r14
6808c2ecf20Sopenharmony_ci	mov	8*4+frame_GPRSAVE(%rsp), %r15
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	# Restore Stack Pointer
6838c2ecf20Sopenharmony_ci	mov	frame_RSPSAVE(%rsp), %rsp
6848c2ecf20Sopenharmony_ci	RET
6858c2ecf20Sopenharmony_ciSYM_FUNC_END(sha512_transform_rorx)
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci########################################################################
6888c2ecf20Sopenharmony_ci### Binary Data
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci# Mergeable 640-byte rodata section. This allows linker to merge the table
6928c2ecf20Sopenharmony_ci# with other, exactly the same 640-byte fragment of another rodata section
6938c2ecf20Sopenharmony_ci# (if such section exists).
6948c2ecf20Sopenharmony_ci.section	.rodata.cst640.K512, "aM", @progbits, 640
6958c2ecf20Sopenharmony_ci.align 64
6968c2ecf20Sopenharmony_ci# K[t] used in SHA512 hashing
6978c2ecf20Sopenharmony_ciK512:
6988c2ecf20Sopenharmony_ci	.quad	0x428a2f98d728ae22,0x7137449123ef65cd
6998c2ecf20Sopenharmony_ci	.quad	0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
7008c2ecf20Sopenharmony_ci	.quad	0x3956c25bf348b538,0x59f111f1b605d019
7018c2ecf20Sopenharmony_ci	.quad	0x923f82a4af194f9b,0xab1c5ed5da6d8118
7028c2ecf20Sopenharmony_ci	.quad	0xd807aa98a3030242,0x12835b0145706fbe
7038c2ecf20Sopenharmony_ci	.quad	0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
7048c2ecf20Sopenharmony_ci	.quad	0x72be5d74f27b896f,0x80deb1fe3b1696b1
7058c2ecf20Sopenharmony_ci	.quad	0x9bdc06a725c71235,0xc19bf174cf692694
7068c2ecf20Sopenharmony_ci	.quad	0xe49b69c19ef14ad2,0xefbe4786384f25e3
7078c2ecf20Sopenharmony_ci	.quad	0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
7088c2ecf20Sopenharmony_ci	.quad	0x2de92c6f592b0275,0x4a7484aa6ea6e483
7098c2ecf20Sopenharmony_ci	.quad	0x5cb0a9dcbd41fbd4,0x76f988da831153b5
7108c2ecf20Sopenharmony_ci	.quad	0x983e5152ee66dfab,0xa831c66d2db43210
7118c2ecf20Sopenharmony_ci	.quad	0xb00327c898fb213f,0xbf597fc7beef0ee4
7128c2ecf20Sopenharmony_ci	.quad	0xc6e00bf33da88fc2,0xd5a79147930aa725
7138c2ecf20Sopenharmony_ci	.quad	0x06ca6351e003826f,0x142929670a0e6e70
7148c2ecf20Sopenharmony_ci	.quad	0x27b70a8546d22ffc,0x2e1b21385c26c926
7158c2ecf20Sopenharmony_ci	.quad	0x4d2c6dfc5ac42aed,0x53380d139d95b3df
7168c2ecf20Sopenharmony_ci	.quad	0x650a73548baf63de,0x766a0abb3c77b2a8
7178c2ecf20Sopenharmony_ci	.quad	0x81c2c92e47edaee6,0x92722c851482353b
7188c2ecf20Sopenharmony_ci	.quad	0xa2bfe8a14cf10364,0xa81a664bbc423001
7198c2ecf20Sopenharmony_ci	.quad	0xc24b8b70d0f89791,0xc76c51a30654be30
7208c2ecf20Sopenharmony_ci	.quad	0xd192e819d6ef5218,0xd69906245565a910
7218c2ecf20Sopenharmony_ci	.quad	0xf40e35855771202a,0x106aa07032bbd1b8
7228c2ecf20Sopenharmony_ci	.quad	0x19a4c116b8d2d0c8,0x1e376c085141ab53
7238c2ecf20Sopenharmony_ci	.quad	0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
7248c2ecf20Sopenharmony_ci	.quad	0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
7258c2ecf20Sopenharmony_ci	.quad	0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
7268c2ecf20Sopenharmony_ci	.quad	0x748f82ee5defb2fc,0x78a5636f43172f60
7278c2ecf20Sopenharmony_ci	.quad	0x84c87814a1f0ab72,0x8cc702081a6439ec
7288c2ecf20Sopenharmony_ci	.quad	0x90befffa23631e28,0xa4506cebde82bde9
7298c2ecf20Sopenharmony_ci	.quad	0xbef9a3f7b2c67915,0xc67178f2e372532b
7308c2ecf20Sopenharmony_ci	.quad	0xca273eceea26619c,0xd186b8c721c0c207
7318c2ecf20Sopenharmony_ci	.quad	0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
7328c2ecf20Sopenharmony_ci	.quad	0x06f067aa72176fba,0x0a637dc5a2c898a6
7338c2ecf20Sopenharmony_ci	.quad	0x113f9804bef90dae,0x1b710b35131c471b
7348c2ecf20Sopenharmony_ci	.quad	0x28db77f523047d84,0x32caab7b40c72493
7358c2ecf20Sopenharmony_ci	.quad	0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
7368c2ecf20Sopenharmony_ci	.quad	0x4cc5d4becb3e42b6,0x597f299cfc657e2a
7378c2ecf20Sopenharmony_ci	.quad	0x5fcb6fab3ad6faec,0x6c44198c4a475817
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci.section	.rodata.cst32.PSHUFFLE_BYTE_FLIP_MASK, "aM", @progbits, 32
7408c2ecf20Sopenharmony_ci.align 32
7418c2ecf20Sopenharmony_ci# Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
7428c2ecf20Sopenharmony_ciPSHUFFLE_BYTE_FLIP_MASK:
7438c2ecf20Sopenharmony_ci	.octa 0x08090a0b0c0d0e0f0001020304050607
7448c2ecf20Sopenharmony_ci	.octa 0x18191a1b1c1d1e1f1011121314151617
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci.section	.rodata.cst32.MASK_YMM_LO, "aM", @progbits, 32
7478c2ecf20Sopenharmony_ci.align 32
7488c2ecf20Sopenharmony_ciMASK_YMM_LO:
7498c2ecf20Sopenharmony_ci	.octa 0x00000000000000000000000000000000
7508c2ecf20Sopenharmony_ci	.octa 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
751