Home | History | Annotate | Line # | Download | only in sys
t_bitops.c revision 1.2
      1 /*	$NetBSD: t_bitops.c,v 1.2 2011/03/24 07:06:34 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <atf-c.h>
     33 
     34 #include <sys/cdefs.h>
     35 #include <sys/bitops.h>
     36 
     37 #include <math.h>
     38 
     39 static const struct {
     40 	uint32_t	val;
     41 	int		ffs;
     42 	int		fls;
     43 } bits[] = {
     44 
     45 	{ 0x00, 0, 0 }, { 0x01, 1, 1 },	{ 0x02, 2, 2 },	{ 0x03, 1, 2 },
     46 	{ 0x04, 3, 3 }, { 0x05, 1, 3 },	{ 0x06, 2, 3 },	{ 0x07, 1, 3 },
     47 	{ 0x08, 4, 4 }, { 0x09, 1, 4 },	{ 0x0A, 2, 4 },	{ 0x0B, 1, 4 },
     48 	{ 0x0C, 3, 4 }, { 0x0D, 1, 4 },	{ 0x0E, 2, 4 },	{ 0x0F, 1, 4 },
     49 
     50 	{ 0x10, 5, 5 },	{ 0x11, 1, 5 },	{ 0x12, 2, 5 },	{ 0x13, 1, 5 },
     51 	{ 0x14, 3, 5 },	{ 0x15, 1, 5 },	{ 0x16, 2, 5 },	{ 0x17, 1, 5 },
     52 	{ 0x18, 4, 5 },	{ 0x19, 1, 5 },	{ 0x1A, 2, 5 },	{ 0x1B, 1, 5 },
     53 	{ 0x1C, 3, 5 },	{ 0x1D, 1, 5 },	{ 0x1E, 2, 5 },	{ 0x1F, 1, 5 },
     54 
     55 	{ 0xF0, 5, 8 },	{ 0xF1, 1, 8 },	{ 0xF2, 2, 8 },	{ 0xF3, 1, 8 },
     56 	{ 0xF4, 3, 8 },	{ 0xF5, 1, 8 },	{ 0xF6, 2, 8 },	{ 0xF7, 1, 8 },
     57 	{ 0xF8, 4, 8 },	{ 0xF9, 1, 8 },	{ 0xFA, 2, 8 },	{ 0xFB, 1, 8 },
     58 	{ 0xFC, 3, 8 },	{ 0xFD, 1, 8 },	{ 0xFE, 2, 8 },	{ 0xFF, 1, 8 },
     59 
     60 };
     61 
     62 ATF_TC(ffsfls);
     63 ATF_TC_HEAD(ffsfls, tc)
     64 {
     65 	atf_tc_set_md_var(tc, "descr", "Test ffs32(3)-family for correctness");
     66 }
     67 
     68 ATF_TC_BODY(ffsfls, tc)
     69 {
     70 	uint8_t i;
     71 
     72 	ATF_REQUIRE(ffs32(0) == 0x00);
     73 	ATF_REQUIRE(fls32(0) == 0x00);
     74 	ATF_REQUIRE(ffs64(0) == 0x00);
     75 	ATF_REQUIRE(fls64(0) == 0x00);
     76 
     77 	ATF_REQUIRE(ffs32(UINT32_MAX) == 0x01);
     78 	ATF_REQUIRE(fls32(UINT32_MAX) == 0x20);
     79 	ATF_REQUIRE(ffs64(UINT64_MAX) == 0x01);
     80 	ATF_REQUIRE(fls64(UINT64_MAX) == 0x40);
     81 
     82 	for (i = 1; i < __arraycount(bits); i++) {
     83 
     84 		ATF_REQUIRE(ffs32(bits[i].val) == bits[i].ffs);
     85 		ATF_REQUIRE(fls32(bits[i].val) == bits[i].fls);
     86 		ATF_REQUIRE(ffs64(bits[i].val) == bits[i].ffs);
     87 		ATF_REQUIRE(fls64(bits[i].val) == bits[i].fls);
     88 
     89 		ATF_REQUIRE(ffs32(bits[i].val << 1) == bits[i].ffs + 1);
     90 		ATF_REQUIRE(fls32(bits[i].val << 1) == bits[i].fls + 1);
     91 		ATF_REQUIRE(ffs64(bits[i].val << 1) == bits[i].ffs + 1);
     92 		ATF_REQUIRE(fls64(bits[i].val << 1) == bits[i].fls + 1);
     93 
     94 		ATF_REQUIRE(ffs32(bits[i].val << 9) == bits[i].ffs + 9);
     95 		ATF_REQUIRE(fls32(bits[i].val << 9) == bits[i].fls + 9);
     96 		ATF_REQUIRE(ffs64(bits[i].val << 9) == bits[i].ffs + 9);
     97 		ATF_REQUIRE(fls64(bits[i].val << 9) == bits[i].fls + 9);
     98 	}
     99 }
    100 
    101 ATF_TC(ilog2_1);
    102 ATF_TC_HEAD(ilog2_1, tc)
    103 {
    104 	atf_tc_set_md_var(tc, "descr", "Test ilog2(3) for correctness");
    105 }
    106 
    107 ATF_TC_BODY(ilog2_1, tc)
    108 {
    109 	uint64_t i, x;
    110 
    111 	for (i = x = 0; i < 64; i++) {
    112 
    113 		x = (uint64_t)1 << i;
    114 
    115 		ATF_REQUIRE(i == (uint64_t)ilog2(x));
    116 	}
    117 }
    118 
    119 ATF_TC(ilog2_2);
    120 ATF_TC_HEAD(ilog2_2, tc)
    121 {
    122 	atf_tc_set_md_var(tc, "descr", "Test log2(3) vs. ilog2(3)");
    123 }
    124 
    125 ATF_TC_BODY(ilog2_2, tc)
    126 {
    127 	double  x, y;
    128 	uint64_t i;
    129 
    130 	for (i = 1; i < UINT32_MAX; i += UINT16_MAX) {
    131 
    132 		x = log2(i);
    133 		y = (double)(ilog2(i));
    134 
    135 		ATF_REQUIRE(ceil(x) >= y);
    136 		ATF_REQUIRE(fabs(floor(x) - y) < 1.0e-40);
    137 	}
    138 }
    139 
    140 ATF_TP_ADD_TCS(tp)
    141 {
    142 
    143 	ATF_TP_ADD_TC(tp, ffsfls);
    144 	ATF_TP_ADD_TC(tp, ilog2_1);
    145 	ATF_TP_ADD_TC(tp, ilog2_2);
    146 
    147 	return atf_no_error();
    148 }
    149