/*-1* Copyright (c) 2023 The FreeBSD Foundation2*3* This software was developed by Robert Clausecker <[email protected]>4* under sponsorship from the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE26*/2728#include <atf-c.h>29#include <limits.h>30#include <stdint.h>31#include <strings.h>3233#ifndef FLS34# define FLS fls35# define TYPE int36# define TYPE_MIN INT_MIN37#endif3839ATF_TC_WITHOUT_HEAD(zero);40ATF_TC_BODY(zero, tc)41{42ATF_CHECK_EQ((TYPE)0, FLS(0));43}4445ATF_TC_WITHOUT_HEAD(twobit);46ATF_TC_BODY(twobit, tc)47{48const TYPE one = 1;49TYPE x;50const int n = sizeof(TYPE) * CHAR_BIT;51int i, j;5253for (i = 0; i < n - 1; i++)54for (j = 0; j <= i; j++) {55x = one << i | one << j;56ATF_CHECK_EQ_MSG(i + 1, FLS(x),57"%s(%#jx) == %d != %d", __STRING(FLS), (intmax_t)x, FLS(x), i + 1);58}59}6061ATF_TC_WITHOUT_HEAD(twobitneg);62ATF_TC_BODY(twobitneg, tc)63{64const TYPE one = 1;65TYPE x;66const int n = sizeof(TYPE) * CHAR_BIT;67int i, j;6869for (i = 0; i < n - 1; i++)70for (j = 0; j <= i; j++) {71x = one << i | one << j | TYPE_MIN;72ATF_CHECK_EQ_MSG(n, FLS(x),73"%s(%#jx) == %d != %d", __STRING(FLS), (intmax_t)x, FLS(x), n);74}75}767778ATF_TP_ADD_TCS(tp)79{8081ATF_TP_ADD_TC(tp, zero);82ATF_TP_ADD_TC(tp, twobit);83ATF_TP_ADD_TC(tp, twobitneg);8485return (atf_no_error());86}878889