Path: blob/main/sys/contrib/openzfs/module/zcommon/zfs_fletcher_avx512.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (C) 2016 Gvozden Nešković. All rights reserved.23*/2425#if defined(__x86_64) && defined(HAVE_AVX512F)2627#include <sys/byteorder.h>28#include <sys/frame.h>29#include <sys/spa_checksum.h>30#include <sys/string.h>31#include <sys/simd.h>32#include <zfs_fletcher.h>3334#ifdef __linux__35#define __asm __asm__ __volatile__36#endif3738static void39fletcher_4_avx512f_init(fletcher_4_ctx_t *ctx)40{41memset(ctx->avx512, 0, 4 * sizeof (zfs_fletcher_avx512_t));42}4344static void45fletcher_4_avx512f_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)46{47static const uint64_t48CcA[] = { 0, 0, 1, 3, 6, 10, 15, 21 },49CcB[] = { 28, 36, 44, 52, 60, 68, 76, 84 },50DcA[] = { 0, 0, 0, 1, 4, 10, 20, 35 },51DcB[] = { 56, 84, 120, 164, 216, 276, 344, 420 },52DcC[] = { 448, 512, 576, 640, 704, 768, 832, 896 };5354uint64_t A, B, C, D;55uint64_t i;5657A = ctx->avx512[0].v[0];58B = 8 * ctx->avx512[1].v[0];59C = 64 * ctx->avx512[2].v[0] - CcB[0] * ctx->avx512[1].v[0];60D = 512 * ctx->avx512[3].v[0] - DcC[0] * ctx->avx512[2].v[0] +61DcB[0] * ctx->avx512[1].v[0];6263for (i = 1; i < 8; i++) {64A += ctx->avx512[0].v[i];65B += 8 * ctx->avx512[1].v[i] - i * ctx->avx512[0].v[i];66C += 64 * ctx->avx512[2].v[i] - CcB[i] * ctx->avx512[1].v[i] +67CcA[i] * ctx->avx512[0].v[i];68D += 512 * ctx->avx512[3].v[i] - DcC[i] * ctx->avx512[2].v[i] +69DcB[i] * ctx->avx512[1].v[i] - DcA[i] * ctx->avx512[0].v[i];70}7172ZIO_SET_CHECKSUM(zcp, A, B, C, D);73}7475#define FLETCHER_4_AVX512_RESTORE_CTX(ctx) \76{ \77__asm("vmovdqu64 %0, %%zmm0" :: "m" ((ctx)->avx512[0])); \78__asm("vmovdqu64 %0, %%zmm1" :: "m" ((ctx)->avx512[1])); \79__asm("vmovdqu64 %0, %%zmm2" :: "m" ((ctx)->avx512[2])); \80__asm("vmovdqu64 %0, %%zmm3" :: "m" ((ctx)->avx512[3])); \81}8283#define FLETCHER_4_AVX512_SAVE_CTX(ctx) \84{ \85__asm("vmovdqu64 %%zmm0, %0" : "=m" ((ctx)->avx512[0])); \86__asm("vmovdqu64 %%zmm1, %0" : "=m" ((ctx)->avx512[1])); \87__asm("vmovdqu64 %%zmm2, %0" : "=m" ((ctx)->avx512[2])); \88__asm("vmovdqu64 %%zmm3, %0" : "=m" ((ctx)->avx512[3])); \89}9091static void92fletcher_4_avx512f_native(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size)93{94const uint32_t *ip = buf;95const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);9697FLETCHER_4_AVX512_RESTORE_CTX(ctx);9899do {100__asm("vpmovzxdq %0, %%zmm4"::"m" (*ip));101__asm("vpaddq %zmm4, %zmm0, %zmm0");102__asm("vpaddq %zmm0, %zmm1, %zmm1");103__asm("vpaddq %zmm1, %zmm2, %zmm2");104__asm("vpaddq %zmm2, %zmm3, %zmm3");105} while ((ip += 8) < ipend);106107FLETCHER_4_AVX512_SAVE_CTX(ctx);108}109STACK_FRAME_NON_STANDARD(fletcher_4_avx512f_native);110111static void112fletcher_4_avx512f_byteswap(fletcher_4_ctx_t *ctx, const void *buf,113uint64_t size)114{115static const uint64_t byteswap_mask = 0xFFULL;116const uint32_t *ip = buf;117const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);118119FLETCHER_4_AVX512_RESTORE_CTX(ctx);120121__asm("vpbroadcastq %0, %%zmm8" :: "r" (byteswap_mask));122__asm("vpsllq $8, %zmm8, %zmm9");123__asm("vpsllq $16, %zmm8, %zmm10");124__asm("vpsllq $24, %zmm8, %zmm11");125126do {127__asm("vpmovzxdq %0, %%zmm5"::"m" (*ip));128129__asm("vpsrlq $24, %zmm5, %zmm6");130__asm("vpandd %zmm8, %zmm6, %zmm6");131__asm("vpsrlq $8, %zmm5, %zmm7");132__asm("vpandd %zmm9, %zmm7, %zmm7");133__asm("vpord %zmm6, %zmm7, %zmm4");134__asm("vpsllq $8, %zmm5, %zmm6");135__asm("vpandd %zmm10, %zmm6, %zmm6");136__asm("vpord %zmm6, %zmm4, %zmm4");137__asm("vpsllq $24, %zmm5, %zmm5");138__asm("vpandd %zmm11, %zmm5, %zmm5");139__asm("vpord %zmm5, %zmm4, %zmm4");140141__asm("vpaddq %zmm4, %zmm0, %zmm0");142__asm("vpaddq %zmm0, %zmm1, %zmm1");143__asm("vpaddq %zmm1, %zmm2, %zmm2");144__asm("vpaddq %zmm2, %zmm3, %zmm3");145} while ((ip += 8) < ipend);146147FLETCHER_4_AVX512_SAVE_CTX(ctx)148}149STACK_FRAME_NON_STANDARD(fletcher_4_avx512f_byteswap);150151static boolean_t152fletcher_4_avx512f_valid(void)153{154return (kfpu_allowed() && zfs_avx512f_available());155}156157const fletcher_4_ops_t fletcher_4_avx512f_ops = {158.init_native = fletcher_4_avx512f_init,159.fini_native = fletcher_4_avx512f_fini,160.compute_native = fletcher_4_avx512f_native,161.init_byteswap = fletcher_4_avx512f_init,162.fini_byteswap = fletcher_4_avx512f_fini,163.compute_byteswap = fletcher_4_avx512f_byteswap,164.valid = fletcher_4_avx512f_valid,165.uses_fpu = B_TRUE,166.name = "avx512f"167};168169#if defined(HAVE_AVX512BW)170static void171fletcher_4_avx512bw_byteswap(fletcher_4_ctx_t *ctx, const void *buf,172uint64_t size)173{174static const zfs_fletcher_avx512_t mask = {175.v = { 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,1760xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,1770xFFFFFFFF00010203, 0xFFFFFFFF08090A0B,1780xFFFFFFFF00010203, 0xFFFFFFFF08090A0B }179};180const uint32_t *ip = buf;181const uint32_t *ipend = (uint32_t *)((uint8_t *)ip + size);182183FLETCHER_4_AVX512_RESTORE_CTX(ctx);184185__asm("vmovdqu64 %0, %%zmm5" :: "m" (mask));186187do {188__asm("vpmovzxdq %0, %%zmm4"::"m" (*ip));189190__asm("vpshufb %zmm5, %zmm4, %zmm4");191192__asm("vpaddq %zmm4, %zmm0, %zmm0");193__asm("vpaddq %zmm0, %zmm1, %zmm1");194__asm("vpaddq %zmm1, %zmm2, %zmm2");195__asm("vpaddq %zmm2, %zmm3, %zmm3");196} while ((ip += 8) < ipend);197198FLETCHER_4_AVX512_SAVE_CTX(ctx)199}200STACK_FRAME_NON_STANDARD(fletcher_4_avx512bw_byteswap);201202static boolean_t203fletcher_4_avx512bw_valid(void)204{205return (fletcher_4_avx512f_valid() && zfs_avx512bw_available());206}207208const fletcher_4_ops_t fletcher_4_avx512bw_ops = {209.init_native = fletcher_4_avx512f_init,210.fini_native = fletcher_4_avx512f_fini,211.compute_native = fletcher_4_avx512f_native,212.init_byteswap = fletcher_4_avx512f_init,213.fini_byteswap = fletcher_4_avx512f_fini,214.compute_byteswap = fletcher_4_avx512bw_byteswap,215.valid = fletcher_4_avx512bw_valid,216.uses_fpu = B_TRUE,217.name = "avx512bw"218};219#endif220221#endif /* defined(__x86_64) && defined(HAVE_AVX512F) */222223224