Use arm64 sha256 intrinsics in libmd

Summary:
When running on a CPU that supports the arm64 sha256 intrinsics use them
to improve perfromance of sha256 calculations.

With this changethe following improvement has been seen on an Apple M1
with FreeBS running under Parallels, with similar results on a
Neoverse-N1 r3p1.

x sha256.orig
+ sha256.arm64
+--------------------------------------------------------------------+
|++                                                               x x|
|+++                                                              xxx|
||A                                                               |A||
+--------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x   5          3.41           3.5          3.46         3.458   0.042661458
+   5          0.47          0.54           0.5         0.504   0.027018512
Difference at 95.0% confidence
        -2.954 +/- 0.0520768
        -85.4251% +/- 0.826831%
        (Student's t, pooled s = 0.0357071)

Reviewed by:	cem
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D31284
This commit is contained in:
Andrew Turner 2021-07-23 10:14:03 +01:00
parent 89c085b899
commit 69d8dc20be
4 changed files with 173 additions and 1 deletions

View File

@ -129,6 +129,11 @@ skein_block.o skein_block.pico: Makefile
.if exists(${MACHINE_ARCH}/sha.S) || exists(${MACHINE_ARCH}/rmd160.S) || exists(${MACHINE_ARCH}/skein_block_asm.S)
ACFLAGS+= -DELF -Wa,--noexecstack
.endif
.if ${MACHINE_CPUARCH} == "aarch64"
SRCS+= sha256c_arm64.c
CFLAGS+= -DARM64_SHA2
CFLAGS.sha256c_arm64.c+= -march=armv8-a+crypto
.endif
.endif # ${USE_ASM_SOURCES} != 0
md4hl.c: mdXhl.c

View File

@ -38,6 +38,12 @@ __FBSDID("$FreeBSD$");
#include "sha224.h"
#include "sha256.h"
#include "sha256c_impl.h"
#if defined(ARM64_SHA2)
#include <sys/auxv.h>
#include <machine/ifunc.h>
#endif
#if BYTE_ORDER == BIG_ENDIAN
@ -132,7 +138,7 @@ static const uint32_t K[64] = {
* the 512-bit input block to produce a new state.
*/
static void
SHA256_Transform(uint32_t * state, const unsigned char block[64])
SHA256_Transform_c(uint32_t * state, const unsigned char block[64])
{
uint32_t W[64];
uint32_t S[8];
@ -188,6 +194,33 @@ SHA256_Transform(uint32_t * state, const unsigned char block[64])
state[i] += S[i];
}
#if defined(ARM64_SHA2)
static void
SHA256_Transform_arm64(uint32_t * state, const unsigned char block[64])
{
SHA256_Transform_arm64_impl(state, block, K);
}
DEFINE_UIFUNC(static, void, SHA256_Transform,
(uint32_t * state, const unsigned char block[64]))
{
u_long hwcap;
if (elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)) == 0) {
if ((hwcap & HWCAP_SHA2) != 0)
return (SHA256_Transform_arm64);
}
return (SHA256_Transform_c);
}
#else
static void
SHA256_Transform(uint32_t * state, const unsigned char block[64])
{
SHA256_Transform_c(state, block);
}
#endif
static unsigned char PAD[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@ -0,0 +1,97 @@
/*-
* Copyright (c) 2021 The FreeBSD Foundation
*
* This software was developed by Andrew Turner under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <arm_neon.h>
#include "sha256c_impl.h"
void __hidden
SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64],
const uint32_t K[64])
{
uint32x4_t W[4];
uint32x4_t S[2];
uint32x4_t S_start[2];
uint32x4_t K_tmp, S_tmp;
int i;
#define A64_LOAD_W(x) \
W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16])); \
W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x])))
/* 1. Prepare the first part of the message schedule W. */
A64_LOAD_W(0);
A64_LOAD_W(1);
A64_LOAD_W(2);
A64_LOAD_W(3);
/* 2. Initialize working variables. */
S[0] = vld1q_u32(&state[0]);
S[1] = vld1q_u32(&state[4]);
S_start[0] = S[0];
S_start[1] = S[1];
/* 3. Mix. */
for (i = 0; i < 64; i += 16) {
#define A64_RNDr(i, ii) \
K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4])); \
S_tmp = vsha256hq_u32(S[0], S[1], K_tmp); \
S[1] = vsha256h2q_u32(S[1], S[0], K_tmp); \
S[0] = S_tmp
A64_RNDr(0, i);
A64_RNDr(1, i);
A64_RNDr(2, i);
A64_RNDr(3, i);
if (i == 48)
break;
#define A64_MSCH(x) \
W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]); \
W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4])
A64_MSCH(0);
A64_MSCH(1);
A64_MSCH(2);
A64_MSCH(3);
}
/* 4. Mix local working variables into global state */
S[0] = vaddq_u32(S[0], S_start[0]);
S[1] = vaddq_u32(S[1], S_start[1]);
vst1q_u32(&state[0], S[0]);
vst1q_u32(&state[4], S[1]);
}

View File

@ -0,0 +1,37 @@
/*-
* Copyright (c) 2021 The FreeBSD Foundation
*
* This software was developed by Andrew Turner under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SHA256_IMPL_H_
#define _SHA256_IMPL_H_
#if defined(ARM64_SHA2)
void SHA256_Transform_arm64_impl(uint32_t * state,
const unsigned char block[64], const uint32_t K[64]);
#endif
#endif /* !_SHA256_IMPL_H_ */