LCOV - code coverage report
Current view: top level - app/test - test_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 155 177 87.6 %
Date: 2026-11-01 19:11:22 Functions: 10 10 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 141 206 68.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <inttypes.h>
       7                 :            : #include <string.h>
       8                 :            : #include <math.h>
       9                 :            : #include <rte_common.h>
      10                 :            : #include <rte_bitops.h>
      11                 :            : #include <rte_hexdump.h>
      12                 :            : #include <rte_random.h>
      13                 :            : #include <rte_pause.h>
      14                 :            : 
      15                 :            : #include "test.h"
      16                 :            : 
      17                 :            : #define MAX_NUM (1 << 20)
      18                 :            : 
      19                 :            : #define FAIL(x)\
      20                 :            :         {printf(x "() test failed!\n");\
      21                 :            :         return -1;}
      22                 :            : 
      23                 :            : /* test_ptr_add_sub_align independent test parameters */
      24                 :            : #define RTE_TEST_COMMON_MAX_ALIGNMENT RTE_CACHE_LINE_SIZE
      25                 :            : #define RTE_TEST_COMMON_MAX_OFFSET 256
      26                 :            : #define RTE_TEST_COMMON_MAX_INCREMENT 128
      27                 :            : /* test_ptr_add_sub_align dependent: computed based on test requirements */
      28                 :            : /* Extra RTE_TEST_COMMON_MAX_ALIGNMENT to ensure CEIL can round up without going out of bounds */
      29                 :            : #define TEST_BUFFER_SIZE (RTE_TEST_COMMON_MAX_OFFSET + RTE_TEST_COMMON_MAX_INCREMENT + \
      30                 :            :         (2 * RTE_TEST_COMMON_MAX_ALIGNMENT) + 16)
      31                 :            : 
      32                 :            : /* test_ptr_align_edge_cases independent test parameters */
      33                 :            : #define RTE_COMMON_TEST_PAGE_SIZE 4096
      34                 :            : #define RTE_COMMON_TEST_CACHE_LINE_ALIGN RTE_CACHE_LINE_SIZE
      35                 :            : /* test_ptr_align_edge_cases dependent: computed based on test requirements */
      36                 :            : /* Must fit PAGE_SIZE alignment tests */
      37                 :            : #define RTE_COMMON_TEST_EDGE_CASE_BUFFER_SIZE (2 * RTE_COMMON_TEST_PAGE_SIZE)
      38                 :            : #define RTE_COMMON_TEST_DOUBLE_PAGE_SIZE (2 * RTE_COMMON_TEST_PAGE_SIZE)
      39                 :            : /* Must be >= CACHE_LINE_ALIGN to prevent overflow in CEIL boundary test */
      40                 :            : #define RTE_COMMON_TEST_BOUNDARY_TEST_OFFSET (2 * RTE_COMMON_TEST_CACHE_LINE_ALIGN)
      41                 :            : 
      42                 :            : static int
      43                 :          1 : test_ptr_add_sub_align(void)
      44                 :            : {
      45                 :            :         /* Unaligned buffer for testing unaligned pointer types */
      46                 :            :         char unaligned_buffer[TEST_BUFFER_SIZE];
      47                 :            :         /* Aligned buffer for testing aligned pointer types */
      48                 :            :         alignas(RTE_TEST_COMMON_MAX_ALIGNMENT) char aligned_buffer[TEST_BUFFER_SIZE];
      49                 :            :         size_t offset;
      50                 :            :         uint8_t uval, aval;
      51                 :            :         uint16_t u16_uval, u16_aval;
      52                 :            :         uint32_t u32_uval, u32_aval;
      53                 :            :         uint64_t u64_uval, u64_aval;
      54                 :            : 
      55                 :          1 :         uval = (uint8_t)rte_rand();
      56                 :          1 :         aval = (uint8_t)rte_rand();
      57         [ -  + ]:          1 :         if (uval == aval)
      58                 :          0 :                 aval = (uint8_t)~aval;
      59                 :            : 
      60                 :            :         /* Compute expected values for each type width by replicating byte pattern */
      61                 :          1 :         memset(&u16_uval, uval, sizeof(u16_uval));
      62                 :          1 :         memset(&u16_aval, aval, sizeof(u16_aval));
      63                 :            :         memset(&u32_uval, uval, sizeof(u32_uval));
      64                 :            :         memset(&u32_aval, aval, sizeof(u32_aval));
      65                 :            :         memset(&u64_uval, uval, sizeof(u64_uval));
      66                 :            :         memset(&u64_aval, aval, sizeof(u64_aval));
      67                 :            : 
      68                 :            :         /* Initialize buffers - prevents compiler optimization and tests unaligned access */
      69                 :            :         memset(unaligned_buffer, uval, sizeof(unaligned_buffer));
      70                 :            :         memset(aligned_buffer, aval, sizeof(aligned_buffer));
      71                 :            : 
      72                 :            :         /* Test various offsets to ensure correctness across memory range */
      73         [ +  + ]:        257 :         for (offset = 0; offset < RTE_TEST_COMMON_MAX_OFFSET; offset++) {
      74                 :        256 :                 void *ubase = unaligned_buffer + offset;
      75                 :        256 :                 void *abase = aligned_buffer + offset;
      76                 :            :                 size_t increment;
      77                 :            : 
      78                 :            :                 /* Test different increment values */
      79         [ +  + ]:      33024 :                 for (increment = 0; increment < RTE_TEST_COMMON_MAX_INCREMENT; increment++) {
      80                 :            :                         void *result;
      81                 :            :                         char *cp_result;
      82                 :            :                         const void *cvp_result;
      83                 :            :                         unaligned_uint16_t *u16p_result;
      84                 :            :                         unaligned_uint32_t *u32p_result;
      85                 :            :                         unaligned_uint64_t *u64p_result;
      86                 :            :                         uintptr_t uptr_val, aptr_val;
      87                 :            :                         uintptr_t uexp_floor, uexp_ceil, aexp_floor, aexp_ceil;
      88                 :            :                         size_t align;
      89                 :            : 
      90                 :            :                         /* Test void* ADD and SUB using unaligned buffer */
      91                 :      32768 :                         result = RTE_PTR_ADD(ubase, increment);
      92                 :            :                         RTE_TEST_ASSERT_EQUAL(result, (void *)((char *)ubase + increment),
      93                 :            :                                 "RTE_PTR_ADD for void* at offset=%zu inc=%zu",
      94                 :            :                                 offset, increment);
      95                 :            :                         result = RTE_PTR_SUB(result, increment);
      96                 :            :                         RTE_TEST_ASSERT_EQUAL(result, ubase,
      97                 :            :                                 "RTE_PTR_SUB for void* at offset=%zu inc=%zu",
      98                 :            :                                 offset, increment);
      99                 :            : 
     100                 :            :                         /* Test char* type preservation using unaligned buffer */
     101                 :            :                         cp_result = RTE_PTR_ADD((char *)ubase, increment);
     102                 :            :                         RTE_TEST_ASSERT_EQUAL(cp_result, (char *)ubase + increment,
     103                 :            :                                 "RTE_PTR_ADD for char* at offset=%zu inc=%zu",
     104                 :            :                                 offset, increment);
     105         [ -  + ]:      32768 :                         RTE_TEST_ASSERT_EQUAL((unsigned char)*cp_result, (unsigned char)uval,
     106                 :            :                                 "char* dereference at offset=%zu inc=%zu",
     107                 :            :                                 offset, increment);
     108                 :            :                         cp_result = RTE_PTR_SUB(cp_result, increment);
     109                 :            :                         RTE_TEST_ASSERT_EQUAL(cp_result, (char *)ubase,
     110                 :            :                                 "RTE_PTR_SUB for char* at offset=%zu inc=%zu",
     111                 :            :                                 offset, increment);
     112                 :            : 
     113                 :            :                         /* Test const void* preservation using unaligned buffer */
     114                 :            :                         cvp_result = RTE_PTR_ADD((const void *)ubase, increment);
     115                 :            :                         RTE_TEST_ASSERT_EQUAL(cvp_result,
     116                 :            :                                 (const void *)((char *)ubase + increment),
     117                 :            :                                 "RTE_PTR_ADD for const void* at offset=%zu inc=%zu",
     118                 :            :                                 offset, increment);
     119                 :            :                         cvp_result = RTE_PTR_SUB(cvp_result, increment);
     120                 :            :                         RTE_TEST_ASSERT_EQUAL(cvp_result, (const void *)ubase,
     121                 :            :                                 "RTE_PTR_SUB for const void* at offset=%zu inc=%zu",
     122                 :            :                                 offset, increment);
     123                 :            : 
     124                 :            :                         /* Test unaligned_uint16_t* using unaligned buffer */
     125                 :            :                         u16p_result = RTE_PTR_ADD((unaligned_uint16_t *)ubase, increment);
     126                 :            :                         RTE_TEST_ASSERT_EQUAL(u16p_result,
     127                 :            :                                 (unaligned_uint16_t *)((char *)ubase + increment),
     128                 :            :                                 "RTE_PTR_ADD for u16* at offset=%zu inc=%zu",
     129                 :            :                                 offset, increment);
     130         [ -  + ]:      32768 :                         RTE_TEST_ASSERT_EQUAL(*u16p_result, u16_uval,
     131                 :            :                                 "unaligned u16 dereference at offset=%zu inc=%zu",
     132                 :            :                                 offset, increment);
     133                 :            :                         u16p_result = RTE_PTR_SUB(u16p_result, increment);
     134                 :            :                         RTE_TEST_ASSERT_EQUAL(u16p_result, (unaligned_uint16_t *)ubase,
     135                 :            :                                 "RTE_PTR_SUB for u16* at offset=%zu inc=%zu",
     136                 :            :                                 offset, increment);
     137                 :            : 
     138                 :            :                         /* Test unaligned_uint32_t* using unaligned buffer */
     139                 :            :                         u32p_result = RTE_PTR_ADD((unaligned_uint32_t *)ubase, increment);
     140                 :            :                         RTE_TEST_ASSERT_EQUAL(u32p_result,
     141                 :            :                                 (unaligned_uint32_t *)((char *)ubase + increment),
     142                 :            :                                 "RTE_PTR_ADD for u32* at offset=%zu inc=%zu",
     143                 :            :                                 offset, increment);
     144         [ -  + ]:      32768 :                         RTE_TEST_ASSERT_EQUAL(*u32p_result, u32_uval,
     145                 :            :                                 "unaligned u32 dereference at offset=%zu inc=%zu",
     146                 :            :                                 offset, increment);
     147                 :            :                         u32p_result = RTE_PTR_SUB(u32p_result, increment);
     148                 :            :                         RTE_TEST_ASSERT_EQUAL(u32p_result, (unaligned_uint32_t *)ubase,
     149                 :            :                                 "RTE_PTR_SUB for u32* at offset=%zu inc=%zu",
     150                 :            :                                 offset, increment);
     151                 :            : 
     152                 :            :                         /* Test unaligned_uint64_t* using unaligned buffer */
     153                 :            :                         u64p_result = RTE_PTR_ADD((unaligned_uint64_t *)ubase, increment);
     154                 :            :                         RTE_TEST_ASSERT_EQUAL(u64p_result,
     155                 :            :                                 (unaligned_uint64_t *)((char *)ubase + increment),
     156                 :            :                                 "RTE_PTR_ADD for u64* at offset=%zu inc=%zu",
     157                 :            :                                 offset, increment);
     158         [ -  + ]:      32768 :                         RTE_TEST_ASSERT_EQUAL(*u64p_result, u64_uval,
     159                 :            :                                 "unaligned u64 dereference at offset=%zu inc=%zu",
     160                 :            :                                 offset, increment);
     161                 :            :                         u64p_result = RTE_PTR_SUB(u64p_result, increment);
     162                 :            :                         RTE_TEST_ASSERT_EQUAL(u64p_result, (unaligned_uint64_t *)ubase,
     163                 :            :                                 "RTE_PTR_SUB for u64* at offset=%zu inc=%zu",
     164                 :            :                                 offset, increment);
     165                 :            : 
     166                 :            :                         /* Test aligned uint16_t* at 2-byte aligned offsets */
     167         [ +  + ]:      32768 :                         if (offset % sizeof(uint16_t) == 0 &&
     168         [ +  + ]:      16384 :                                         increment % sizeof(uint16_t) == 0) {
     169                 :            :                                 uint16_t *a16p_result;
     170                 :       8192 :                                 a16p_result = RTE_PTR_ADD((uint16_t *)abase, increment);
     171                 :            :                                 RTE_TEST_ASSERT_EQUAL(a16p_result,
     172                 :            :                                         (uint16_t *)((char *)abase + increment),
     173                 :            :                                         "RTE_PTR_ADD for uint16_t* at offset=%zu inc=%zu",
     174                 :            :                                         offset, increment);
     175         [ -  + ]:       8192 :                                 RTE_TEST_ASSERT_EQUAL(*a16p_result, u16_aval,
     176                 :            :                                         "aligned u16 dereference at offset=%zu inc=%zu",
     177                 :            :                                         offset, increment);
     178                 :            :                                 a16p_result = RTE_PTR_SUB(a16p_result, increment);
     179                 :            :                                 RTE_TEST_ASSERT_EQUAL(a16p_result, (uint16_t *)abase,
     180                 :            :                                         "RTE_PTR_SUB for uint16_t* at offset=%zu inc=%zu",
     181                 :            :                                         offset, increment);
     182                 :            :                         }
     183                 :            : 
     184                 :            :                         /* Test aligned uint32_t* at 4-byte aligned offsets */
     185         [ +  + ]:      32768 :                         if (offset % sizeof(uint32_t) == 0 &&
     186         [ +  + ]:       8192 :                                         increment % sizeof(uint32_t) == 0) {
     187                 :            :                                 uint32_t *a32p_result;
     188                 :       2048 :                                 a32p_result = RTE_PTR_ADD((uint32_t *)abase, increment);
     189                 :            :                                 RTE_TEST_ASSERT_EQUAL(a32p_result,
     190                 :            :                                         (uint32_t *)((char *)abase + increment),
     191                 :            :                                         "RTE_PTR_ADD for uint32_t* at offset=%zu inc=%zu",
     192                 :            :                                         offset, increment);
     193         [ -  + ]:       2048 :                                 RTE_TEST_ASSERT_EQUAL(*a32p_result, u32_aval,
     194                 :            :                                         "aligned u32 dereference at offset=%zu inc=%zu",
     195                 :            :                                         offset, increment);
     196                 :            :                                 a32p_result = RTE_PTR_SUB(a32p_result, increment);
     197                 :            :                                 RTE_TEST_ASSERT_EQUAL(a32p_result, (uint32_t *)abase,
     198                 :            :                                         "RTE_PTR_SUB for uint32_t* at offset=%zu inc=%zu",
     199                 :            :                                         offset, increment);
     200                 :            :                         }
     201                 :            : 
     202                 :            :                         /* Test aligned uint64_t* at 8-byte aligned offsets */
     203         [ +  + ]:      32768 :                         if (offset % sizeof(uint64_t) == 0 &&
     204         [ +  + ]:       4096 :                                         increment % sizeof(uint64_t) == 0) {
     205                 :            :                                 uint64_t *a64p_result;
     206                 :        512 :                                 a64p_result = RTE_PTR_ADD((uint64_t *)abase, increment);
     207                 :            :                                 RTE_TEST_ASSERT_EQUAL(a64p_result,
     208                 :            :                                         (uint64_t *)((char *)abase + increment),
     209                 :            :                                         "RTE_PTR_ADD for uint64_t* at offset=%zu inc=%zu",
     210                 :            :                                         offset, increment);
     211         [ -  + ]:        512 :                                 RTE_TEST_ASSERT_EQUAL(*a64p_result, u64_aval,
     212                 :            :                                         "aligned u64 dereference at offset=%zu inc=%zu",
     213                 :            :                                         offset, increment);
     214                 :            :                                 a64p_result = RTE_PTR_SUB(a64p_result, increment);
     215                 :            :                                 RTE_TEST_ASSERT_EQUAL(a64p_result, (uint64_t *)abase,
     216                 :            :                                         "RTE_PTR_SUB for uint64_t* at offset=%zu inc=%zu",
     217                 :            :                                         offset, increment);
     218                 :            :                         }
     219                 :            : 
     220                 :            :                         /* Test alignment functions with various alignments */
     221                 :      32768 :                         uptr_val = (uintptr_t)RTE_PTR_ADD(ubase, increment);
     222                 :      32768 :                         aptr_val = (uintptr_t)RTE_PTR_ADD(abase, increment);
     223                 :            : 
     224                 :            :                         /* Test power-of-2 alignments: 1, 2, 4, 8, 16 */
     225         [ +  + ]:     262144 :                         for (align = 1; align <= RTE_TEST_COMMON_MAX_ALIGNMENT; align <<= 1) {
     226                 :            :                                 /* Compute expected values using arithmetic, not masking */
     227                 :     229376 :                                 uexp_floor = (uptr_val / align) * align;
     228                 :     229376 :                                 uexp_ceil = ((uptr_val + align - 1) / align) * align;
     229                 :     229376 :                                 aexp_floor = (aptr_val / align) * align;
     230                 :     229376 :                                 aexp_ceil = ((aptr_val + align - 1) / align) * align;
     231                 :            : 
     232                 :            :                                 result = RTE_PTR_ADD(ubase, increment);
     233                 :     229376 :                                 result = RTE_PTR_ALIGN_FLOOR(result, align);
     234         [ -  + ]:     229376 :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)result, uexp_floor,
     235                 :            :                                         "ALIGN_FLOOR offset=%zu inc=%zu align=%zu",
     236                 :            :                                         offset, increment, align);
     237         [ -  + ]:     229376 :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)result % align, 0,
     238                 :            :                                         "ALIGN_FLOOR not aligned offset=%zu inc=%zu align=%zu",
     239                 :            :                                         offset, increment, align);
     240                 :            : 
     241                 :            :                                 result = RTE_PTR_ADD(ubase, increment);
     242                 :     229376 :                                 result = RTE_PTR_ALIGN_CEIL(result, align);
     243         [ -  + ]:     229376 :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)result, uexp_ceil,
     244                 :            :                                         "ALIGN_CEIL offset=%zu inc=%zu align=%zu",
     245                 :            :                                         offset, increment, align);
     246         [ -  + ]:     229376 :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)result % align, 0,
     247                 :            :                                         "ALIGN_CEIL not aligned offset=%zu inc=%zu align=%zu",
     248                 :            :                                         offset, increment, align);
     249                 :            : 
     250                 :            :                                 result = RTE_PTR_ADD(ubase, increment);
     251                 :            :                                 result = RTE_PTR_ALIGN(result, align);
     252                 :            :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)result, uexp_ceil,
     253                 :            :                                         "ALIGN != CEIL offset=%zu inc=%zu align=%zu",
     254                 :            :                                         offset, increment, align);
     255                 :            : 
     256                 :            :                                 /* Test type preservation */
     257                 :            :                                 cp_result = RTE_PTR_ADD((char *)ubase, increment);
     258                 :            :                                 cp_result = RTE_PTR_ALIGN_FLOOR(cp_result, align);
     259                 :            :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)cp_result, uexp_floor,
     260                 :            :                                         "char* ALIGN_FLOOR offset=%zu inc=%zu align=%zu",
     261                 :            :                                         offset, increment, align);
     262                 :            : 
     263                 :            :                                 cp_result = RTE_PTR_ADD((char *)ubase, increment);
     264                 :            :                                 cp_result = RTE_PTR_ALIGN_CEIL(cp_result, align);
     265                 :            :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)cp_result, uexp_ceil,
     266                 :            :                                         "char* ALIGN_CEIL offset=%zu inc=%zu align=%zu",
     267                 :            :                                         offset, increment, align);
     268                 :            : 
     269                 :            :                                 cp_result = RTE_PTR_ADD((char *)ubase, increment);
     270                 :            :                                 cp_result = RTE_PTR_ALIGN(cp_result, align);
     271                 :            :                                 RTE_TEST_ASSERT_EQUAL((uintptr_t)cp_result, uexp_ceil,
     272                 :            :                                         "char* ALIGN != CEIL offset=%zu inc=%zu align=%zu",
     273                 :            :                                         offset, increment, align);
     274                 :            : 
     275                 :            :                                 /* Test aligned uint16_t* at 2-byte aligned offsets */
     276   [ +  +  +  + ]:     229376 :                                 if (offset % sizeof(uint16_t) == 0 && align >= sizeof(uint16_t)) {
     277                 :            :                                         uint16_t *a16p_result;
     278                 :            : 
     279                 :            :                                         a16p_result = RTE_PTR_ADD((uint16_t *)abase, increment);
     280                 :      98304 :                                         a16p_result = RTE_PTR_ALIGN_FLOOR(a16p_result, align);
     281         [ -  + ]:      98304 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a16p_result, aexp_floor,
     282                 :            :                                                 "uint16_t* ALIGN_FLOOR offset=%zu inc=%zu "
     283                 :            :                                                 "align=%zu", offset, increment, align);
     284         [ -  + ]:      98304 :                                         RTE_TEST_ASSERT_EQUAL(*a16p_result, u16_aval,
     285                 :            :                                                 "uint16_t* ALIGN_FLOOR dereference offset=%zu inc=%zu "
     286                 :            :                                                 "align=%zu", offset, increment, align);
     287                 :            : 
     288                 :            :                                         a16p_result = RTE_PTR_ADD((uint16_t *)abase, increment);
     289                 :      98304 :                                         a16p_result = RTE_PTR_ALIGN_CEIL(a16p_result, align);
     290         [ -  + ]:      98304 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a16p_result, aexp_ceil,
     291                 :            :                                                 "uint16_t* ALIGN_CEIL offset=%zu inc=%zu "
     292                 :            :                                                 "align=%zu", offset, increment, align);
     293         [ -  + ]:      98304 :                                         RTE_TEST_ASSERT_EQUAL(*a16p_result, u16_aval,
     294                 :            :                                                 "uint16_t* ALIGN_CEIL dereference offset=%zu inc=%zu "
     295                 :            :                                                 "align=%zu", offset, increment, align);
     296                 :            : 
     297                 :            :                                         a16p_result = RTE_PTR_ADD((uint16_t *)abase, increment);
     298                 :            :                                         a16p_result = RTE_PTR_ALIGN(a16p_result, align);
     299                 :            :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a16p_result, aexp_ceil,
     300                 :            :                                                 "uint16_t* ALIGN != CEIL offset=%zu inc=%zu "
     301                 :            :                                                 "align=%zu", offset, increment, align);
     302                 :            :                                         RTE_TEST_ASSERT_EQUAL(*a16p_result, u16_aval,
     303                 :            :                                                 "uint16_t* ALIGN dereference offset=%zu inc=%zu "
     304                 :            :                                                 "align=%zu", offset, increment, align);
     305                 :            :                                 }
     306                 :            : 
     307                 :            :                                 /* Test aligned uint32_t* at 4-byte aligned offsets */
     308   [ +  +  +  + ]:     229376 :                                 if (offset % sizeof(uint32_t) == 0 && align >= sizeof(uint32_t)) {
     309                 :            :                                         uint32_t *a32p_result;
     310                 :            : 
     311                 :            :                                         a32p_result = RTE_PTR_ADD((uint32_t *)abase, increment);
     312                 :      40960 :                                         a32p_result = RTE_PTR_ALIGN_FLOOR(a32p_result, align);
     313         [ -  + ]:      40960 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a32p_result, aexp_floor,
     314                 :            :                                                 "uint32_t* ALIGN_FLOOR offset=%zu inc=%zu "
     315                 :            :                                                 "align=%zu", offset, increment, align);
     316         [ -  + ]:      40960 :                                         RTE_TEST_ASSERT_EQUAL(*a32p_result, u32_aval,
     317                 :            :                                                 "uint32_t* ALIGN_FLOOR dereference offset=%zu inc=%zu "
     318                 :            :                                                 "align=%zu", offset, increment, align);
     319                 :            : 
     320                 :            :                                         a32p_result = RTE_PTR_ADD((uint32_t *)abase, increment);
     321                 :      40960 :                                         a32p_result = RTE_PTR_ALIGN_CEIL(a32p_result, align);
     322         [ -  + ]:      40960 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a32p_result, aexp_ceil,
     323                 :            :                                                 "uint32_t* ALIGN_CEIL offset=%zu inc=%zu "
     324                 :            :                                                 "align=%zu", offset, increment, align);
     325         [ -  + ]:      40960 :                                         RTE_TEST_ASSERT_EQUAL(*a32p_result, u32_aval,
     326                 :            :                                                 "uint32_t* ALIGN_CEIL dereference offset=%zu inc=%zu "
     327                 :            :                                                 "align=%zu", offset, increment, align);
     328                 :            : 
     329                 :            :                                         a32p_result = RTE_PTR_ADD((uint32_t *)abase, increment);
     330                 :            :                                         a32p_result = RTE_PTR_ALIGN(a32p_result, align);
     331                 :            :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a32p_result, aexp_ceil,
     332                 :            :                                                 "uint32_t* ALIGN != CEIL offset=%zu inc=%zu "
     333                 :            :                                                 "align=%zu", offset, increment, align);
     334                 :            :                                         RTE_TEST_ASSERT_EQUAL(*a32p_result, u32_aval,
     335                 :            :                                                 "uint32_t* ALIGN dereference offset=%zu inc=%zu "
     336                 :            :                                                 "align=%zu", offset, increment, align);
     337                 :            :                                 }
     338                 :            : 
     339                 :            :                                 /* Test aligned uint64_t* at 8-byte aligned offsets */
     340   [ +  +  +  + ]:     229376 :                                 if (offset % sizeof(uint64_t) == 0 && align >= sizeof(uint64_t)) {
     341                 :            :                                         uint64_t *a64p_result;
     342                 :            : 
     343                 :            :                                         a64p_result = RTE_PTR_ADD((uint64_t *)abase, increment);
     344                 :      16384 :                                         a64p_result = RTE_PTR_ALIGN_FLOOR(a64p_result, align);
     345         [ -  + ]:      16384 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a64p_result, aexp_floor,
     346                 :            :                                                 "uint64_t* ALIGN_FLOOR offset=%zu inc=%zu "
     347                 :            :                                                 "align=%zu", offset, increment, align);
     348         [ -  + ]:      16384 :                                         RTE_TEST_ASSERT_EQUAL(*a64p_result, u64_aval,
     349                 :            :                                                 "uint64_t* ALIGN_FLOOR dereference offset=%zu inc=%zu "
     350                 :            :                                                 "align=%zu", offset, increment, align);
     351                 :            : 
     352                 :            :                                         a64p_result = RTE_PTR_ADD((uint64_t *)abase, increment);
     353                 :      16384 :                                         a64p_result = RTE_PTR_ALIGN_CEIL(a64p_result, align);
     354         [ -  + ]:      16384 :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a64p_result, aexp_ceil,
     355                 :            :                                                 "uint64_t* ALIGN_CEIL offset=%zu inc=%zu "
     356                 :            :                                                 "align=%zu", offset, increment, align);
     357         [ -  + ]:      16384 :                                         RTE_TEST_ASSERT_EQUAL(*a64p_result, u64_aval,
     358                 :            :                                                 "uint64_t* ALIGN_CEIL dereference offset=%zu inc=%zu "
     359                 :            :                                                 "align=%zu", offset, increment, align);
     360                 :            : 
     361                 :            :                                         a64p_result = RTE_PTR_ADD((uint64_t *)abase, increment);
     362                 :            :                                         a64p_result = RTE_PTR_ALIGN(a64p_result, align);
     363                 :            :                                         RTE_TEST_ASSERT_EQUAL((uintptr_t)a64p_result, aexp_ceil,
     364                 :            :                                                 "uint64_t* ALIGN != CEIL offset=%zu inc=%zu "
     365                 :            :                                                 "align=%zu", offset, increment, align);
     366                 :            :                                         RTE_TEST_ASSERT_EQUAL(*a64p_result, u64_aval,
     367                 :            :                                                 "uint64_t* ALIGN dereference offset=%zu inc=%zu "
     368                 :            :                                                 "align=%zu", offset, increment, align);
     369                 :            :                                 }
     370                 :            :                         }
     371                 :            :                 }
     372                 :            :         }
     373                 :            : 
     374                 :            :         return 0;
     375                 :            : }
     376                 :            : 
     377                 :            : static int
     378                 :          1 : test_ptr_align_edge_cases(void)
     379                 :            : {
     380                 :            :         /* Ensure BOUNDARY_TEST_OFFSET is large enough to prevent overflow in CEIL test */
     381                 :            :         /* near_max + CACHE_LINE_ALIGN - 1 must not wrap, so
     382                 :            :          * BOUNDARY_TEST_OFFSET >= CACHE_LINE_ALIGN.
     383                 :            :          */
     384                 :            :         RTE_BUILD_BUG_ON(RTE_COMMON_TEST_BOUNDARY_TEST_OFFSET < RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     385                 :            : 
     386                 :            :         alignas(RTE_CACHE_LINE_SIZE) char test_buffer[RTE_COMMON_TEST_EDGE_CASE_BUFFER_SIZE];
     387                 :            :         void *result;
     388                 :            :         uint64_t *typed_result;
     389                 :            : 
     390                 :            :         /* Initialize buffer */
     391                 :            :         memset(test_buffer, 0xAA, sizeof(test_buffer));
     392                 :            : 
     393                 :            :         /* Test 1: Very large alignment values (page size and beyond) */
     394                 :          1 :         const size_t large_alignments[] = {RTE_COMMON_TEST_PAGE_SIZE,
     395                 :            :                                             RTE_COMMON_TEST_DOUBLE_PAGE_SIZE};
     396         [ +  + ]:          3 :         for (size_t i = 0; i < RTE_DIM(large_alignments); i++) {
     397                 :          2 :                 size_t align = large_alignments[i];
     398                 :            :                 void *unaligned_ptr = test_buffer + 1;  /* Intentionally misaligned by 1 byte */
     399                 :            : 
     400                 :            :                 /* Ensure buffer is large enough for this alignment */
     401         [ -  + ]:          2 :                 RTE_TEST_ASSERT(align <= RTE_COMMON_TEST_EDGE_CASE_BUFFER_SIZE,
     402                 :            :                         "Buffer too small for alignment %zu", align);
     403                 :            : 
     404                 :          2 :                 result = RTE_PTR_ALIGN_FLOOR(unaligned_ptr, align);
     405         [ -  + ]:          2 :                 RTE_TEST_ASSERT((uintptr_t)result % align == 0,
     406                 :            :                         "FLOOR with alignment %zu not aligned", align);
     407         [ -  + ]:          2 :                 RTE_TEST_ASSERT(result <= unaligned_ptr,
     408                 :            :                         "FLOOR with alignment %zu went forward", align);
     409                 :            : 
     410                 :          2 :                 result = RTE_PTR_ALIGN_CEIL(unaligned_ptr, align);
     411         [ -  + ]:          2 :                 RTE_TEST_ASSERT((uintptr_t)result % align == 0,
     412                 :            :                         "CEIL with alignment %zu not aligned", align);
     413         [ -  + ]:          2 :                 RTE_TEST_ASSERT(result >= unaligned_ptr,
     414                 :            :                         "CEIL with alignment %zu went backward", align);
     415                 :            :         }
     416                 :            : 
     417                 :            :         /* Test 2: Address space boundary arithmetic (no dereferencing) */
     418                 :            :         /* Test FLOOR lower bound - pointer near zero */
     419                 :            :         /* Dynamically compute offset that allows FLOOR to align down without underflow */
     420                 :            :         uintptr_t near_zero = RTE_COMMON_TEST_BOUNDARY_TEST_OFFSET;
     421                 :            :         void *low_ptr = (void *)near_zero;
     422                 :            :         uintptr_t expected_floor = (near_zero / RTE_COMMON_TEST_CACHE_LINE_ALIGN) *
     423                 :            :                                    RTE_COMMON_TEST_CACHE_LINE_ALIGN;
     424                 :            : 
     425                 :            :         result = RTE_PTR_ALIGN_FLOOR(low_ptr, RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     426                 :            :         RTE_TEST_ASSERT((uintptr_t)result % RTE_COMMON_TEST_CACHE_LINE_ALIGN == 0,
     427                 :            :                 "Low address FLOOR not aligned to %d", RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     428                 :            :         RTE_TEST_ASSERT((uintptr_t)result == expected_floor,
     429                 :            :                 "Low address FLOOR computed incorrectly: got %p, expected %p",
     430                 :            :                 result, (void *)expected_floor);
     431                 :            :         RTE_TEST_ASSERT((uintptr_t)result <= near_zero,
     432                 :            :                 "Low address FLOOR went forward");
     433                 :            : 
     434                 :            :         /* Test CEIL upper bound - pointer near UINTPTR_MAX */
     435                 :            :         /* Compute offset that allows CEIL to align up without wrapping */
     436                 :            :         /* Ensure no overflow: near_max + CACHE_LINE_ALIGN - 1 must not wrap */
     437                 :            :         uintptr_t near_max = UINTPTR_MAX - RTE_COMMON_TEST_BOUNDARY_TEST_OFFSET;
     438                 :            :         void *high_ptr = (void *)near_max;
     439                 :            :         uintptr_t expected_ceil = ((near_max + RTE_COMMON_TEST_CACHE_LINE_ALIGN - 1) /
     440                 :            :                                    RTE_COMMON_TEST_CACHE_LINE_ALIGN) *
     441                 :            :                                   RTE_COMMON_TEST_CACHE_LINE_ALIGN;
     442                 :            : 
     443                 :            :         result = RTE_PTR_ALIGN_CEIL(high_ptr, RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     444                 :            :         RTE_TEST_ASSERT((uintptr_t)result % RTE_COMMON_TEST_CACHE_LINE_ALIGN == 0,
     445                 :            :                 "High address CEIL not aligned to %d", RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     446                 :            :         RTE_TEST_ASSERT((uintptr_t)result == expected_ceil,
     447                 :            :                 "High address CEIL computed incorrectly: got %p, expected %p",
     448                 :            :                 result, (void *)expected_ceil);
     449                 :            :         RTE_TEST_ASSERT((uintptr_t)result >= near_max,
     450                 :            :                 "High address CEIL went backward");
     451                 :            : 
     452                 :            :         /* Test 3: Type preservation with extreme alignments */
     453                 :            :         /* Test CEIL with PAGE_SIZE - aligns upward into buffer */
     454                 :            :         typed_result = (uint64_t *)test_buffer;
     455                 :          1 :         typed_result = RTE_PTR_ALIGN_CEIL(typed_result, RTE_COMMON_TEST_PAGE_SIZE);
     456         [ -  + ]:          1 :         RTE_TEST_ASSERT((uintptr_t)typed_result % RTE_COMMON_TEST_PAGE_SIZE == 0,
     457                 :            :                 "CEIL type preservation failed with PAGE_SIZE alignment");
     458         [ -  + ]:          1 :         RTE_TEST_ASSERT((uintptr_t)typed_result <
     459                 :            :                         (uintptr_t)test_buffer + RTE_COMMON_TEST_EDGE_CASE_BUFFER_SIZE,
     460                 :            :                 "CEIL went beyond buffer bounds");
     461                 :            :         /* Verify we can dereference as uint64_t* (compiler should allow this) */
     462                 :          1 :         *typed_result = 0x123456789ABCDEF0ULL;
     463                 :            :         RTE_TEST_ASSERT(*typed_result == 0x123456789ABCDEF0ULL,
     464                 :            :                 "CEIL type-preserved pointer dereference failed");
     465                 :            : 
     466                 :            :         /* Test FLOOR with CACHE_LINE_ALIGN - buffer is guaranteed cache-line aligned */
     467                 :            :         /* Use cache line alignment since buffer is only guaranteed RTE_CACHE_LINE_SIZE aligned */
     468                 :            :         typed_result = (uint64_t *)(test_buffer + RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     469                 :          1 :         typed_result = RTE_PTR_ALIGN_FLOOR(typed_result, RTE_COMMON_TEST_CACHE_LINE_ALIGN);
     470                 :            :         RTE_TEST_ASSERT((uintptr_t)typed_result % RTE_COMMON_TEST_CACHE_LINE_ALIGN == 0,
     471                 :            :                 "FLOOR type preservation failed with CACHE_LINE alignment");
     472                 :            :         RTE_TEST_ASSERT((uintptr_t)typed_result >= (uintptr_t)test_buffer,
     473                 :            :                 "FLOOR went before buffer start");
     474         [ -  + ]:          1 :         RTE_TEST_ASSERT((uintptr_t)typed_result <
     475                 :            :                         (uintptr_t)test_buffer + RTE_COMMON_TEST_EDGE_CASE_BUFFER_SIZE,
     476                 :            :                 "FLOOR went beyond buffer bounds");
     477                 :            :         /* Safe to dereference now */
     478                 :            :         *typed_result = 0xDEADBEEFCAFEBABEULL;
     479                 :            :         RTE_TEST_ASSERT(*typed_result == 0xDEADBEEFCAFEBABEULL,
     480                 :            :                 "FLOOR type-preserved pointer dereference failed");
     481                 :            : 
     482                 :            :         return 0;
     483                 :            : }
     484                 :            : 
     485                 :            : /* this is really a sanity check */
     486                 :            : static int
     487                 :          1 : test_macros(void)
     488                 :            : {
     489                 :            : #define SMALLER 0x1000U
     490                 :            : #define BIGGER 0x2000U
     491                 :            : #define PTR_DIFF BIGGER - SMALLER
     492                 :            : 
     493                 :            :         uintptr_t unused = 0;
     494                 :            :         unsigned int smaller = SMALLER, bigger  = BIGGER;
     495                 :            :         uint32_t arr[3];
     496                 :            : 
     497                 :            :         RTE_SET_USED(unused);
     498                 :            : 
     499                 :            :         RTE_SWAP(smaller, bigger);
     500                 :            :         RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
     501                 :            :                 "RTE_SWAP");
     502                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
     503                 :            :                 "RTE_PTR_DIFF");
     504                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
     505                 :            :                 "RTE_MAX");
     506                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_MIN(SMALLER, BIGGER), SMALLER,
     507                 :            :                 "RTE_MIN");
     508                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_DIV_ROUND_UP(10, 5), 2,
     509                 :            :                 "RTE_DIV_ROUND_UP exact");
     510                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_DIV_ROUND_UP(11, 5), 3,
     511                 :            :                 "RTE_DIV_ROUND_UP remainder");
     512                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_DIV_ROUND_UP(0, 5), 0,
     513                 :            :                 "RTE_DIV_ROUND_UP n=0");
     514                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_DIV_ROUND_UP(0, 1), 0,
     515                 :            :                 "RTE_DIV_ROUND_UP n=0, d=1");
     516                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_DIV_ROUND_UP(5, 1), 5,
     517                 :            :                 "RTE_DIV_ROUND_UP d=1");
     518                 :            : 
     519                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ADD(arr + 1, sizeof(arr[0])), &arr[2],
     520                 :            :                 "RTE_PTR_ADD(expr, x)");
     521                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_SUB(arr + 1, sizeof(arr[0])), &arr[0],
     522                 :            :                 "RTE_PTR_SUB(expr, x)");
     523                 :          1 :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_FLOOR(arr + 2, 4), &arr[2],
     524                 :            :                 "RTE_PTR_ALIGN_FLOOR(expr, x)");
     525         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_CEIL(arr + 2, 4), &arr[2],
     526                 :            :                 "RTE_PTR_ALIGN_CEIL(expr, x)");
     527                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN(arr + 2, 4), &arr[2],
     528                 :            :                 "RTE_PTR_ALIGN(expr, x)");
     529                 :            : 
     530                 :            :         RTE_TEST_ASSERT_EQUAL(
     531                 :            :                 RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 1), 4), &arr[1],
     532                 :            :                 "RTE_PTR_ALIGN_FLOOR(x < y/2, y)");
     533                 :          1 :         RTE_TEST_ASSERT_EQUAL(
     534                 :            :                 RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 3), 4), &arr[1],
     535                 :            :                 "RTE_PTR_ALIGN_FLOOR(x > y/2, y)");
     536         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(
     537                 :            :                 RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 3), 4), &arr[2],
     538                 :            :                 "RTE_PTR_ALIGN_CEIL(x < y/2, y)");
     539                 :            :         RTE_TEST_ASSERT_EQUAL(
     540                 :            :                 RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 1), 4), &arr[2],
     541                 :            :                 "RTE_PTR_ALIGN_CEIL(x > y/2, y)");
     542                 :            : 
     543                 :            :         RTE_TEST_ASSERT(strncmp(RTE_STR(test), "test", sizeof("test")) == 0,
     544                 :            :                 "RTE_STR");
     545                 :            : 
     546                 :            :         return 0;
     547                 :            : }
     548                 :            : 
     549                 :            : static int
     550                 :          1 : test_bsf(void)
     551                 :            : {
     552                 :            :         uint32_t shift, pos;
     553                 :            : 
     554                 :            :         /* safe versions should be able to handle 0 */
     555                 :            :         if (rte_bsf32_safe(0, &pos) != 0)
     556                 :            :                 FAIL("rte_bsf32_safe");
     557                 :            :         if (rte_bsf64_safe(0, &pos) != 0)
     558                 :            :                 FAIL("rte_bsf64_safe");
     559                 :            : 
     560         [ +  + ]:         64 :         for (shift = 0; shift < 63; shift++) {
     561                 :            :                 uint32_t val32;
     562                 :            :                 uint64_t val64;
     563                 :            : 
     564         [ -  + ]:         63 :                 val64 = 1ULL << shift;
     565         [ -  + ]:         63 :                 if ((uint32_t)rte_bsf64(val64) != shift)
     566                 :          0 :                         FAIL("rte_bsf64");
     567                 :            :                 if (rte_bsf64_safe(val64, &pos) != 1)
     568                 :            :                         FAIL("rte_bsf64_safe");
     569                 :            :                 if (pos != shift)
     570                 :            :                         FAIL("rte_bsf64_safe");
     571                 :            : 
     572         [ +  + ]:         63 :                 if (shift > 31)
     573                 :         31 :                         continue;
     574                 :            : 
     575         [ -  + ]:         32 :                 val32 = 1U << shift;
     576         [ -  + ]:         32 :                 if ((uint32_t)rte_bsf32(val32) != shift)
     577                 :          0 :                         FAIL("rte_bsf32");
     578                 :            :                 if (rte_bsf32_safe(val32, &pos) != 1)
     579                 :            :                         FAIL("rte_bsf32_safe");
     580                 :            :                 if (pos != shift)
     581                 :            :                         FAIL("rte_bsf32_safe");
     582                 :            :         }
     583                 :            : 
     584                 :            :         return 0;
     585                 :            : }
     586                 :            : 
     587                 :            : static int
     588                 :          1 : test_misc(void)
     589                 :            : {
     590                 :          1 :         char memdump[] = "memdump_test";
     591                 :            : 
     592                 :          1 :         rte_memdump(stdout, "test", memdump, sizeof(memdump));
     593                 :          1 :         rte_hexdump(stdout, "test", memdump, sizeof(memdump));
     594                 :            : 
     595                 :            :         rte_pause();
     596                 :            : 
     597                 :          1 :         return 0;
     598                 :            : }
     599                 :            : 
     600                 :            : static int
     601                 :          1 : test_align(void)
     602                 :            : {
     603                 :            : #define FAIL_ALIGN(x, i, p)\
     604                 :            :         {printf(x "() test failed: %u %u\n", i, p);\
     605                 :            :         return -1;}
     606                 :            : #define FAIL_ALIGN64(x, j, q)\
     607                 :            :         {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
     608                 :            :         return -1; }
     609                 :            : #define ERROR_FLOOR(res, i, pow) \
     610                 :            :                 (res % pow) ||                                          /* check if not aligned */ \
     611                 :            :                 ((res / pow) != (i / pow))              /* check if correct alignment */
     612                 :            : #define ERROR_CEIL(res, i, pow) \
     613                 :            :                 (res % pow) ||                                          /* check if not aligned */ \
     614                 :            :                         ((i % pow) == 0 ?                               /* check if ceiling is invoked */ \
     615                 :            :                         val / pow != i / pow :                  /* if aligned */ \
     616                 :            :                         val / pow != (i / pow) + 1)             /* if not aligned, hence +1 */
     617                 :            : 
     618                 :            :         uint32_t i, p, val;
     619                 :            :         uint64_t j, q;
     620                 :            : 
     621         [ +  + ]:    1048577 :         for (i = 1, p = 1; i <= MAX_NUM; i ++) {
     622         [ -  + ]:    1048576 :                 if (rte_align32pow2(i) != p)
     623                 :          0 :                         FAIL_ALIGN("rte_align32pow2", i, p);
     624         [ +  + ]:    1048576 :                 if (i == p)
     625                 :         21 :                         p <<= 1;
     626                 :            :         }
     627                 :            : 
     628         [ +  + ]:    1048577 :         for (i = 1, p = 1; i <= MAX_NUM; i++) {
     629         [ -  + ]:    1048576 :                 if (rte_align32prevpow2(i) != p)
     630                 :          0 :                         FAIL_ALIGN("rte_align32prevpow2", i, p);
     631         [ +  + ]:    1048576 :                 if (rte_is_power_of_2(i + 1))
     632                 :            :                         p = i + 1;
     633                 :            :         }
     634                 :            : 
     635         [ +  + ]:    1048577 :         for (j = 1, q = 1; j <= MAX_NUM ; j++) {
     636         [ -  + ]:    1048576 :                 if (rte_align64pow2(j) != q)
     637                 :          0 :                         FAIL_ALIGN64("rte_align64pow2", j, q);
     638         [ +  + ]:    1048576 :                 if (j == q)
     639                 :         21 :                         q <<= 1;
     640                 :            :         }
     641                 :            : 
     642         [ +  + ]:    1048577 :         for (j = 1, q = 1; j <= MAX_NUM ; j++) {
     643         [ -  + ]:    1048576 :                 if (rte_align64prevpow2(j) != q)
     644                 :          0 :                         FAIL_ALIGN64("rte_align64prevpow2", j, q);
     645         [ +  + ]:    1048576 :                 if (rte_is_power_of_2(j + 1))
     646                 :         20 :                         q = j + 1;
     647                 :            :         }
     648                 :            : 
     649         [ +  + ]:         21 :         for (p = 2; p <= MAX_NUM; p <<= 1) {
     650                 :            : 
     651                 :            :                 if (!rte_is_power_of_2(p))
     652                 :          0 :                         FAIL("rte_is_power_of_2");
     653                 :            : 
     654         [ +  + ]:   20971540 :                 for (i = 1; i <= MAX_NUM; i++) {
     655                 :            :                         /* align floor */
     656         [ -  + ]:   20971520 :                         if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
     657                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
     658                 :            : 
     659                 :   20971520 :                         val = RTE_ALIGN_FLOOR(i, p);
     660   [ +  -  -  + ]:   20971520 :                         if (ERROR_FLOOR(val, i, p))
     661                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
     662                 :            : 
     663                 :            :                         /* align ceiling */
     664                 :   20971520 :                         val = RTE_ALIGN(i, p);
     665   [ +  -  +  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
             -  +  -  + ]
     666                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN", i, p);
     667                 :            : 
     668                 :            :                         val = RTE_ALIGN_CEIL(i, p);
     669   [ +  +  -  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
                   -  + ]
     670                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
     671                 :            : 
     672                 :            :                         /* by this point we know that val is aligned to p */
     673         [ -  + ]:   20971520 :                         if (!rte_is_aligned((void*)(uintptr_t) val, p))
     674                 :          0 :                                 FAIL("rte_is_aligned");
     675                 :            :                 }
     676                 :            :         }
     677                 :            : 
     678                 :            :         /* testing the whole space of 2^20^2 takes too long. */
     679         [ +  + ]:    1048577 :         for (j = 1; j <= MAX_NUM ; j++) {
     680                 :    1048576 :                 i = rte_rand_max(MAX_NUM - 1) + 1;
     681                 :    1048576 :                 p = rte_rand_max(MAX_NUM - 1) + 1;
     682                 :            : 
     683                 :    1048576 :                 val = RTE_ALIGN_MUL_CEIL(i, p);
     684   [ +  -  -  + ]:    1048576 :                 if (val % p != 0 || val < i)
     685                 :          0 :                         FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
     686                 :    1048576 :                 val = RTE_ALIGN_MUL_FLOOR(i, p);
     687   [ +  -  -  + ]:    1048576 :                 if (val % p != 0 || val > i)
     688                 :          0 :                         FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
     689         [ +  + ]:    1048576 :                 val = RTE_ALIGN_MUL_NEAR(i, p);
     690         [ +  - ]:    1048576 :                 if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
     691         [ -  + ]:    1048576 :                                      & (val != RTE_ALIGN_MUL_FLOOR(i, p))))
     692                 :          0 :                         FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
     693                 :            :         }
     694                 :            : 
     695                 :            :         return 0;
     696                 :            : }
     697                 :            : 
     698                 :            : static int
     699                 :          1 : test_log2(void)
     700                 :            : {
     701                 :            :         uint32_t i, base, compare;
     702                 :            :         const uint32_t max = 0x10000;
     703                 :            :         const uint32_t step = 1;
     704                 :            : 
     705                 :            :         compare = rte_log2_u32(0);
     706                 :            :         if (compare != 0) {
     707                 :            :                 printf("Wrong rte_log2_u32(0) val %x, expected 0\n", compare);
     708                 :            :                 return TEST_FAILED;
     709                 :            :         }
     710                 :            : 
     711                 :            :         compare = rte_log2_u64(0);
     712                 :            :         if (compare != 0) {
     713                 :            :                 printf("Wrong rte_log2_u64(0) val %x, expected 0\n", compare);
     714                 :            :                 return TEST_FAILED;
     715                 :            :         }
     716                 :            : 
     717         [ +  + ]:      65536 :         for (i = 1; i < max; i = i + step) {
     718                 :            :                 uint64_t i64;
     719                 :            : 
     720                 :            :                 /* extend range for 64-bit */
     721                 :      65535 :                 i64 = (uint64_t)i << 32;
     722         [ -  + ]:      65535 :                 base = (uint32_t)ceilf(log2(i64));
     723                 :            :                 compare = rte_log2_u64(i64);
     724         [ -  + ]:      65535 :                 if (base != compare) {
     725                 :            :                         printf("Wrong rte_log2_u64(%" PRIx64 ") val %x, expected %x\n",
     726                 :            :                                 i64, compare, base);
     727                 :          0 :                         return TEST_FAILED;
     728                 :            :                 }
     729                 :            : 
     730         [ -  + ]:      65535 :                 base = (uint32_t)ceilf(log2((uint32_t)i));
     731                 :            :                 compare = rte_log2_u32((uint32_t)i);
     732         [ -  + ]:      65535 :                 if (base != compare) {
     733                 :            :                         printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
     734                 :            :                                 i, compare, base);
     735                 :          0 :                         return TEST_FAILED;
     736                 :            :                 }
     737                 :            :                 compare = rte_log2_u64((uint64_t)i);
     738         [ -  + ]:      65535 :                 if (base != compare) {
     739                 :            :                         printf("Wrong rte_log2_u64(%x) val %x, expected %x\n",
     740                 :            :                                 i, compare, base);
     741                 :          0 :                         return TEST_FAILED;
     742                 :            :                 }
     743                 :            :         }
     744                 :            :         return 0;
     745                 :            : }
     746                 :            : 
     747                 :            : static int
     748                 :          1 : test_fls(void)
     749                 :            : {
     750                 :            :         struct fls_test_vector {
     751                 :            :                 uint32_t arg;
     752                 :            :                 int rc;
     753                 :            :         };
     754                 :            :         int expected, rc;
     755                 :            :         uint32_t i, arg;
     756                 :            : 
     757                 :          1 :         const struct fls_test_vector test[] = {
     758                 :            :                 {0x0, 0},
     759                 :            :                 {0x1, 1},
     760                 :            :                 {0x4000, 15},
     761                 :            :                 {0x80000000, 32},
     762                 :            :         };
     763                 :            : 
     764         [ +  + ]:          5 :         for (i = 0; i < RTE_DIM(test); i++) {
     765                 :            :                 uint64_t arg64;
     766                 :            : 
     767         [ +  + ]:          4 :                 arg = test[i].arg;
     768                 :          4 :                 rc = rte_fls_u32(arg);
     769                 :          4 :                 expected = test[i].rc;
     770         [ -  + ]:          4 :                 if (rc != expected) {
     771                 :            :                         printf("Wrong rte_fls_u32(0x%x) rc=%d, expected=%d\n",
     772                 :            :                                 arg, rc, expected);
     773                 :          0 :                         return TEST_FAILED;
     774                 :            :                 }
     775                 :            :                 /* 64-bit version */
     776                 :            :                 arg = test[i].arg;
     777         [ +  + ]:          4 :                 rc = rte_fls_u64(arg);
     778                 :            :                 expected = test[i].rc;
     779         [ -  + ]:          4 :                 if (rc != expected) {
     780                 :            :                         printf("Wrong rte_fls_u64(0x%x) rc=%d, expected=%d\n",
     781                 :            :                                 arg, rc, expected);
     782                 :          0 :                         return TEST_FAILED;
     783                 :            :                 }
     784                 :            :                 /* 64-bit version shifted by 32 bits */
     785         [ +  + ]:          4 :                 arg64 = (uint64_t)test[i].arg << 32;
     786                 :          4 :                 rc = rte_fls_u64(arg64);
     787                 :            :                 /* don't shift zero */
     788         [ +  + ]:          4 :                 expected = test[i].rc == 0 ? 0 : test[i].rc + 32;
     789         [ -  + ]:          4 :                 if (rc != expected) {
     790                 :            :                         printf("Wrong rte_fls_u64(0x%" PRIx64 ") rc=%d, expected=%d\n",
     791                 :            :                                 arg64, rc, expected);
     792                 :          0 :                         return TEST_FAILED;
     793                 :            :                 }
     794                 :            :         }
     795                 :            : 
     796                 :            :         return 0;
     797                 :            : }
     798                 :            : 
     799                 :            : static struct unit_test_suite common_test_suite = {
     800                 :            :         .suite_name = "common autotest",
     801                 :            :         .setup = NULL,
     802                 :            :         .teardown = NULL,
     803                 :            :         .unit_test_cases = {
     804                 :            :                 TEST_CASE(test_ptr_add_sub_align),
     805                 :            :                 TEST_CASE(test_ptr_align_edge_cases),
     806                 :            :                 TEST_CASE(test_align),
     807                 :            :                 TEST_CASE(test_macros),
     808                 :            :                 TEST_CASE(test_misc),
     809                 :            :                 TEST_CASE(test_bsf),
     810                 :            :                 TEST_CASE(test_log2),
     811                 :            :                 TEST_CASE(test_fls),
     812                 :            :                 TEST_CASES_END()
     813                 :            :         }
     814                 :            : };
     815                 :            : 
     816                 :            : static int
     817                 :          1 : test_common(void)
     818                 :            : {
     819                 :          1 :         return unit_test_suite_runner(&common_test_suite);
     820                 :            : }
     821                 :            : 
     822                 :        305 : REGISTER_FAST_TEST(common_autotest, NOHUGE_OK, ASAN_OK, test_common);

Generated by: LCOV version 1.14