Commit 35180543 authored by Emil Gydesen's avatar Emil Gydesen Committed by Benjamin Cabé
Browse files

Revert "include: util: Add generic function to count bits set in a value"

This reverts commit d05de070.

Signed-off-by: default avatarEmil Gydesen <emil.gydesen@nordicsemi.no>
parent fb68b4be
Loading
Loading
Loading
Loading
+0 −4
Changes for doc/releases/release-notes-4.2.rst: 0 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -102,10 +102,6 @@ New APIs and options

    * LE Connection Subrating is no longer experimental.

* Other

  * :c:func:`count_bits`

New Boards
**********

+0 −28
Changes for include/zephyr/sys/util.h: 0 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -783,34 +783,6 @@ static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const ui
	mem_xor_n(dst, src1, src2, 16);
}

/**
 * @brief Returns the number of bits set in a value
 *
 * @param value The value to count number of bits set of
 * @param len The number of octets in @p value
 */
static inline size_t count_bits(const void *value, size_t len)
{
	const uint8_t *value_u8 = (const uint8_t *)value;
	size_t cnt = 0U;

	for (size_t i = 0U; i < len; i++) {
		if (value_u8[i] != 0U) {
#ifdef POPCOUNT
			cnt += POPCOUNT(value_u8[i]);
#else
			/* Implements Brian Kernighan’s Algorithm to count bits */
			while (value_u8[i]) {
				cnt += value_u8[i] & 1;
				value_u8[i] >>= 1;
			}
#endif
		}
	}

	return cnt;
}

#ifdef __cplusplus
}
#endif
+2 −26
Changes for tests/unit/util/main.c: 2 added lines, 26 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2019 Oticon A/S
 * Copyright (c) 2025 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
#include <zephyr/ztest_assert.h>
#include <zephyr/ztest_test.h>
#include <stdio.h>
#include <string.h>

ZTEST(util, test_u8_to_dec) {
	char text[4];
@@ -773,25 +768,6 @@ ZTEST(util, test_mem_xor_128)
	zassert_mem_equal(expected_result, dst, 16);
}

ZTEST(util, test_count_bits)
{
	uint8_t zero = 0U;
	uint8_t u8 = 29U;
	uint16_t u16 = 29999U;
	uint32_t u32 = 2999999999U;
	uint64_t u64 = 123456789012345ULL;
	uint8_t u8_arr[] = {u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
			    u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8};

	zassert_equal(count_bits(&zero, sizeof(zero)), 0);
	zassert_equal(count_bits(&u8, sizeof(u8)), 4);
	zassert_equal(count_bits(&u16, sizeof(u16)), 10);
	zassert_equal(count_bits(&u32, sizeof(u32)), 20);
	zassert_equal(count_bits(&u64, sizeof(u64)), 23);

	zassert_equal(count_bits(u8_arr, sizeof(u8_arr)), 128);
}

ZTEST(util, test_CONCAT)
{
#define _CAT_PART1 1