1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * Implement fast Fletcher4 with AVX2 instructions. (x86_64) 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * Use the 256-bit AVX2 SIMD instructions and registers to compute 5eda14cbcSMatt Macy * Fletcher4 in four incremental 64-bit parallel accumulator streams, 6eda14cbcSMatt Macy * and then combine the streams to form the final four checksum words. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * Copyright (C) 2015 Intel Corporation. 9eda14cbcSMatt Macy * 10eda14cbcSMatt Macy * Authors: 11eda14cbcSMatt Macy * James Guilford <james.guilford@intel.com> 12eda14cbcSMatt Macy * Jinshan Xiong <jinshan.xiong@intel.com> 13eda14cbcSMatt Macy * 14eda14cbcSMatt Macy * This software is available to you under a choice of one of two 15eda14cbcSMatt Macy * licenses. You may choose to be licensed under the terms of the GNU 16eda14cbcSMatt Macy * General Public License (GPL) Version 2, available from the file 17eda14cbcSMatt Macy * COPYING in the main directory of this source tree, or the 18eda14cbcSMatt Macy * OpenIB.org BSD license below: 19eda14cbcSMatt Macy * 20eda14cbcSMatt Macy * Redistribution and use in source and binary forms, with or 21eda14cbcSMatt Macy * without modification, are permitted provided that the following 22eda14cbcSMatt Macy * conditions are met: 23eda14cbcSMatt Macy * 24eda14cbcSMatt Macy * - Redistributions of source code must retain the above 25eda14cbcSMatt Macy * copyright notice, this list of conditions and the following 26eda14cbcSMatt Macy * disclaimer. 27eda14cbcSMatt Macy * 28eda14cbcSMatt Macy * - Redistributions in binary form must reproduce the above 29eda14cbcSMatt Macy * copyright notice, this list of conditions and the following 30eda14cbcSMatt Macy * disclaimer in the documentation and/or other materials 31eda14cbcSMatt Macy * provided with the distribution. 32eda14cbcSMatt Macy * 33eda14cbcSMatt Macy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 34eda14cbcSMatt Macy * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 35eda14cbcSMatt Macy * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 36eda14cbcSMatt Macy * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 37eda14cbcSMatt Macy * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 38eda14cbcSMatt Macy * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 39eda14cbcSMatt Macy * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 40eda14cbcSMatt Macy * SOFTWARE. 41eda14cbcSMatt Macy */ 42eda14cbcSMatt Macy 43eda14cbcSMatt Macy #if defined(HAVE_AVX) && defined(HAVE_AVX2) 44eda14cbcSMatt Macy 45eda14cbcSMatt Macy #include <sys/spa_checksum.h> 46da5137abSMartin Matuska #include <sys/string.h> 47eda14cbcSMatt Macy #include <sys/simd.h> 48eda14cbcSMatt Macy #include <zfs_fletcher.h> 49eda14cbcSMatt Macy 50c03c5b1cSMartin Matuska ZFS_NO_SANITIZE_UNDEFINED 51eda14cbcSMatt Macy static void 52eda14cbcSMatt Macy fletcher_4_avx2_init(fletcher_4_ctx_t *ctx) 53eda14cbcSMatt Macy { 54*bb2d13b6SMartin Matuska kfpu_begin(); 55da5137abSMartin Matuska memset(ctx->avx, 0, 4 * sizeof (zfs_fletcher_avx_t)); 56eda14cbcSMatt Macy } 57eda14cbcSMatt Macy 58c03c5b1cSMartin Matuska ZFS_NO_SANITIZE_UNDEFINED 59eda14cbcSMatt Macy static void 60eda14cbcSMatt Macy fletcher_4_avx2_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp) 61eda14cbcSMatt Macy { 62eda14cbcSMatt Macy uint64_t A, B, C, D; 63eda14cbcSMatt Macy 64eda14cbcSMatt Macy A = ctx->avx[0].v[0] + ctx->avx[0].v[1] + 65eda14cbcSMatt Macy ctx->avx[0].v[2] + ctx->avx[0].v[3]; 66eda14cbcSMatt Macy B = 0 - ctx->avx[0].v[1] - 2 * ctx->avx[0].v[2] - 3 * ctx->avx[0].v[3] + 67eda14cbcSMatt Macy 4 * ctx->avx[1].v[0] + 4 * ctx->avx[1].v[1] + 4 * ctx->avx[1].v[2] + 68eda14cbcSMatt Macy 4 * ctx->avx[1].v[3]; 69eda14cbcSMatt Macy 70eda14cbcSMatt Macy C = ctx->avx[0].v[2] + 3 * ctx->avx[0].v[3] - 6 * ctx->avx[1].v[0] - 71eda14cbcSMatt Macy 10 * ctx->avx[1].v[1] - 14 * ctx->avx[1].v[2] - 72eda14cbcSMatt Macy 18 * ctx->avx[1].v[3] + 16 * ctx->avx[2].v[0] + 73eda14cbcSMatt Macy 16 * ctx->avx[2].v[1] + 16 * ctx->avx[2].v[2] + 74eda14cbcSMatt Macy 16 * ctx->avx[2].v[3]; 75eda14cbcSMatt Macy 76eda14cbcSMatt Macy D = 0 - ctx->avx[0].v[3] + 4 * ctx->avx[1].v[0] + 77eda14cbcSMatt Macy 10 * ctx->avx[1].v[1] + 20 * ctx->avx[1].v[2] + 78eda14cbcSMatt Macy 34 * ctx->avx[1].v[3] - 48 * ctx->avx[2].v[0] - 79eda14cbcSMatt Macy 64 * ctx->avx[2].v[1] - 80 * ctx->avx[2].v[2] - 80eda14cbcSMatt Macy 96 * ctx->avx[2].v[3] + 64 * ctx->avx[3].v[0] + 81eda14cbcSMatt Macy 64 * ctx->avx[3].v[1] + 64 * ctx->avx[3].v[2] + 82eda14cbcSMatt Macy 64 * ctx->avx[3].v[3]; 83eda14cbcSMatt Macy 84eda14cbcSMatt Macy ZIO_SET_CHECKSUM(zcp, A, B, C, D); 85*bb2d13b6SMartin Matuska kfpu_end(); 86eda14cbcSMatt Macy } 87eda14cbcSMatt Macy 88eda14cbcSMatt Macy #define FLETCHER_4_AVX2_RESTORE_CTX(ctx) \ 89eda14cbcSMatt Macy { \ 90eda14cbcSMatt Macy asm volatile("vmovdqu %0, %%ymm0" :: "m" ((ctx)->avx[0])); \ 91eda14cbcSMatt Macy asm volatile("vmovdqu %0, %%ymm1" :: "m" ((ctx)->avx[1])); \ 92eda14cbcSMatt Macy asm volatile("vmovdqu %0, %%ymm2" :: "m" ((ctx)->avx[2])); \ 93eda14cbcSMatt Macy asm volatile("vmovdqu %0, %%ymm3" :: "m" ((ctx)->avx[3])); \ 94eda14cbcSMatt Macy } 95eda14cbcSMatt Macy 96eda14cbcSMatt Macy #define FLETCHER_4_AVX2_SAVE_CTX(ctx) \ 97eda14cbcSMatt Macy { \ 98eda14cbcSMatt Macy asm volatile("vmovdqu %%ymm0, %0" : "=m" ((ctx)->avx[0])); \ 99eda14cbcSMatt Macy asm volatile("vmovdqu %%ymm1, %0" : "=m" ((ctx)->avx[1])); \ 100eda14cbcSMatt Macy asm volatile("vmovdqu %%ymm2, %0" : "=m" ((ctx)->avx[2])); \ 101eda14cbcSMatt Macy asm volatile("vmovdqu %%ymm3, %0" : "=m" ((ctx)->avx[3])); \ 102eda14cbcSMatt Macy } 103eda14cbcSMatt Macy 104eda14cbcSMatt Macy 105eda14cbcSMatt Macy static void 106eda14cbcSMatt Macy fletcher_4_avx2_native(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size) 107eda14cbcSMatt Macy { 108eda14cbcSMatt Macy const uint64_t *ip = buf; 109eda14cbcSMatt Macy const uint64_t *ipend = (uint64_t *)((uint8_t *)ip + size); 110eda14cbcSMatt Macy 111eda14cbcSMatt Macy FLETCHER_4_AVX2_RESTORE_CTX(ctx); 112eda14cbcSMatt Macy 113*bb2d13b6SMartin Matuska do { 114eda14cbcSMatt Macy asm volatile("vpmovzxdq %0, %%ymm4"::"m" (*ip)); 115eda14cbcSMatt Macy asm volatile("vpaddq %ymm4, %ymm0, %ymm0"); 116eda14cbcSMatt Macy asm volatile("vpaddq %ymm0, %ymm1, %ymm1"); 117eda14cbcSMatt Macy asm volatile("vpaddq %ymm1, %ymm2, %ymm2"); 118eda14cbcSMatt Macy asm volatile("vpaddq %ymm2, %ymm3, %ymm3"); 119*bb2d13b6SMartin Matuska } while ((ip += 2) < ipend); 120eda14cbcSMatt Macy 121eda14cbcSMatt Macy FLETCHER_4_AVX2_SAVE_CTX(ctx); 122eda14cbcSMatt Macy asm volatile("vzeroupper"); 123eda14cbcSMatt Macy } 124eda14cbcSMatt Macy 125eda14cbcSMatt Macy static void 126eda14cbcSMatt Macy fletcher_4_avx2_byteswap(fletcher_4_ctx_t *ctx, const void *buf, uint64_t size) 127eda14cbcSMatt Macy { 128eda14cbcSMatt Macy static const zfs_fletcher_avx_t mask = { 129eda14cbcSMatt Macy .v = { 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B, 130eda14cbcSMatt Macy 0xFFFFFFFF00010203, 0xFFFFFFFF08090A0B } 131eda14cbcSMatt Macy }; 132eda14cbcSMatt Macy const uint64_t *ip = buf; 133eda14cbcSMatt Macy const uint64_t *ipend = (uint64_t *)((uint8_t *)ip + size); 134eda14cbcSMatt Macy 135eda14cbcSMatt Macy FLETCHER_4_AVX2_RESTORE_CTX(ctx); 136eda14cbcSMatt Macy 137eda14cbcSMatt Macy asm volatile("vmovdqu %0, %%ymm5" :: "m" (mask)); 138eda14cbcSMatt Macy 139*bb2d13b6SMartin Matuska do { 140eda14cbcSMatt Macy asm volatile("vpmovzxdq %0, %%ymm4"::"m" (*ip)); 141eda14cbcSMatt Macy asm volatile("vpshufb %ymm5, %ymm4, %ymm4"); 142eda14cbcSMatt Macy 143eda14cbcSMatt Macy asm volatile("vpaddq %ymm4, %ymm0, %ymm0"); 144eda14cbcSMatt Macy asm volatile("vpaddq %ymm0, %ymm1, %ymm1"); 145eda14cbcSMatt Macy asm volatile("vpaddq %ymm1, %ymm2, %ymm2"); 146eda14cbcSMatt Macy asm volatile("vpaddq %ymm2, %ymm3, %ymm3"); 147*bb2d13b6SMartin Matuska } while ((ip += 2) < ipend); 148eda14cbcSMatt Macy 149eda14cbcSMatt Macy FLETCHER_4_AVX2_SAVE_CTX(ctx); 150eda14cbcSMatt Macy asm volatile("vzeroupper"); 151eda14cbcSMatt Macy } 152eda14cbcSMatt Macy 153eda14cbcSMatt Macy static boolean_t fletcher_4_avx2_valid(void) 154eda14cbcSMatt Macy { 155eda14cbcSMatt Macy return (kfpu_allowed() && zfs_avx_available() && zfs_avx2_available()); 156eda14cbcSMatt Macy } 157eda14cbcSMatt Macy 158eda14cbcSMatt Macy const fletcher_4_ops_t fletcher_4_avx2_ops = { 159eda14cbcSMatt Macy .init_native = fletcher_4_avx2_init, 160eda14cbcSMatt Macy .fini_native = fletcher_4_avx2_fini, 161eda14cbcSMatt Macy .compute_native = fletcher_4_avx2_native, 162eda14cbcSMatt Macy .init_byteswap = fletcher_4_avx2_init, 163eda14cbcSMatt Macy .fini_byteswap = fletcher_4_avx2_fini, 164eda14cbcSMatt Macy .compute_byteswap = fletcher_4_avx2_byteswap, 165eda14cbcSMatt Macy .valid = fletcher_4_avx2_valid, 166eda14cbcSMatt Macy .name = "avx2" 167eda14cbcSMatt Macy }; 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy #endif /* defined(HAVE_AVX) && defined(HAVE_AVX2) */ 170