static uint32_t derive_interval(uint32_t num) { uint32_t interval; if ((DETAILED_STATS) && (LOG2_STATS)) { /* Use a logarithmic method to generate geometric variates in order to * populate the result table evenly across all buckets */ /* Static exponent mask, picking the mask as tightly as possible reduces the * probability of discarded values, which reduces the computing overhead * between test iterations */ static uint32_t exp_mask = 0; if (exp_mask == 0) { /* non-constant initializer */ exp_mask = (2 << bitarithm_msb(TEST_LOG2NUM)) - 1; print_str("exp_mask = "); print_u32_hex(exp_mask); print("\n", 1); print_str("max interval = "); print_u32_dec((2 << exp_mask) - 1); print("\n", 1); } /* Pick an exponent based on the top bits of the number */ /* exponent will be a number in the interval [0, log2(TEST_NUM) + 1] */ unsigned int exponent = ((num >> (32 - 8)) & exp_mask); if (exponent == 0) { /* Special handling to avoid the situation where we never see a zero */ /* We could also have used an extra right shift in the else case, * but the state grouping also groups 0 and 1 in the same bucket, which means that they are twice as likely */ interval = bitarithm_bits_set(num) & 1; } else { interval = (1 << exponent); interval |= (num & (interval - 1)); } } else {
static void test_bitarithm_bits_set_random(void) { TEST_ASSERT_EQUAL_INT(3, bitarithm_bits_set(7)); /* randomized by fair dice roll ;-) */ }
static void test_bitarithm_bits_set_one(void) { TEST_ASSERT_EQUAL_INT(1, bitarithm_bits_set(1)); }
static void test_bitarithm_bits_set_limit(void) { TEST_ASSERT_EQUAL_INT(sizeof(unsigned) * 8, bitarithm_bits_set(UINT_MAX)); }
static void test_bitarithm_bits_set_null(void) { TEST_ASSERT_EQUAL_INT(0, bitarithm_bits_set(0)); }