crypto: Provide clmul.h and host accel
Subsystems (2)
v2 cover crypto: Provide clmul.h and host accel
Inspired by Ard Biesheuvel's RFC patches [1] for accelerating carry-less multiply under emulation.
Changes for v2:
* Only accelerate clmul_64; keep generic helpers for other sizes.
* Drop most of the Int128 interfaces, except for clmul_64.
* Use the same acceleration format as aes-round.h.
r~
[1] https://patchew.org/QEMU/[email protected]/
Richard Henderson (18):
crypto: Add generic 8-bit carry-less multiply routines target/arm: Use clmul_8* routines target/s390x: Use clmul_8* routines target/ppc: Use clmul_8* routines
crypto: Add generic 16-bit carry-less multiply routines target/arm: Use clmul_16* routines target/s390x: Use clmul_16* routines target/ppc: Use clmul_16* routines
crypto: Add generic 32-bit carry-less multiply routines target/arm: Use clmul_32* routines target/s390x: Use clmul_32* routines target/ppc: Use clmul_32* routines
crypto: Add generic 64-bit carry-less multiply routine target/arm: Use clmul_64
target/s390x: Use clmul_64
target/ppc: Use clmul_64
host/include/i386: Implement clmul.h host/include/aarch64: Implement clmul.h
host/include/aarch64/host/cpuinfo.h | 1 + host/include/aarch64/host/crypto/clmul.h | 41 +++++ host/include/generic/host/crypto/clmul.h | 15 ++ host/include/i386/host/cpuinfo.h | 1 + host/include/i386/host/crypto/clmul.h | 29 ++++ host/include/x86_64/host/crypto/clmul.h | 1 + include/crypto/clmul.h | 83 ++++++++++ include/qemu/cpuid.h | 3 + target/arm/tcg/vec_internal.h | 11 -- crypto/clmul.c | 112 ++++++++++++++ target/arm/tcg/mve_helper.c | 16 +- target/arm/tcg/vec_helper.c | 102 ++----------- target/ppc/int_helper.c | 64 ++++---- target/s390x/tcg/vec_int_helper.c | 186 ++++++++++------------- util/cpuinfo-aarch64.c | 4 +- util/cpuinfo-i386.c | 1 + crypto/meson.build | 9 +- 17 files changed, 425 insertions(+), 254 deletions(-) create mode 100644 host/include/aarch64/host/crypto/clmul.h create mode 100644 host/include/generic/host/crypto/clmul.h create mode 100644 host/include/i386/host/crypto/clmul.h create mode 100644 host/include/x86_64/host/crypto/clmul.h create mode 100644 include/crypto/clmul.h create mode 100644 crypto/clmul.c
Signature · 1 line
2.34.1
PATCH v2 1/18 crypto: Add generic 8-bit carry-less multiply routines
--- include/crypto/clmul.h | 41 +++++++++++++++++++++++++++++ crypto/clmul.c | 60 ++++++++++++++++++++++++++++++++++++++++++ crypto/meson.build | 9 ++++--- 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 include/crypto/clmul.h create mode 100644 crypto/clmul.c
include/crypto/clmul.h new file +41 -0
@@ -0,0 +1,41 @@ +/* +* Carry-less multiply operations. +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Copyright (C) 2023 Linaro, Ltd. +*/ + +#ifndef CRYPTO_CLMUL_H +#define CRYPTO_CLMUL_H + +/** +* clmul_8x8_low: +* +* Perform eight 8x8->8 carry-less multiplies. +*/ +uint64_t clmul_8x8_low (uint64_t ,uint64_t ); + +/** +* clmul_8x4_even: +* +* Perform four 8x8->16 carry-less multiplies. +* The odd bytes of the inputs are ignored. +*/ +uint64_t clmul_8x4_even (uint64_t ,uint64_t ); + +/** +* clmul_8x4_odd: +* +* Perform four 8x8->16 carry-less multiplies. +* The even bytes of the inputs are ignored. +*/ +uint64_t clmul_8x4_odd (uint64_t ,uint64_t ); + +/** +* clmul_8x4_packed: +* +* Perform four 8x8->16 carry-less multiplies. +*/ +uint64_t clmul_8x4_packed (uint32_t ,uint32_t ); + +#endif /* CRYPTO_CLMUL_H */
crypto/clmul.c new file +60 -0
60 lines not on this page · raw message
crypto/meson.build +6 -3
@@ -48,9 +48,12 @@ if have_afalg endif crypto_ss.add(when: gnutls, if_true: files('tls-cipher-suites.c')) -util_ss.add(files('sm4.c')) -util_ss.add(files('aes.c')) -util_ss.add(files('init.c')) +util_ss.add(files( + 'aes.c', + 'clmul.c', + 'init.c', + 'sm4.c', +)) if gnutls.found() util_ss.add(gnutls) endif
Signature · 1 line
2.34.1
Re: [PATCH v2 01/18] crypto: Add generic 8-bit carry-less multiply routines
Quoted 2 lines · Richard Henderson
Signed-off-by Richard Henderson <[email protected]>
Quoted 144 lines
--- include/crypto/clmul.h | 41 +++++++++++++++++++++++++++++ crypto/clmul.c | 60 ++++++++++++++++++++++++++++++++++++++++++ crypto/meson.build | 9 ++++--- 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 include/crypto/clmul.h create mode 100644 crypto/clmul.cinclude/crypto/clmul.h new file +41
-0@@ -0,0 +1,41 @@ +/* +* Carry-less multiply operations. +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Copyright (C) 2023 Linaro, Ltd. +*/ + +#ifndef CRYPTO_CLMUL_H +#define CRYPTO_CLMUL_H + +/** +* clmul_8x8_low: +* +* Perform eight 8x8->8 carry-less multiplies. +*/ +uint64_t clmul_8x8_low (uint64_t ,uint64_t ); + +/** +* clmul_8x4_even: +* +* Perform four 8x8->16 carry-less multiplies. +* The odd bytes of the inputs are ignored. +*/ +uint64_t clmul_8x4_even (uint64_t ,uint64_t ); + +/** +* clmul_8x4_odd: +* +* Perform four 8x8->16 carry-less multiplies. +* The even bytes of the inputs are ignored. +*/ +uint64_t clmul_8x4_odd (uint64_t ,uint64_t ); + +/** +* clmul_8x4_packed: +* +* Perform four 8x8->16 carry-less multiplies. +*/ +uint64_t clmul_8x4_packed (uint32_t ,uint32_t ); + +#endif /* CRYPTO_CLMUL_H */ crypto/clmul.c new file +60
-0@@ -0,0 +1,60 @@ +/* +* Carry-less multiply operations. +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Copyright (C) 2023 Linaro, Ltd. +*/ + +#include "qemu/osdep.h" +#include "crypto/clmul.h" + +uint64_t clmul_8x8_low (uint64_t n,uint64_t m) +{ +uint64_t r =0 ; + +for (int i =0 ; i <8 ; ++i) { +uint64_t mask = (n &0x0101010101010101ull ) *0xff ; + r ^= m & mask; + m = (m <<1 ) &0xfefefefefefefefeull ; + n >>=1 ; + } +return r; +} + +static uint64_t clmul_8x4_even_int (uint64_t n,uint64_t m) +{ +uint64_t r =0 ; + +for (int i =0 ; i <8 ; ++i) { +uint64_t mask = (n &0x0001000100010001ull ) *0xffff ; + r ^= m & mask; + n >>=1 ; + m <<=1 ; + } +return r; +} + +uint64_t clmul_8x4_even (uint64_t n,uint64_t m) +{ + n &=0x00ff00ff00ff00ffull ; + m &=0x00ff00ff00ff00ffull ; +return clmul_8x4_even_int (n, m); +} + +uint64_t clmul_8x4_odd (uint64_t n,uint64_t m) +{ +return clmul_8x4_even (n >>8 , m >>8 ); +} + +static uint64_t unpack_8_to_16 (uint64_t x) +{ +return (x &0x000000ff ) + | ((x &0x0000ff00 ) <<8 ) + | ((x &0x00ff0000 ) <<16 ) + | ((x &0xff000000 ) <<24 ); +} + +uint64_t clmul_8x4_packed (uint32_t n,uint32_t m) +{ +return clmul_8x4_even_int (unpack_8_to_16 (n),unpack_8_to_16 (m)); +}crypto/meson.build +6
-4@@ -48,9 +48,12 @@ if have_afalg endif crypto_ss.add(when: gnutls, if_true: files('tls-cipher-suites.c')) -util_ss.add(files('sm4.c')) -util_ss.add(files('aes.c')) -util_ss.add(files('init.c')) +util_ss.add(files( + 'aes.c', + 'clmul.c', + 'init.c', + 'sm4.c', +)) if gnutls.found() util_ss.add(gnutls) endif --2.34.1
PATCH v2 2/18 target/arm: Use clmul_8* routines
In reply to Richard HendersonUse generic routines for 8-bit carry-less multiply. Remove our local version of pmull_h.
--- target/arm/tcg/vec_internal.h | 5 ---- target/arm/tcg/mve_helper.c | 8 ++---- target/arm/tcg/vec_helper.c | 53 ++++------------------------------- 3 files changed, 9 insertions(+), 57 deletions(-)
target/arm/tcg/vec_internal.h +0 -5
@@ -219,11 +219,6 @@ int16_t do_sqrdmlah_h(int16_t, int16_t, int16_t, bool, bool, uint32_t *);int32_t do_sqrdmlah_s (int32_t ,int32_t ,int32_t ,bool ,bool ,uint32_t *);int64_t do_sqrdmlah_d (int64_t ,int64_t ,int64_t ,bool ,bool ); -/* -* 8 x 8 -> 16 vector polynomial multiply where the inputs are -* in the low 8 bits of each 16-bit element -*/ -uint64_t pmull_h (uint64_t op1,uint64_t op2);/* * 16 x 16 -> 32 vector polynomial multiply where the inputs are * in the low 16 bits of each 32-bit element
target/arm/tcg/mve_helper.c +3 -5
@@ -26,6 +26,7 @@#include "exec/exec-all.h" #include "tcg/tcg.h" #include "fpu/softfloat.h" +#include "crypto/clmul.h" static uint16_t mve_eci_mask (CPUARMState *env) {
@@ -984,15 +985,12 @@ DO_2OP_L(vmulltuw, 1, 4, uint32_t, 8, uint64_t, DO_MUL)* Polynomial multiply. We can always do this generating 64 bits * of the result at a time, so we don't need to use DO_2OP_L. */ -#define VMULLPH_MASK 0x00ff00ff00ff00ffULL#define VMULLPW_MASK 0x0000ffff0000ffffULL -#define DO_VMULLPBH (N ,M ) pmull_h((N) & VMULLPH_MASK, (M) & VMULLPH_MASK) -#define DO_VMULLPTH (N ,M ) DO_VMULLPBH((N) >> 8, (M) >> 8)#define DO_VMULLPBW (N ,M ) pmull_w((N) & VMULLPW_MASK, (M) & VMULLPW_MASK)#define DO_VMULLPTW (N ,M ) DO_VMULLPBW((N) >> 16, (M) >> 16) -DO_2OP(vmullpbh, 8, uint64_t, DO_VMULLPBH) -DO_2OP(vmullpth, 8, uint64_t, DO_VMULLPTH) +DO_2OP(vmullpbh, 8, uint64_t, clmul_8x4_even) +DO_2OP(vmullpth, 8, uint64_t, clmul_8x4_odd) DO_2OP(vmullpbw, 8, uint64_t, DO_VMULLPBW)DO_2OP (vmullptw,8 , uint64_t,DO_VMULLPTW )
target/arm/tcg/vec_helper.c +6 -47
94 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 02/18] target/arm: Use clmul_8* routines
Quoted 9 lines · Richard Henderson
Use generic routines for 8-bit carry-less multiply. Remove our local version of
pmull_h.Signed-off-by Richard Henderson <[email protected]>--- target/arm/tcg/vec_internal.h | 5 ---- target/arm/tcg/mve_helper.c | 8 ++---- target/arm/tcg/vec_helper.c | 53 ++++------------------------------- 3 files changed, 9 insertions(+), 57 deletions(-)
PATCH v2 3/18 target/s390x: Use clmul_8* routines
In reply to Richard HendersonUse generic routines for 8-bit carry-less multiply. Remove our local version of galois_multiply8.
--- target/s390x/tcg/vec_int_helper.c | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)
target/s390x/tcg/vec_int_helper.c +29 -3
@@ -14,6 +14,7 @@#include "vec.h" #include "exec/helper-proto.h" #include "tcg/tcg-gvec-desc.h" +#include "crypto/clmul.h" static bool s390_vec_is_zero (const S390Vector *v) {
@@ -179,7 +180,6 @@ static uint##TBITS##_t galois_multiply##BITS(uint##TBITS##_t a, \ } \return res; \ } -DEF_GALOIS_MULTIPLY(8, 16) DEF_GALOIS_MULTIPLY(16, 32)DEF_GALOIS_MULTIPLY (32 ,64 )
@@ -203,6 +203,34 @@ static S390Vector galois_multiply64(uint64_t a, uint64_t b)return res; } +/* +* There is no carry across the two doublewords, so their order does +* not matter. Nor is there partial overlap between registers. +*/ +static inline uint64_t do_gfma8 (uint64_t n,uint64_t m,uint64_t a) +{ +return clmul_8x4_even (n, m) ^clmul_8x4_odd (n, m) ^ a; +} + +void HELPER (gvec_vgfm8 )(void *v1,const void *v2,const void *v3,uint32_t d) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3; + + q1[0 ] =do_gfma8 (q2[0 ], q3[0 ],0 ); + q1[1 ] =do_gfma8 (q2[1 ], q3[1 ],0 ); +} + +void HELPER (gvec_vgfma8 )(void *v1,const void *v2,const void *v3, +const void *v4,uint32_t desc) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3, *q4 = v4; + + q1[0 ] =do_gfma8 (q2[0 ], q3[0 ], q4[0 ]); + q1[1 ] =do_gfma8 (q2[1 ], q3[1 ], q4[1 ]); +} +#define DEF_VGFM (BITS ,TBITS ) \ void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \ uint32_t desc) \
@@ -220,7 +248,6 @@ void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \ s390_vec_write_element##TBITS(v1, i, d); \ } \ } -DEF_VGFM(8, 16) DEF_VGFM(16, 32)DEF_VGFM (32 ,64 )
@@ -257,7 +284,6 @@ void HELPER(gvec_vgfma##BITS)(void *v1, const void *v2, const void *v3, \ s390_vec_write_element##TBITS(v1, i, d); \ } \ } -DEF_VGFMA(8, 16) DEF_VGFMA(16, 32)DEF_VGFMA (32 ,64 )
Signature · 1 line
2.34.1
Re: [PATCH v2 03/18] target/s390x: Use clmul_8* routines
Quoted 7 lines · Richard Henderson
Use generic routines for 8-bit carry-less multiply. Remove our local version of
galois_multiply8.Signed-off-by Richard Henderson <[email protected]>--- target/s390x/tcg/vec_int_helper.c | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)
PATCH v2 4/18 target/ppc: Use clmul_8* routines
In reply to Richard HendersonUse generic routines for 8-bit carry-less multiply.
--- target/ppc/int_helper.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
target/ppc/int_helper.c +13 -1
@@ -26,6 +26,7 @@#include "exec/helper-proto.h" #include "crypto/aes.h" #include "crypto/aes-round.h" +#include "crypto/clmul.h" #include "fpu/softfloat.h" #include "qapi/error.h" #include "qemu/guest-random.h"
@@ -1425,6 +1426,18 @@ void helper_vbpermq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)#undef VBPERMQ_INDEX#undef VBPERMQ_DW +/* +* There is no carry across the two doublewords, so their order does +* not matter. Nor is there partial overlap between registers. +*/ +void helper_vpmsumb (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b) +{ +for (int i =0 ; i <2 ; ++i) { +uint64_t aa = a->u64[i], bb = b->u64[i]; + r->u64[i] =clmul_8x4_even (aa, bb) ^clmul_8x4_odd (aa, bb); + } +} +#define PMSUM (name, srcfld, trgfld, trgtyp) \ void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \ { \
@@ -1445,7 +1458,6 @@ void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \ } \ } -PMSUM(vpmsumb, u8, u16, uint16_t) PMSUM(vpmsumh, u16, u32, uint32_t)PMSUM (vpmsumw, u32, u64, uint64_t)
Signature · 1 line
2.34.1
PATCH v2 5/18 crypto: Add generic 16-bit carry-less multiply routines
In reply to Richard Henderson--- include/crypto/clmul.h | 16 ++++++++++++++++ crypto/clmul.c | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+)
include/crypto/clmul.h +16 -0
@@ -38,4 +38,20 @@ uint64_t clmul_8x4_odd(uint64_t, uint64_t);*/ uint64_t clmul_8x4_packed (uint32_t ,uint32_t ); +/** +* clmul_16x2_even: +* +* Perform two 16x16->32 carry-less multiplies. +* The odd words of the inputs are ignored. +*/ +uint64_t clmul_16x2_even (uint64_t ,uint64_t ); + +/** +* clmul_16x2_odd: +* +* Perform two 16x16->32 carry-less multiplies. +* The even bytes of the inputs are ignored. +*/ +uint64_t clmul_16x2_odd (uint64_t ,uint64_t ); +#endif /* CRYPTO_CLMUL_H */
crypto/clmul.c +21 -0
@@ -58,3 +58,24 @@ uint64_t clmul_8x4_packed(uint32_t n, uint32_t m) {return clmul_8x4_even_int (unpack_8_to_16 (n),unpack_8_to_16 (m)); } + +uint64_t clmul_16x2_even (uint64_t n,uint64_t m) +{ +uint64_t r =0 ; + + n &=0x0000ffff0000ffffull ; + m &=0x0000ffff0000ffffull ; + +for (int i =0 ; i <16 ; ++i) { +uint64_t mask = (n &0x0000000100000001ull ) *0xffffffffull ; + r ^= m & mask; + n >>=1 ; + m <<=1 ; + } +return r; +} + +uint64_t clmul_16x2_odd (uint64_t n,uint64_t m) +{ +return clmul_16x2_even (n >>16 , m >>16 ); +}
Signature · 1 line
2.34.1
Re: [PATCH v2 05/18] crypto: Add generic 16-bit carry-less multiply routines
Quoted 2 lines · Richard Henderson
Signed-off-by Richard Henderson <[email protected]>
Quoted 62 lines
--- include/crypto/clmul.h | 16 ++++++++++++++++ crypto/clmul.c | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+)include/crypto/clmul.h +16
-0@@ -38,4 +38,20 @@ uint64_t clmul_8x4_odd(uint64_t, uint64_t);*/ uint64_t clmul_8x4_packed (uint32_t ,uint32_t ); +/** +* clmul_16x2_even: +* +* Perform two 16x16->32 carry-less multiplies. +* The odd words of the inputs are ignored. +*/ +uint64_t clmul_16x2_even (uint64_t ,uint64_t ); + +/** +* clmul_16x2_odd: +* +* Perform two 16x16->32 carry-less multiplies. +* The even bytes of the inputs are ignored. +*/ +uint64_t clmul_16x2_odd (uint64_t ,uint64_t ); +#endif /* CRYPTO_CLMUL_H */ crypto/clmul.c +21
-1@@ -58,3 +58,24 @@ uint64_t clmul_8x4_packed(uint32_t n, uint32_t m) {return clmul_8x4_even_int (unpack_8_to_16 (n),unpack_8_to_16 (m)); } + +uint64_t clmul_16x2_even (uint64_t n,uint64_t m) +{ +uint64_t r =0 ; + + n &=0x0000ffff0000ffffull ; + m &=0x0000ffff0000ffffull ; + +for (int i =0 ; i <16 ; ++i) { +uint64_t mask = (n &0x0000000100000001ull ) *0xffffffffull ; + r ^= m & mask; + n >>=1 ; + m <<=1 ; + } +return r; +} + +uint64_t clmul_16x2_odd (uint64_t n,uint64_t m) +{ +return clmul_16x2_even (n >>16 , m >>16 ); +} --2.34.1
PATCH v2 6/18 target/arm: Use clmul_16* routines
In reply to Richard HendersonUse generic routines for 16-bit carry-less multiply. Remove our local version of pmull_w.
--- target/arm/tcg/vec_internal.h | 6 ------ target/arm/tcg/mve_helper.c | 8 ++------ target/arm/tcg/vec_helper.c | 13 ------------- 3 files changed, 2 insertions(+), 25 deletions(-)
target/arm/tcg/vec_internal.h +0 -6
@@ -219,12 +219,6 @@ int16_t do_sqrdmlah_h(int16_t, int16_t, int16_t, bool, bool, uint32_t *);int32_t do_sqrdmlah_s (int32_t ,int32_t ,int32_t ,bool ,bool ,uint32_t *);int64_t do_sqrdmlah_d (int64_t ,int64_t ,int64_t ,bool ,bool ); -/* -* 16 x 16 -> 32 vector polynomial multiply where the inputs are -* in the low 16 bits of each 32-bit element -*/ -uint64_t pmull_w (uint64_t op1,uint64_t op2); -/** * bfdotadd: * @sum: addend
target/arm/tcg/mve_helper.c +2 -6
@@ -985,14 +985,10 @@ DO_2OP_L(vmulltuw, 1, 4, uint32_t, 8, uint64_t, DO_MUL)* Polynomial multiply. We can always do this generating 64 bits * of the result at a time, so we don't need to use DO_2OP_L. */ -#define VMULLPW_MASK 0x0000ffff0000ffffULL -#define DO_VMULLPBW (N ,M ) pmull_w((N) & VMULLPW_MASK, (M) & VMULLPW_MASK) -#define DO_VMULLPTW (N ,M ) DO_VMULLPBW((N) >> 16, (M) >> 16) - DO_2OP(vmullpbh, 8, uint64_t, clmul_8x4_even) DO_2OP(vmullpth, 8, uint64_t, clmul_8x4_odd) -DO_2OP(vmullpbw, 8, uint64_t, DO_VMULLPBW) -DO_2OP (vmullptw,8 , uint64_t,DO_VMULLPTW ) +DO_2OP(vmullpbw, 8, uint64_t, clmul_16x2_even) +DO_2OP (vmullptw,8 , uint64_t, clmul_16x2_odd)/* * Because the computation type is at least twice as large as required,
target/arm/tcg/vec_helper.c +0 -13
@@ -2029,19 +2029,6 @@ void HELPER(gvec_pmull_q)(void *vd, void *vn, void *vm, uint32_t desc)clear_tail (d, opr_sz,simd_maxsz (desc)); } -uint64_t pmull_w (uint64_t op1,uint64_t op2) -{ -uint64_t result =0 ; -int i; -for (i =0 ; i <16 ; ++i) { -uint64_t mask = (op1 &0x0000000100000001ull ) *0xffffffff ; - result ^= op2 & mask; - op1 >>=1 ; - op2 <<=1 ; - } -return result; -} -void HELPER (neon_pmull_h )(void *vd,void *vn,void *vm,uint32_t desc) {int hi =simd_data (desc);
Signature · 1 line
2.34.1
Re: [PATCH v2 06/18] target/arm: Use clmul_16* routines
Quoted 9 lines · Richard Henderson
Use generic routines for 16-bit carry-less multiply. Remove our local version of
pmull_w.Signed-off-by Richard Henderson <[email protected]>--- target/arm/tcg/vec_internal.h | 6 ------ target/arm/tcg/mve_helper.c | 8 ++------ target/arm/tcg/vec_helper.c | 13 ------------- 3 files changed, 2 insertions(+), 25 deletions(-)
PATCH v2 7/18 target/s390x: Use clmul_16* routines
In reply to Richard HendersonUse generic routines for 16-bit carry-less multiply. Remove our local version of galois_multiply16.
--- target/s390x/tcg/vec_int_helper.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
target/s390x/tcg/vec_int_helper.c +24 -3
@@ -180,7 +180,6 @@ static uint##TBITS##_t galois_multiply##BITS(uint##TBITS##_t a, \ } \return res; \ } -DEF_GALOIS_MULTIPLY(16, 32) DEF_GALOIS_MULTIPLY(32, 64)static S390Vector galois_multiply64 (uint64_t a,uint64_t b)
@@ -231,6 +230,30 @@ void HELPER(gvec_vgfma8)(void *v1, const void *v2, const void *v3, q1[1 ] =do_gfma8 (q2[1 ], q3[1 ], q4[1 ]); } +static inline uint64_t do_gfma16 (uint64_t n,uint64_t m,uint64_t a) +{ +return clmul_16x2_even (n, m) ^clmul_16x2_odd (n, m) ^ a; +} + +void HELPER (gvec_vgfm16 )(void *v1,const void *v2,const void *v3,uint32_t d) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3; + + q1[0 ] =do_gfma16 (q2[0 ], q3[0 ],0 ); + q1[1 ] =do_gfma16 (q2[1 ], q3[1 ],0 ); +} + +void HELPER (gvec_vgfma16 )(void *v1,const void *v2,const void *v3, +const void *v4,uint32_t d) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3, *q4 = v4; + + q1[0 ] =do_gfma16 (q2[0 ], q3[0 ], q4[0 ]); + q1[1 ] =do_gfma16 (q2[1 ], q3[1 ], q4[1 ]); +} +#define DEF_VGFM (BITS ,TBITS ) \ void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \ uint32_t desc) \
@@ -248,7 +271,6 @@ void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \ s390_vec_write_element##TBITS(v1, i, d); \ } \ } -DEF_VGFM(16, 32) DEF_VGFM(32, 64) void HELPER(gvec_vgfm64)(void *v1, const void *v2, const void *v3,
@@ -284,7 +306,6 @@ void HELPER(gvec_vgfma##BITS)(void *v1, const void *v2, const void *v3, \ s390_vec_write_element##TBITS(v1, i, d); \ } \ } -DEF_VGFMA(16, 32) DEF_VGFMA(32, 64) void HELPER(gvec_vgfma64)(void *v1, const void *v2, const void *v3,
Signature · 1 line
2.34.1
Re: [PATCH v2 07/18] target/s390x: Use clmul_16* routines
Quoted 7 lines · Richard Henderson
Use generic routines for 16-bit carry-less multiply. Remove our local version of
galois_multiply16.Signed-off-by Richard Henderson <[email protected]>--- target/s390x/tcg/vec_int_helper.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
PATCH v2 8/18 target/ppc: Use clmul_16* routines
In reply to Richard HendersonUse generic routines for 16-bit carry-less multiply.
--- target/ppc/int_helper.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
target/ppc/int_helper.c +8 -1
@@ -1438,6 +1438,14 @@ void helper_vpmsumb(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) } } +void helper_vpmsumh (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b) +{ +for (int i =0 ; i <2 ; ++i) { +uint64_t aa = a->u64[i], bb = b->u64[i]; + r->u64[i] =clmul_16x2_even (aa, bb) ^clmul_16x2_odd (aa, bb); + } +} +#define PMSUM (name, srcfld, trgfld, trgtyp) \ void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \ { \
@@ -1458,7 +1466,6 @@ void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \ } \ } -PMSUM(vpmsumh, u16, u32, uint32_t) PMSUM(vpmsumw, u32, u64, uint64_t)void helper_VPMSUMD (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b)
Signature · 1 line
2.34.1
PATCH v2 9/18 crypto: Add generic 32-bit carry-less multiply routines
In reply to Richard Henderson--- include/crypto/clmul.h | 7 +++++++ crypto/clmul.c | 13 +++++++++++++ 2 files changed, 20 insertions(+)
include/crypto/clmul.h +7 -0
@@ -54,4 +54,11 @@ uint64_t clmul_16x2_even(uint64_t, uint64_t);*/ uint64_t clmul_16x2_odd (uint64_t ,uint64_t ); +/** +* clmul_32: +* +* Perform a 32x32->64 carry-less multiply. +*/ +uint64_t clmul_32 (uint32_t ,uint32_t ); +#endif /* CRYPTO_CLMUL_H */
crypto/clmul.c +13 -0
@@ -79,3 +79,16 @@ uint64_t clmul_16x2_odd(uint64_t n, uint64_t m) {return clmul_16x2_even (n >>16 , m >>16 ); } + +uint64_t clmul_32 (uint32_t n,uint32_t m32) +{ +uint64_t r =0 ; +uint64_t m = m32; + +for (int i =0 ; i <32 ; ++i) { + r ^= n &1 ? m :0 ; + n >>=1 ; + m <<=1 ; + } +return r; +}
Signature · 1 line
2.34.1
Re: [PATCH v2 09/18] crypto: Add generic 32-bit carry-less multiply routines
Quoted 2 lines · Richard Henderson
Signed-off-by Richard Henderson <[email protected]>
Quoted 45 lines
--- include/crypto/clmul.h | 7 +++++++ crypto/clmul.c | 13 +++++++++++++ 2 files changed, 20 insertions(+)include/crypto/clmul.h +7
-0@@ -54,4 +54,11 @@ uint64_t clmul_16x2_even(uint64_t, uint64_t);*/ uint64_t clmul_16x2_odd (uint64_t ,uint64_t ); +/** +* clmul_32: +* +* Perform a 32x32->64 carry-less multiply. +*/ +uint64_t clmul_32 (uint32_t ,uint32_t ); +#endif /* CRYPTO_CLMUL_H */ crypto/clmul.c +13
-1@@ -79,3 +79,16 @@ uint64_t clmul_16x2_odd(uint64_t n, uint64_t m) {return clmul_16x2_even (n >>16 , m >>16 ); } + +uint64_t clmul_32 (uint32_t n,uint32_t m32) +{ +uint64_t r =0 ; +uint64_t m = m32; + +for (int i =0 ; i <32 ; ++i) { + r ^= n &1 ? m :0 ; + n >>=1 ; + m <<=1 ; + } +return r; +} --2.34.1
PATCH v2 10/18 target/arm: Use clmul_32* routines
In reply to Richard HendersonUse generic routines for 32-bit carry-less multiply. Remove our local version of pmull_d.
--- target/arm/tcg/vec_helper.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
target/arm/tcg/vec_helper.c +1 -13
@@ -2055,18 +2055,6 @@ void HELPER(sve2_pmull_h)(void *vd, void *vn, void *vm, uint32_t desc) } } -static uint64_t pmull_d (uint64_t op1,uint64_t op2) -{ -uint64_t result =0 ; -int i; - -for (i =0 ; i <32 ; ++i) { -uint64_t mask = -((op1 >> i) &1 ); - result ^= (op2 << i) & mask; - } -return result; -} -void HELPER (sve2_pmull_d )(void *vd,void *vn,void *vm,uint32_t desc) {intptr_t sel =H4 (simd_data (desc));
@@ -2075,7 +2063,7 @@ void HELPER(sve2_pmull_d)(void *vd, void *vn, void *vm, uint32_t desc)uint64_t *d = vd;for (i =0 ; i < opr_sz /8 ; ++i) { - d[i] =pmull_d (n[2 * i + sel], m[2 * i + sel]); + d[i] =clmul_32 (n[2 * i + sel], m[2 * i + sel]); } }#endif
Signature · 1 line
2.34.1
Re: [PATCH v2 10/18] target/arm: Use clmul_32* routines
Quoted 7 lines · Richard Henderson
Use generic routines for 32-bit carry-less multiply. Remove our local version of
pmull_d.Signed-off-by Richard Henderson <[email protected]>--- target/arm/tcg/vec_helper.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
PATCH v2 11/18 target/s390x: Use clmul_32* routines
In reply to Richard HendersonUse generic routines for 32-bit carry-less multiply. Remove our local version of galois_multiply32.
--- target/s390x/tcg/vec_int_helper.c | 75 +++++++++---------------------- 1 file changed, 22 insertions(+), 53 deletions(-)
target/s390x/tcg/vec_int_helper.c +22 -53
@@ -165,22 +165,6 @@ DEF_VCTZ(8)DEF_VCTZ (16 ) /* like binary multiplication, but XOR instead of addition */ -#define DEF_GALOIS_MULTIPLY (BITS ,TBITS ) \ -static uint##TBITS##_t galois_multiply##BITS(uint##TBITS##_t a, \ - uint##TBITS##_t b) \ -{ \ - uint##TBITS##_t res = 0; \ - \ - while (b) { \ - if (b & 0x1) { \ - res = res ^ a; \ - } \ - a = a << 1; \ - b = b >> 1; \ - } \ - return res; \ -} -DEF_GALOIS_MULTIPLY(32, 64)static S390Vector galois_multiply64 (uint64_t a,uint64_t b) {
@@ -254,24 +238,29 @@ void HELPER(gvec_vgfma16)(void *v1, const void *v2, const void *v3, q1[1 ] =do_gfma16 (q2[1 ], q3[1 ], q4[1 ]); } -#define DEF_VGFM (BITS ,TBITS ) \ -void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \ - uint32_t desc) \ -{ \ - int i; \ - \ - for (i = 0; i < (128 / TBITS); i++) { \ - uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \ - uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \ - uint##TBITS##_t d = galois_multiply##BITS(a, b); \ - \ - a = s390_vec_read_element##BITS(v2, i * 2 + 1); \ - b = s390_vec_read_element##BITS(v3, i * 2 + 1); \ - d = d ^ galois_multiply32(a, b); \ - s390_vec_write_element##TBITS(v1, i, d); \ - } \ +static inline uint64_t do_gfma32 (uint64_t n,uint64_t m,uint64_t a) +{ +return clmul_32 (n, m) ^clmul_32 (n >>32 , m >>32 ) ^ a; +} + +void HELPER (gvec_vgfm32 )(void *v1,const void *v2,const void *v3,uint32_t d) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3; + + q1[0 ] =do_gfma32 (q2[0 ], q3[0 ],0 ); + q1[1 ] =do_gfma32 (q2[1 ], q3[1 ],0 ); +} + +void HELPER (gvec_vgfma32 )(void *v1,const void *v2,const void *v3, +const void *v4,uint32_t d) +{ +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3, *q4 = v4; + + q1[0 ] =do_gfma32 (q2[0 ], q3[0 ], q4[0 ]); + q1[1 ] =do_gfma32 (q2[1 ], q3[1 ], q4[1 ]); } -DEF_VGFM(32, 64)void HELPER (gvec_vgfm64 )(void *v1,const void *v2,const void *v3,uint32_t desc)
@@ -288,26 +277,6 @@ void HELPER(gvec_vgfm64)(void *v1, const void *v2, const void *v3,s390_vec_xor (v1, &tmp1, &tmp2); } -#define DEF_VGFMA (BITS ,TBITS ) \ -void HELPER(gvec_vgfma##BITS)(void *v1, const void *v2, const void *v3, \ - const void *v4, uint32_t desc) \ -{ \ - int i; \ - \ - for (i = 0; i < (128 / TBITS); i++) { \ - uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \ - uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \ - uint##TBITS##_t d = galois_multiply##BITS(a, b); \ - \ - a = s390_vec_read_element##BITS(v2, i * 2 + 1); \ - b = s390_vec_read_element##BITS(v3, i * 2 + 1); \ - d = d ^ galois_multiply32(a, b); \ - d = d ^ s390_vec_read_element##TBITS(v4, i); \ - s390_vec_write_element##TBITS(v1, i, d); \ - } \ -} -DEF_VGFMA(32, 64) -void HELPER (gvec_vgfma64 )(void *v1,const void *v2,const void *v3,const void *v4,uint32_t desc) {
Signature · 1 line
2.34.1
Re: [PATCH v2 11/18] target/s390x: Use clmul_32* routines
Quoted 7 lines · Richard Henderson
Use generic routines for 32-bit carry-less multiply. Remove our local version of
galois_multiply32.Signed-off-by Richard Henderson <[email protected]>--- target/s390x/tcg/vec_int_helper.c | 75 +++++++++---------------------- 1 file changed, 22 insertions(+), 53 deletions(-)
PATCH v2 12/18 target/ppc: Use clmul_32* routines
In reply to Richard HendersonUse generic routines for 32-bit carry-less multiply.
--- target/ppc/int_helper.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-)
target/ppc/int_helper.c +6 -20
@@ -1446,28 +1446,14 @@ void helper_vpmsumh(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) } } -#define PMSUM (name, srcfld, trgfld, trgtyp) \ -void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \ -{ \ - int i, j; \ - trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])]; \ - \ - VECTOR_FOR_INORDER_I(i, srcfld) { \ - prod[i] = 0; \ - for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) { \ - if (a->srcfld[i] & (1ull << j)) { \ - prod[i] ^= ((trgtyp)b->srcfld[i] << j); \ - } \ - } \ - } \ - \ - VECTOR_FOR_INORDER_I(i, trgfld) { \ - r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1]; \ - } \ +void helper_vpmsumw (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b) +{ +for (int i =0 ; i <2 ; ++i) { +uint64_t aa = a->u64[i], bb = b->u64[i]; + r->u64[i] =clmul_32 (aa, bb) ^clmul_32 (aa >>32 , bb >>32 ); + } } -PMSUM(vpmsumw, u32, u64, uint64_t) -void helper_VPMSUMD (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b) {int i, j;
Signature · 1 line
2.34.1
PATCH v2 13/18 crypto: Add generic 64-bit carry-less multiply routine
In reply to Richard Henderson--- host/include/generic/host/crypto/clmul.h | 15 +++++++++++++++ include/crypto/clmul.h | 19 +++++++++++++++++++ crypto/clmul.c | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 host/include/generic/host/crypto/clmul.h
host/include/generic/host/crypto/clmul.h new file +15 -0
@@ -0,0 +1,15 @@ +/* +* No host specific carry-less multiply acceleration. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef GENERIC_HOST_CRYPTO_CLMUL_H +#define GENERIC_HOST_CRYPTO_CLMUL_H + +#define HAVE_CLMUL_ACCEL false +#define ATTR_CLMUL_ACCEL + +Int128 clmul_64_accel(uint64_t, uint64_t) + QEMU_ERROR("unsupported accel"); + +#endif /* GENERIC_HOST_CRYPTO_CLMUL_H */
include/crypto/clmul.h +19 -0
@@ -8,6 +8,9 @@#ifndef CRYPTO_CLMUL_H #define CRYPTO_CLMUL_H +#include "qemu/int128.h" +#include "host/crypto/clmul.h" +/** * clmul_8x8_low: *
@@ -61,4 +64,20 @@ uint64_t clmul_16x2_odd(uint64_t, uint64_t);*/ uint64_t clmul_32 (uint32_t ,uint32_t ); +/** +* clmul_64: +* +* Perform a 64x64->128 carry-less multiply. +*/ +Int128 clmul_64_gen (uint64_t ,uint64_t ); + +static inline Int128 clmul_64 (uint64_t a,uint64_t b) +{ +if (HAVE_CLMUL_ACCEL ) { +return clmul_64_accel (a, b); + }else { +return clmul_64_gen (a, b); + } +} +#endif /* CRYPTO_CLMUL_H */
crypto/clmul.c +18 -0
@@ -92,3 +92,21 @@ uint64_t clmul_32(uint32_t n, uint32_t m32) }return r; } + +Int128 clmul_64_gen (uint64_t n,uint64_t m) +{ +uint64_t rl =0 , rh =0 ; + +/* Bit 0 can only influence the low 64-bit result. */ +if (n &1 ) { + rl = m; + } + +for (int i =1 ; i <64 ; ++i) { +uint64_t mask = -(n &1 ); + rl ^= (m << i) & mask; + rh ^= (m >> (64 - i)) & mask; + n >>=1 ; + } +return int128_make128 (rl, rh); +}
Signature · 1 line
2.34.1
PATCH v2 14/18 target/arm: Use clmul_64
In reply to Richard HendersonUse generic routine for 64-bit carry-less multiply.
--- target/arm/tcg/vec_helper.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)
target/arm/tcg/vec_helper.c +4 -18
@@ -2003,28 +2003,14 @@ void HELPER(gvec_pmul_b)(void *vd, void *vn, void *vm, uint32_t desc)*/ void HELPER (gvec_pmull_q )(void *vd,void *vn,void *vm,uint32_t desc) { -intptr_t i, j, opr_sz =simd_oprsz (desc); +intptr_t i, opr_sz =simd_oprsz (desc);intptr_t hi =simd_data (desc);uint64_t *d = vd, *n = vn, *m = vm;for (i =0 ; i < opr_sz /8 ; i +=2 ) { -uint64_t nn = n[i + hi]; -uint64_t mm = m[i + hi]; -uint64_t rhi =0 ; -uint64_t rlo =0 ; - -/* Bit 0 can only influence the low 64-bit result. */ -if (nn &1 ) { - rlo = mm; - } - -for (j =1 ; j <64 ; ++j) { -uint64_t mask = -((nn >> j) &1 ); - rlo ^= (mm << j) & mask; - rhi ^= (mm >> (64 - j)) & mask; - } - d[i] = rlo; - d[i +1 ] = rhi; +Int128 r =clmul_64 (n[i + hi], m[i + hi]); + d[i] =int128_getlo (r); + d[i +1 ] =int128_gethi (r); }clear_tail (d, opr_sz,simd_maxsz (desc)); }
Signature · 1 line
2.34.1
Re: [PATCH v2 14/18] target/arm: Use clmul_64
Quoted 6 lines · Richard Henderson
Use generic routine for 64-bit carry-less multiply.
Signed-off-by Richard Henderson <[email protected]>--- target/arm/tcg/vec_helper.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-)
PATCH v2 15/18 target/s390x: Use clmul_64
In reply to Richard HendersonUse the generic routine for 64-bit carry-less multiply. Remove our local version of galois_multiply64.
--- target/s390x/tcg/vec_int_helper.c | 58 +++++++------------------------ 1 file changed, 12 insertions(+), 46 deletions(-)
target/s390x/tcg/vec_int_helper.c +12 -46
@@ -21,13 +21,6 @@ static bool s390_vec_is_zero(const S390Vector *v)return !v->doubleword[0 ] && !v->doubleword[1 ]; } -static void s390_vec_xor (S390Vector *res,const S390Vector *a, -const S390Vector *b) -{ - res->doubleword[0 ] = a->doubleword[0 ] ^ b->doubleword[0 ]; - res->doubleword[1 ] = a->doubleword[1 ] ^ b->doubleword[1 ]; -} -static void s390_vec_and (S390Vector *res,const S390Vector *a,const S390Vector *b) {
@@ -166,26 +159,6 @@ DEF_VCTZ(16)/* like binary multiplication, but XOR instead of addition */ -static S390Vector galois_multiply64 (uint64_t a,uint64_t b) -{ -S390Vector res = {}; -S390Vector va = { - .doubleword[1 ] = a, - }; -S390Vector vb = { - .doubleword[1 ] = b, - }; - -while (!s390_vec_is_zero (&vb)) { -if (vb.doubleword[1 ] &0x1 ) { -s390_vec_xor (&res, &res, &va); - } -s390_vec_shl (&va, &va,1 ); -s390_vec_shr (&vb, &vb,1 ); - } -return res; -} -/* * There is no carry across the two doublewords, so their order does * not matter. Nor is there partial overlap between registers.
@@ -265,32 +238,25 @@ void HELPER(gvec_vgfma32)(void *v1, const void *v2, const void *v3,void HELPER (gvec_vgfm64 )(void *v1,const void *v2,const void *v3,uint32_t desc) { -S390Vector tmp1, tmp2; -uint64_t a, b; +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3; +Int128 r; - a =s390_vec_read_element64 (v2,0 ); - b =s390_vec_read_element64 (v3,0 ); - tmp1 =galois_multiply64 (a, b); - a =s390_vec_read_element64 (v2,1 ); - b =s390_vec_read_element64 (v3,1 ); - tmp2 =galois_multiply64 (a, b); -s390_vec_xor (v1, &tmp1, &tmp2); + r =int128_xor (clmul_64 (q2[0 ], q3[0 ]),clmul_64 (q2[1 ], q3[1 ])); + q1[0 ] =int128_gethi (r); + q1[1 ] =int128_getlo (r); }void HELPER (gvec_vgfma64 )(void *v1,const void *v2,const void *v3,const void *v4,uint32_t desc) { -S390Vector tmp1, tmp2; -uint64_t a, b; +uint64_t *q1 = v1; +const uint64_t *q2 = v2, *q3 = v3, *q4 = v4; +Int128 r; - a =s390_vec_read_element64 (v2,0 ); - b =s390_vec_read_element64 (v3,0 ); - tmp1 =galois_multiply64 (a, b); - a =s390_vec_read_element64 (v2,1 ); - b =s390_vec_read_element64 (v3,1 ); - tmp2 =galois_multiply64 (a, b); -s390_vec_xor (&tmp1, &tmp1, &tmp2); -s390_vec_xor (v1, &tmp1, v4); + r =int128_xor (clmul_64 (q2[0 ], q3[0 ]),clmul_64 (q2[1 ], q3[1 ])); + q1[0 ] = q4[0 ] ^int128_gethi (r); + q1[1 ] = q4[1 ] ^int128_getlo (r); }#define DEF_VMAL (BITS ) \
Signature · 1 line
2.34.1
Re: [PATCH v2 15/18] target/s390x: Use clmul_64
Quoted 7 lines · Richard Henderson
Use the generic routine for 64-bit carry-less multiply. Remove our local version of
galois_multiply64.Signed-off-by Richard Henderson <[email protected]>--- target/s390x/tcg/vec_int_helper.c | 58 +++++++------------------------ 1 file changed, 12 insertions(+), 46 deletions(-)
PATCH v2 16/18 target/ppc: Use clmul_64
In reply to Richard HendersonUse generic routine for 64-bit carry-less multiply.
--- target/ppc/int_helper.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)
target/ppc/int_helper.c +3 -14
@@ -1456,20 +1456,9 @@ void helper_vpmsumw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)void helper_VPMSUMD (ppc_avr_t *r,ppc_avr_t *a,ppc_avr_t *b) { -int i, j; -Int128 tmp, prod[2 ] = {int128_zero (),int128_zero ()}; - -for (j =0 ; j <64 ; j++) { -for (i =0 ; i <ARRAY_SIZE (r->u64); i++) { -if (a->VsrD (i) & (1ull << j)) { - tmp =int128_make64 (b->VsrD (i)); - tmp =int128_lshift (tmp, j); - prod[i] =int128_xor (prod[i], tmp); - } - } - } - - r->s128 =int128_xor (prod[0 ], prod[1 ]); +Int128 e =clmul_64 (a->u64[0 ], b->u64[0 ]); +Int128 o =clmul_64 (a->u64[1 ], b->u64[1 ]); + r->s128 =int128_xor (e, o); }#if HOST_BIG_ENDIAN
Signature · 1 line
2.34.1
Re: [PATCH v2 16/18] target/ppc: Use clmul_64
Quoted 6 lines · Richard Henderson
Use generic routine for 64-bit carry-less multiply.
Signed-off-by Richard Henderson <[email protected]>--- target/ppc/int_helper.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)
PATCH v2 17/18 host/include/i386: Implement clmul.h
In reply to Richard HendersonDetect PCLMUL in cpuinfo; implement the accel hook.
--- host/include/i386/host/cpuinfo.h | 1 + host/include/i386/host/crypto/clmul.h | 29 +++++++++++++++++++++++++ host/include/x86_64/host/crypto/clmul.h | 1 + include/qemu/cpuid.h | 3 +++ util/cpuinfo-i386.c | 1 + 5 files changed, 35 insertions(+) create mode 100644 host/include/i386/host/crypto/clmul.h create mode 100644 host/include/x86_64/host/crypto/clmul.h
host/include/i386/host/cpuinfo.h +1 -0
@@ -27,6 +27,7 @@#define CPUINFO_ATOMIC_VMOVDQA (1u << 16)#define CPUINFO_ATOMIC_VMOVDQU (1u << 17)#define CPUINFO_AES (1u << 18) +#define CPUINFO_PCLMUL (1u << 19)/* Initialized with a constructor. */ extern unsigned cpuinfo;
host/include/i386/host/crypto/clmul.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* x86 specific clmul acceleration. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef X86_HOST_CRYPTO_CLMUL_H +#define X86_HOST_CRYPTO_CLMUL_H + +#include "host/cpuinfo.h" +#include <immintrin.h> + +#if defined(__PCLMUL__) +# define HAVE_CLMUL_ACCEL true +# define ATTR_CLMUL_ACCEL +#else +# define HAVE_CLMUL_ACCEL likely(cpuinfo & CPUINFO_PCLMUL) +# define ATTR_CLMUL_ACCEL __attribute__((target("pclmul"))) +#endif + +static inline Int128 ATTR_CLMUL_ACCEL +clmul_64_accel (uint64_t n,uint64_t m) +{ +union {__m128i v;Int128 s; } u; + + u.v =_mm_clmulepi64_si128 (_mm_set_epi64x (0 , n),_mm_set_epi64x (0 , m),0 ); +return u.s; +} + +#endif /* X86_HOST_CRYPTO_CLMUL_H */
host/include/x86_64/host/crypto/clmul.h new file +1 -0
@@ -0,0 +1 @@ +#include "host/include/i386/host/crypto/clmul.h"
include/qemu/cpuid.h +3 -0
@@ -25,6 +25,9 @@#endif /* Leaf 1, %ecx */ +#ifndef bit_PCLMUL +#define bit_PCLMUL (1 << 1) +#endif #ifndef bit_SSE4_1#define bit_SSE4_1 (1 << 19)#endif
util/cpuinfo-i386.c +1 -0
@@ -39,6 +39,7 @@ unsigned __attribute__((constructor)) cpuinfo_init(void) info |= (c & bit_SSE4_1 ?CPUINFO_SSE4 :0 ); info |= (c & bit_MOVBE ?CPUINFO_MOVBE :0 ); info |= (c & bit_POPCNT ?CPUINFO_POPCNT :0 ); + info |= (c & bit_PCLMUL ?CPUINFO_PCLMUL :0 );/* Our AES support requires PSHUFB as well. */ info |= ((c & bit_AES) && (c & bit_SSSE3) ?CPUINFO_AES :0 );
Signature · 1 line
2.34.1
PATCH v2 18/18 host/include/aarch64: Implement clmul.h
In reply to Richard HendersonDetect PMULL in cpuinfo; implement the accel hook.
--- host/include/aarch64/host/cpuinfo.h | 1 + host/include/aarch64/host/crypto/clmul.h | 41 ++++++++++++++++++++++++ util/cpuinfo-aarch64.c | 4 ++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 host/include/aarch64/host/crypto/clmul.h
host/include/aarch64/host/cpuinfo.h +1 -0
@@ -10,6 +10,7 @@#define CPUINFO_LSE (1u << 1)#define CPUINFO_LSE2 (1u << 2)#define CPUINFO_AES (1u << 3) +#define CPUINFO_PMULL (1u << 4)/* Initialized with a constructor. */ extern unsigned cpuinfo;
host/include/aarch64/host/crypto/clmul.h new file +41 -0
41 lines not on this page · raw message
util/cpuinfo-aarch64.c +3 -1
@@ -56,12 +56,14 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)unsigned long hwcap =qemu_getauxval (AT_HWCAP ); info |= (hwcap &HWCAP_ATOMICS ?CPUINFO_LSE :0 ); info |= (hwcap &HWCAP_USCAT ?CPUINFO_LSE2 :0 ); - info |= (hwcap &HWCAP_AES ?CPUINFO_AES :0 ); + info |= (hwcap &HWCAP_AES ?CPUINFO_AES :0 ); + info |= (hwcap &HWCAP_PMULL ?CPUINFO_PMULL :0 );#endif #ifdef CONFIG_DARWIN info |=sysctl_for_bool ("hw.optional.arm.FEAT_LSE" ) *CPUINFO_LSE ; info |=sysctl_for_bool ("hw.optional.arm.FEAT_LSE2" ) *CPUINFO_LSE2 ; info |=sysctl_for_bool ("hw.optional.arm.FEAT_AES" ) *CPUINFO_AES ; + info |=sysctl_for_bool ("hw.optional.arm.FEAT_PMULL" ) *CPUINFO_PMULL ;#endif cpuinfo = info;
Signature · 1 line
2.34.1
Quoted 26 lines · Richard Henderson
Inspired by Ard Biesheuvel's RFC patches [1] for accelerating carry-less multiply under emulation.
Changes for v2:
* Only accelerateclmul_64; keep generic helpers for other sizes.
* Drop most of the Int128 interfaces, except forclmul_64.
* Use the same acceleration format asaes-round.h.r~
[1] https://patchew.org/QEMU/[email protected]/
Richard Henderson (18):
crypto: Add generic 8-bit carry-less multiply routines target/arm: Use clmul_8* routines target/s390x: Use clmul_8* routines target/ppc: Use clmul_8* routines
crypto: Add generic 16-bit carry-less multiply routines target/arm: Use clmul_16* routines target/s390x: Use clmul_16* routines target/ppc: Use clmul_16* routines
crypto: Add generic 32-bit carry-less multiply routines target/arm: Use clmul_32* routines
Quoted 9 lines
target/s390x: Use clmul_32* routines target/ppc: Use clmul_32* routines
crypto: Add generic 64-bit carry-less multiply routine target/arm: Useclmul_64
target/s390x: Useclmul_64
target/ppc: Useclmul_64
host/include/i386: Implementclmul.hhost/include/aarch64: Implementclmul.h
I didn't re-run the OpenSSL benchmark, but the x86 Linux kernel still passes all its crypto selftests when running under TCG emulation on a TX2 arm64 host, so
for the series.
Thanks,
Ard.
Quoted 21 lines · Ard Biesheuvel
Quoted 11 lines
Richard Henderson (18):
crypto: Add generic 8-bit carry-less multiply routines target/arm: Use clmul_8* routines target/s390x: Use clmul_8* routines target/ppc: Use clmul_8* routines
crypto: Add generic 16-bit carry-less multiply routines target/arm: Use clmul_16* routines target/s390x: Use clmul_16* routines target/ppc: Use clmul_16* routines
crypto: Add generic 32-bit carry-less multiply routines target/arm: Use clmul_32* routinesQuoted 9 lines
target/s390x: Use clmul_32* routines target/ppc: Use clmul_32* routines
crypto: Add generic 64-bit carry-less multiply routine target/arm: Useclmul_64
target/s390x: Useclmul_64
target/ppc: Useclmul_64
host/include/i386: Implementclmul.hhost/include/aarch64: Implementclmul.h
Quoted 5 lines
I didn't re-run the OpenSSL benchmark, but the x86 Linux kernel still passes all its crypto selftests when running under TCG emulation on a TX2 arm64 host, so
Tested-by Ard Biesheuvel <[email protected]>
Oh, whoops. What's missing here? Any target/i386 changes.
r~
Quoted 23 lines · Richard Henderson
Quoted 21 lines · Ard Biesheuvel
Quoted 20 lines
Richard Henderson (18):
crypto: Add generic 8-bit carry-less multiply routines target/arm: Use clmul_8* routines target/s390x: Use clmul_8* routines target/ppc: Use clmul_8* routines
crypto: Add generic 16-bit carry-less multiply routines target/arm: Use clmul_16* routines target/s390x: Use clmul_16* routines target/ppc: Use clmul_16* routines
crypto: Add generic 32-bit carry-less multiply routines target/arm: Use clmul_32* routines target/s390x: Use clmul_32* routines target/ppc: Use clmul_32* routines
crypto: Add generic 64-bit carry-less multiply routine target/arm: Useclmul_64
target/s390x: Useclmul_64
target/ppc: Useclmul_64
host/include/i386: Implementclmul.hhost/include/aarch64: Implementclmul.h
Quoted 8 lines
Quoted 5 lines
I didn't re-run the OpenSSL benchmark, but the x86 Linux kernel still passes all its crypto selftests when running under TCG emulation on a TX2 arm64 host, so
Tested-by Ard Biesheuvel <[email protected]>Oh, whoops. What's missing here? Any target/i386 changes.
Ah yes - I hadn't spotted that. The below seems to do the trick.
target/i386/ops_sse.h +0 -0
@@ -2156,7 +2156,10 @@ void glue(helper_pclmulqdq, SUFFIX)(CPUX86State
*env, Reg *d, Reg *v, Reg *s,
for (i = 0; i < 1 << SHIFT; i += 2) {
a = v->Q(((ctrl & 1) != 0) + i);
b = s->Q(((ctrl & 16) != 0) + i);
- clmulq(&d->Q(i), &d->Q(i + 1), a, b);
+
+ Int128 r = clmul_64(a, b);
+ d->Q(i) = int128_getlo(r);
+ d->Q(i + 1) = int128_gethi(r);
}}
[and the #include added and clmulq() dropped]
I did a quick RFC4106 benchmark with tcrypt (which doesn't speed up as much as OpenSSL but it is a bit of a hassle cross-rebuilding that)
no acceleration:
tcrypt: test 7 (160 bit key, 8192 byte blocks): 1547 operations in 1 seconds (12673024 bytes)
AES only:
tcrypt: test 7 (160 bit key, 8192 byte blocks): 1679 operations in 1 seconds (13754368 bytes)
AES and PMULL
tcrypt: test 7 (160 bit key, 8192 byte blocks): 3298 operations in 1 seconds (27017216 bytes)