LCOV - code coverage report
Current view: top level - spdk/lib/util - xor.c (source / functions) Hit Total Coverage
Test: Combined Lines: 64 67 95.5 %
Date: 2024-11-18 22:20:41 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 62 89 69.7 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/xor.h"
       7                 :            : #include "spdk/config.h"
       8                 :            : #include "spdk/assert.h"
       9                 :            : #include "spdk/util.h"
      10                 :            : 
      11                 :            : /* maximum number of source buffers */
      12                 :            : #define SPDK_XOR_MAX_SRC        256
      13                 :            : 
      14                 :            : static inline bool
      15                 :  245849464 : is_aligned(void *ptr, size_t alignment)
      16                 :            : {
      17                 :  245849464 :         uintptr_t p = (uintptr_t)ptr;
      18                 :            : 
      19                 :  245974476 :         return p == SPDK_ALIGN_FLOOR(p, alignment);
      20                 :     125012 : }
      21                 :            : 
      22                 :            : static bool
      23                 :   70946379 : buffers_aligned(void *dest, void **sources, uint32_t n, size_t alignment)
      24                 :            : {
      25                 :      76659 :         uint32_t i;
      26                 :            : 
      27         [ +  + ]:  245849464 :         for (i = 0; i < n; i++) {
      28   [ +  +  +  -  :  175215874 :                 if (!is_aligned(sources[i], alignment)) {
                   +  + ]
      29                 :     312789 :                         return false;
      30                 :            :                 }
      31                 :   13485473 :         }
      32                 :            : 
      33                 :   70633590 :         return is_aligned(dest, alignment);
      34                 :    5473331 : }
      35                 :            : 
      36                 :            : static void
      37                 :     159964 : xor_gen_unaligned(void *dest, void **sources, uint32_t n, uint32_t len)
      38                 :            : {
      39                 :      31737 :         uint32_t i, j;
      40                 :            : 
      41         [ +  + ]: 1083033813 :         for (i = 0; i < len; i++) {
      42                 : 1082873849 :                 uint8_t b = 0;
      43                 :            : 
      44         [ +  + ]: 4593933192 :                 for (j = 0; j < n; j++) {
      45   [ -  +  -  +  : 3511059343 :                         b ^= ((uint8_t *)sources[j])[i];
             -  +  -  + ]
      46                 :  702203209 :                 }
      47   [ -  +  -  + ]: 1082873849 :                 ((uint8_t *)dest)[i] = b;
      48                 :  216573047 :         }
      49                 :     159964 : }
      50                 :            : 
      51                 :            : static void
      52                 :     173199 : xor_gen_basic(void *dest, void **sources, uint32_t n, uint32_t len)
      53                 :            : {
      54                 :      34069 :         uint32_t shift;
      55                 :      34069 :         uint32_t len_div, len_rem;
      56                 :      34069 :         uint32_t i, j;
      57                 :            : 
      58         [ +  + ]:     173199 :         if (!buffers_aligned(dest, sources, n, sizeof(uint64_t))) {
      59                 :     142066 :                 xor_gen_unaligned(dest, sources, n, len);
      60                 :     142066 :                 return;
      61                 :            :         }
      62                 :            : 
      63                 :      31133 :         shift = spdk_u32log2(sizeof(uint64_t));
      64         [ +  + ]:      31133 :         len_div = len >> shift;
      65         [ +  + ]:      31133 :         len_rem = len_div << shift;
      66                 :            : 
      67         [ +  + ]:   16646319 :         for (i = 0; i < len_div; i++) {
      68                 :   16615186 :                 uint64_t w = 0;
      69                 :            : 
      70         [ +  + ]:   79437584 :                 for (j = 0; j < n; j++) {
      71   [ +  -  +  -  :   62822398 :                         w ^= ((uint64_t *)sources[j])[i];
             +  -  +  - ]
      72                 :   12001556 :                 }
      73   [ +  -  +  - ]:   16615186 :                 ((uint64_t *)dest)[i] = w;
      74                 :    3150916 :         }
      75                 :            : 
      76         [ +  + ]:      31133 :         if (len_rem < len) {
      77                 :      13296 :                 void *sources2[SPDK_XOR_MAX_SRC];
      78                 :            : 
      79         [ +  + ]:      78633 :                 for (j = 0; j < n; j++) {
      80   [ +  -  +  -  :      60735 :                         sources2[j] = (uint8_t *)sources[j] + len_rem;
          +  -  +  -  +  
                -  +  - ]
      81                 :      11288 :                 }
      82                 :            : 
      83         [ +  - ]:      17898 :                 xor_gen_unaligned((uint8_t *)dest + len_rem, sources2, n, len - len_rem);
      84                 :       3324 :         }
      85         [ -  + ]:      34069 : }
      86                 :            : 
      87                 :            : #ifdef SPDK_CONFIG_ISAL
      88                 :            : #include "isa-l/include/raid.h"
      89                 :            : 
      90                 :            : #define SPDK_XOR_BUF_ALIGN 32
      91                 :            : 
      92                 :            : static int
      93                 :   70773180 : do_xor_gen(void *dest, void **sources, uint32_t n, uint32_t len)
      94                 :            : {
      95         [ +  + ]:   70773180 :         if (buffers_aligned(dest, sources, n, SPDK_XOR_BUF_ALIGN)) {
      96                 :   28185480 :                 void *buffers[SPDK_XOR_MAX_SRC + 1];
      97                 :            : 
      98         [ -  + ]:   70599981 :                 if (n >= INT_MAX) {
      99                 :          0 :                         return -EINVAL;
     100                 :            :                 }
     101                 :            : 
     102   [ +  +  +  + ]:   70599981 :                 memcpy(buffers, sources, n * sizeof(buffers[0]));
     103   [ +  +  +  +  :   70599981 :                 buffers[n] = dest;
                   +  + ]
     104                 :            : 
     105         [ -  + ]:   70599981 :                 if (xor_gen(n + 1, len, buffers)) {
     106                 :          0 :                         return -EINVAL;
     107                 :            :                 }
     108      [ -  -  + ]:    5405193 :         } else {
     109                 :     173199 :                 xor_gen_basic(dest, sources, n, len);
     110                 :            :         }
     111                 :            : 
     112                 :   70773180 :         return 0;
     113                 :    5439262 : }
     114                 :            : 
     115                 :            : #else
     116                 :            : 
     117                 :            : #define SPDK_XOR_BUF_ALIGN sizeof(uint64_t)
     118                 :            : 
     119                 :            : static inline int
     120                 :            : do_xor_gen(void *dest, void **sources, uint32_t n, uint32_t len)
     121                 :            : {
     122                 :            :         xor_gen_basic(dest, sources, n, len);
     123                 :            :         return 0;
     124                 :            : }
     125                 :            : 
     126                 :            : #endif
     127                 :            : 
     128                 :            : int
     129                 :   70773180 : spdk_xor_gen(void *dest, void **sources, uint32_t n, uint32_t len)
     130                 :            : {
     131   [ +  -  -  + ]:   70773180 :         if (n < 2 || n > SPDK_XOR_MAX_SRC) {
     132                 :          0 :                 return -EINVAL;
     133                 :            :         }
     134                 :            : 
     135                 :   70773180 :         return do_xor_gen(dest, sources, n, len);
     136                 :    5439262 : }
     137                 :            : 
     138                 :            : size_t
     139                 :         48 : spdk_xor_get_optimal_alignment(void)
     140                 :            : {
     141                 :         48 :         return SPDK_XOR_BUF_ALIGN;
     142                 :            : }
     143                 :            : 
     144                 :            : SPDK_STATIC_ASSERT(SPDK_XOR_BUF_ALIGN > 0 && !(SPDK_XOR_BUF_ALIGN & (SPDK_XOR_BUF_ALIGN - 1)),
     145                 :            :                    "Must be power of 2");

Generated by: LCOV version 1.15