/* * Copyright (c) 2016 Nordic Semiconductor ASA * Copyright (c) 2016 Vinayak Kariappa Chettimada * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include "util.h" /** * @brief Population count: Count the number of bits set to 1 * @details * TODO: Faster methods available at [1]. * [1] http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel * * @param octets Data to count over * @param octets_len Must not be bigger than 255/8 = 31 bytes * * @return popcnt of 'octets' */ u8_t util_ones_count_get(u8_t *octets, u8_t octets_len) { u8_t one_count = 0U; while (octets_len--) { u8_t bite; bite = *octets; while (bite) { bite &= (bite - 1); one_count++; } octets++; } return one_count; } int util_rand(void *buf, size_t len) { static struct device *dev; if (unlikely(!dev)) { /* Only one entropy device exists, so this is safe even * if the whole operation isn't atomic. */ dev = device_get_binding(CONFIG_ENTROPY_NAME); __ASSERT((dev != NULL), "Device driver for %s (CONFIG_ENTROPY_NAME) not found. " "Check your build configuration!", CONFIG_ENTROPY_NAME); } return entropy_get_entropy(dev, (u8_t *)buf, len); }