Path: blob/main/lib/libc/tests/stdbit/stdbit-test-kernel.c
96309 views
/*1* Copyright (c) 2025 Robert Clausecker <[email protected]>2*3* SPDX-License-Identifier: BSD-2-Clause4*/56/*7* test kernel for stdbit functions.8* Requires the following macros to be defined:9*10* FUNCSTEM -- stem of the function name11* SUFFIX -- type suffic12* TYPE -- argument type13* MKREFFUNC(ref, type) -- reference function builder14*/1516#define FUNC __CONCAT(FUNCSTEM, SUFFIX)17#define REF __CONCAT(FUNCSTEM, __CONCAT(SUFFIX, _ref))1819MKREFFUNC(REF, TYPE)2021ATF_TC_WITHOUT_HEAD1(FUNCSTEM, SUFFIX);22ATF_TC_BODY1(FUNCSTEM, SUFFIX, tc)23{24uintmax_t has, want;25size_t i, j;26TYPE value;2728/* test all single-bit patterns */29for (i = 0; i < TYPE_WIDTH; i++) {30value = (TYPE)1 << i;31has = FUNC(value);32want = REF(value);33ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",34__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);35}3637/* test all double-bit patterns */38for (i = 0; i < TYPE_WIDTH; i++) {39for (j = 0; j < i; j++) {40value = (TYPE)1 << i | (TYPE)1 << j;41has = FUNC(value);42want = REF(value);43ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",44__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);45}46}4748/* test all barber-pole patterns */49value = ~(TYPE)0;50for (i = 0; i < TYPE_WIDTH; i++) {51has = FUNC(value);52want = REF(value);53ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",54__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);5556value = ~value;57has = FUNC(value);58want = REF(value);59ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",60__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);6162value = ~value << 1;63}64}6566#undef REF67#undef FUNC686970