1139790Simp/*-
297261Sjake * Copyright (c) 2001 Jake Burkholder <jake@FreeBSD.org>
397261Sjake * All rights reserved.
497261Sjake *
597261Sjake * Redistribution and use in source and binary forms, with or without
697261Sjake * modification, are permitted provided that the following conditions
797261Sjake * are met:
897261Sjake * 1. Redistributions of source code must retain the above copyright
997261Sjake *    notice, this list of conditions and the following disclaimer.
1097261Sjake * 2. Redistributions in binary form must reproduce the above copyright
1197261Sjake *    notice, this list of conditions and the following disclaimer in the
1297261Sjake *    documentation and/or other materials provided with the distribution.
1397261Sjake *
1497261Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1597261Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1697261Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1797261Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1897261Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1997261Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2097261Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2197261Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2297261Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2397261Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2497261Sjake * SUCH DAMAGE.
2597261Sjake *
2697261Sjake * $FreeBSD$
2797261Sjake */
2897261Sjake
2997261Sjake#ifndef	_MACHINE_RUNQ_H_
3097261Sjake#define	_MACHINE_RUNQ_H_
3197261Sjake
3297261Sjake#define	RQB_LEN		(1UL)		/* Number of priority status words. */
3397261Sjake#define	RQB_L2BPW	(6UL)		/* Log2(sizeof(rqb_word_t) * NBBY)). */
3497261Sjake#define	RQB_BPW		(1UL<<RQB_L2BPW)	/* Bits in an rqb_word_t. */
3597261Sjake
3697261Sjake#define	RQB_BIT(pri)	(1UL << ((pri) & (RQB_BPW - 1)))
3797261Sjake#define	RQB_WORD(pri)	((pri) >> RQB_L2BPW)
3897261Sjake
39123418Speter#define	RQB_FFS(word)	(__ffsl(word) - 1)
4097261Sjake
4197261Sjake/*
4297261Sjake * Type of run queue status word.
4397261Sjake */
44208283Smarceltypedef	uint64_t	rqb_word_t;
4597261Sjake
46208283Smarcelstatic __inline uint64_t
47208283Smarcel__popcnt(uint64_t bits)
4897261Sjake{
49208283Smarcel        uint64_t result;
5097261Sjake
51123418Speter	__asm __volatile("popcnt %0=%1" : "=r" (result) : "r" (bits));
52123418Speter	return result;
53123418Speter}
54123418Speter
55123418Speter
56123418Speterstatic __inline int
57123418Speter__ffsl(u_long mask)
58123418Speter{
59123418Speter
60123418Speter	if (__predict_false(mask == 0ul))
6197261Sjake		return (0);
62123420Speter	return (__popcnt(mask ^ (mask - 1)));
6397261Sjake}
6497261Sjake
6597261Sjake#endif
66