162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef _ASM_IA64_BITOPS_H 362306a36Sopenharmony_ci#define _ASM_IA64_BITOPS_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci/* 662306a36Sopenharmony_ci * Copyright (C) 1998-2003 Hewlett-Packard Co 762306a36Sopenharmony_ci * David Mosberger-Tang <davidm@hpl.hp.com> 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64 1062306a36Sopenharmony_ci * O(1) scheduler patch 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#ifndef _LINUX_BITOPS_H 1462306a36Sopenharmony_ci#error only <linux/bitops.h> can be included directly 1562306a36Sopenharmony_ci#endif 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <linux/compiler.h> 1862306a36Sopenharmony_ci#include <linux/types.h> 1962306a36Sopenharmony_ci#include <asm/intrinsics.h> 2062306a36Sopenharmony_ci#include <asm/barrier.h> 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/** 2362306a36Sopenharmony_ci * set_bit - Atomically set a bit in memory 2462306a36Sopenharmony_ci * @nr: the bit to set 2562306a36Sopenharmony_ci * @addr: the address to start counting from 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * This function is atomic and may not be reordered. See __set_bit() 2862306a36Sopenharmony_ci * if you do not require the atomic guarantees. 2962306a36Sopenharmony_ci * Note that @nr may be almost arbitrarily large; this function is not 3062306a36Sopenharmony_ci * restricted to acting on a single-word quantity. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * The address must be (at least) "long" aligned. 3362306a36Sopenharmony_ci * Note that there are driver (e.g., eepro100) which use these operations to 3462306a36Sopenharmony_ci * operate on hw-defined data-structures, so we can't easily change these 3562306a36Sopenharmony_ci * operations to force a bigger alignment. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cistatic __inline__ void 4062306a36Sopenharmony_ciset_bit (int nr, volatile void *addr) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci __u32 bit, old, new; 4362306a36Sopenharmony_ci volatile __u32 *m; 4462306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 4762306a36Sopenharmony_ci bit = 1 << (nr & 31); 4862306a36Sopenharmony_ci do { 4962306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 5062306a36Sopenharmony_ci old = *m; 5162306a36Sopenharmony_ci new = old | bit; 5262306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 5362306a36Sopenharmony_ci} 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/** 5662306a36Sopenharmony_ci * arch___set_bit - Set a bit in memory 5762306a36Sopenharmony_ci * @nr: the bit to set 5862306a36Sopenharmony_ci * @addr: the address to start counting from 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * Unlike set_bit(), this function is non-atomic and may be reordered. 6162306a36Sopenharmony_ci * If it's called on the same region of memory simultaneously, the effect 6262306a36Sopenharmony_ci * may be that only one operation succeeds. 6362306a36Sopenharmony_ci */ 6462306a36Sopenharmony_cistatic __always_inline void 6562306a36Sopenharmony_ciarch___set_bit(unsigned long nr, volatile unsigned long *addr) 6662306a36Sopenharmony_ci{ 6762306a36Sopenharmony_ci *((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31)); 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/** 7162306a36Sopenharmony_ci * clear_bit - Clears a bit in memory 7262306a36Sopenharmony_ci * @nr: Bit to clear 7362306a36Sopenharmony_ci * @addr: Address to start counting from 7462306a36Sopenharmony_ci * 7562306a36Sopenharmony_ci * clear_bit() is atomic and may not be reordered. However, it does 7662306a36Sopenharmony_ci * not contain a memory barrier, so if it is used for locking purposes, 7762306a36Sopenharmony_ci * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic() 7862306a36Sopenharmony_ci * in order to ensure changes are visible on other processors. 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_cistatic __inline__ void 8162306a36Sopenharmony_ciclear_bit (int nr, volatile void *addr) 8262306a36Sopenharmony_ci{ 8362306a36Sopenharmony_ci __u32 mask, old, new; 8462306a36Sopenharmony_ci volatile __u32 *m; 8562306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 8862306a36Sopenharmony_ci mask = ~(1 << (nr & 31)); 8962306a36Sopenharmony_ci do { 9062306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 9162306a36Sopenharmony_ci old = *m; 9262306a36Sopenharmony_ci new = old & mask; 9362306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 9462306a36Sopenharmony_ci} 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci/** 9762306a36Sopenharmony_ci * clear_bit_unlock - Clears a bit in memory with release 9862306a36Sopenharmony_ci * @nr: Bit to clear 9962306a36Sopenharmony_ci * @addr: Address to start counting from 10062306a36Sopenharmony_ci * 10162306a36Sopenharmony_ci * clear_bit_unlock() is atomic and may not be reordered. It does 10262306a36Sopenharmony_ci * contain a memory barrier suitable for unlock type operations. 10362306a36Sopenharmony_ci */ 10462306a36Sopenharmony_cistatic __inline__ void 10562306a36Sopenharmony_ciclear_bit_unlock (int nr, volatile void *addr) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci __u32 mask, old, new; 10862306a36Sopenharmony_ci volatile __u32 *m; 10962306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 11262306a36Sopenharmony_ci mask = ~(1 << (nr & 31)); 11362306a36Sopenharmony_ci do { 11462306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 11562306a36Sopenharmony_ci old = *m; 11662306a36Sopenharmony_ci new = old & mask; 11762306a36Sopenharmony_ci } while (cmpxchg_rel(m, old, new) != old); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci/** 12162306a36Sopenharmony_ci * __clear_bit_unlock - Non-atomically clears a bit in memory with release 12262306a36Sopenharmony_ci * @nr: Bit to clear 12362306a36Sopenharmony_ci * @addr: Address to start counting from 12462306a36Sopenharmony_ci * 12562306a36Sopenharmony_ci * Similarly to clear_bit_unlock, the implementation uses a store 12662306a36Sopenharmony_ci * with release semantics. See also arch_spin_unlock(). 12762306a36Sopenharmony_ci */ 12862306a36Sopenharmony_cistatic __inline__ void 12962306a36Sopenharmony_ci__clear_bit_unlock(int nr, void *addr) 13062306a36Sopenharmony_ci{ 13162306a36Sopenharmony_ci __u32 * const m = (__u32 *) addr + (nr >> 5); 13262306a36Sopenharmony_ci __u32 const new = *m & ~(1 << (nr & 31)); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci ia64_st4_rel_nta(m, new); 13562306a36Sopenharmony_ci} 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci/** 13862306a36Sopenharmony_ci * arch___clear_bit - Clears a bit in memory (non-atomic version) 13962306a36Sopenharmony_ci * @nr: the bit to clear 14062306a36Sopenharmony_ci * @addr: the address to start counting from 14162306a36Sopenharmony_ci * 14262306a36Sopenharmony_ci * Unlike clear_bit(), this function is non-atomic and may be reordered. 14362306a36Sopenharmony_ci * If it's called on the same region of memory simultaneously, the effect 14462306a36Sopenharmony_ci * may be that only one operation succeeds. 14562306a36Sopenharmony_ci */ 14662306a36Sopenharmony_cistatic __always_inline void 14762306a36Sopenharmony_ciarch___clear_bit(unsigned long nr, volatile unsigned long *addr) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci *((__u32 *) addr + (nr >> 5)) &= ~(1 << (nr & 31)); 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci/** 15362306a36Sopenharmony_ci * change_bit - Toggle a bit in memory 15462306a36Sopenharmony_ci * @nr: Bit to toggle 15562306a36Sopenharmony_ci * @addr: Address to start counting from 15662306a36Sopenharmony_ci * 15762306a36Sopenharmony_ci * change_bit() is atomic and may not be reordered. 15862306a36Sopenharmony_ci * Note that @nr may be almost arbitrarily large; this function is not 15962306a36Sopenharmony_ci * restricted to acting on a single-word quantity. 16062306a36Sopenharmony_ci */ 16162306a36Sopenharmony_cistatic __inline__ void 16262306a36Sopenharmony_cichange_bit (int nr, volatile void *addr) 16362306a36Sopenharmony_ci{ 16462306a36Sopenharmony_ci __u32 bit, old, new; 16562306a36Sopenharmony_ci volatile __u32 *m; 16662306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 16962306a36Sopenharmony_ci bit = (1 << (nr & 31)); 17062306a36Sopenharmony_ci do { 17162306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 17262306a36Sopenharmony_ci old = *m; 17362306a36Sopenharmony_ci new = old ^ bit; 17462306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 17562306a36Sopenharmony_ci} 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci/** 17862306a36Sopenharmony_ci * arch___change_bit - Toggle a bit in memory 17962306a36Sopenharmony_ci * @nr: the bit to toggle 18062306a36Sopenharmony_ci * @addr: the address to start counting from 18162306a36Sopenharmony_ci * 18262306a36Sopenharmony_ci * Unlike change_bit(), this function is non-atomic and may be reordered. 18362306a36Sopenharmony_ci * If it's called on the same region of memory simultaneously, the effect 18462306a36Sopenharmony_ci * may be that only one operation succeeds. 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_cistatic __always_inline void 18762306a36Sopenharmony_ciarch___change_bit(unsigned long nr, volatile unsigned long *addr) 18862306a36Sopenharmony_ci{ 18962306a36Sopenharmony_ci *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31)); 19062306a36Sopenharmony_ci} 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/** 19362306a36Sopenharmony_ci * test_and_set_bit - Set a bit and return its old value 19462306a36Sopenharmony_ci * @nr: Bit to set 19562306a36Sopenharmony_ci * @addr: Address to count from 19662306a36Sopenharmony_ci * 19762306a36Sopenharmony_ci * This operation is atomic and cannot be reordered. 19862306a36Sopenharmony_ci * It also implies the acquisition side of the memory barrier. 19962306a36Sopenharmony_ci */ 20062306a36Sopenharmony_cistatic __inline__ int 20162306a36Sopenharmony_citest_and_set_bit (int nr, volatile void *addr) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci __u32 bit, old, new; 20462306a36Sopenharmony_ci volatile __u32 *m; 20562306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 20862306a36Sopenharmony_ci bit = 1 << (nr & 31); 20962306a36Sopenharmony_ci do { 21062306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 21162306a36Sopenharmony_ci old = *m; 21262306a36Sopenharmony_ci new = old | bit; 21362306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 21462306a36Sopenharmony_ci return (old & bit) != 0; 21562306a36Sopenharmony_ci} 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci/** 21862306a36Sopenharmony_ci * test_and_set_bit_lock - Set a bit and return its old value for lock 21962306a36Sopenharmony_ci * @nr: Bit to set 22062306a36Sopenharmony_ci * @addr: Address to count from 22162306a36Sopenharmony_ci * 22262306a36Sopenharmony_ci * This is the same as test_and_set_bit on ia64 22362306a36Sopenharmony_ci */ 22462306a36Sopenharmony_ci#define test_and_set_bit_lock test_and_set_bit 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci/** 22762306a36Sopenharmony_ci * arch___test_and_set_bit - Set a bit and return its old value 22862306a36Sopenharmony_ci * @nr: Bit to set 22962306a36Sopenharmony_ci * @addr: Address to count from 23062306a36Sopenharmony_ci * 23162306a36Sopenharmony_ci * This operation is non-atomic and can be reordered. 23262306a36Sopenharmony_ci * If two examples of this operation race, one can appear to succeed 23362306a36Sopenharmony_ci * but actually fail. You must protect multiple accesses with a lock. 23462306a36Sopenharmony_ci */ 23562306a36Sopenharmony_cistatic __always_inline bool 23662306a36Sopenharmony_ciarch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr) 23762306a36Sopenharmony_ci{ 23862306a36Sopenharmony_ci __u32 *p = (__u32 *) addr + (nr >> 5); 23962306a36Sopenharmony_ci __u32 m = 1 << (nr & 31); 24062306a36Sopenharmony_ci int oldbitset = (*p & m) != 0; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci *p |= m; 24362306a36Sopenharmony_ci return oldbitset; 24462306a36Sopenharmony_ci} 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci/** 24762306a36Sopenharmony_ci * test_and_clear_bit - Clear a bit and return its old value 24862306a36Sopenharmony_ci * @nr: Bit to clear 24962306a36Sopenharmony_ci * @addr: Address to count from 25062306a36Sopenharmony_ci * 25162306a36Sopenharmony_ci * This operation is atomic and cannot be reordered. 25262306a36Sopenharmony_ci * It also implies the acquisition side of the memory barrier. 25362306a36Sopenharmony_ci */ 25462306a36Sopenharmony_cistatic __inline__ int 25562306a36Sopenharmony_citest_and_clear_bit (int nr, volatile void *addr) 25662306a36Sopenharmony_ci{ 25762306a36Sopenharmony_ci __u32 mask, old, new; 25862306a36Sopenharmony_ci volatile __u32 *m; 25962306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 26262306a36Sopenharmony_ci mask = ~(1 << (nr & 31)); 26362306a36Sopenharmony_ci do { 26462306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 26562306a36Sopenharmony_ci old = *m; 26662306a36Sopenharmony_ci new = old & mask; 26762306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 26862306a36Sopenharmony_ci return (old & ~mask) != 0; 26962306a36Sopenharmony_ci} 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci/** 27262306a36Sopenharmony_ci * arch___test_and_clear_bit - Clear a bit and return its old value 27362306a36Sopenharmony_ci * @nr: Bit to clear 27462306a36Sopenharmony_ci * @addr: Address to count from 27562306a36Sopenharmony_ci * 27662306a36Sopenharmony_ci * This operation is non-atomic and can be reordered. 27762306a36Sopenharmony_ci * If two examples of this operation race, one can appear to succeed 27862306a36Sopenharmony_ci * but actually fail. You must protect multiple accesses with a lock. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_cistatic __always_inline bool 28162306a36Sopenharmony_ciarch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci __u32 *p = (__u32 *) addr + (nr >> 5); 28462306a36Sopenharmony_ci __u32 m = 1 << (nr & 31); 28562306a36Sopenharmony_ci int oldbitset = (*p & m) != 0; 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ci *p &= ~m; 28862306a36Sopenharmony_ci return oldbitset; 28962306a36Sopenharmony_ci} 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci/** 29262306a36Sopenharmony_ci * test_and_change_bit - Change a bit and return its old value 29362306a36Sopenharmony_ci * @nr: Bit to change 29462306a36Sopenharmony_ci * @addr: Address to count from 29562306a36Sopenharmony_ci * 29662306a36Sopenharmony_ci * This operation is atomic and cannot be reordered. 29762306a36Sopenharmony_ci * It also implies the acquisition side of the memory barrier. 29862306a36Sopenharmony_ci */ 29962306a36Sopenharmony_cistatic __inline__ int 30062306a36Sopenharmony_citest_and_change_bit (int nr, volatile void *addr) 30162306a36Sopenharmony_ci{ 30262306a36Sopenharmony_ci __u32 bit, old, new; 30362306a36Sopenharmony_ci volatile __u32 *m; 30462306a36Sopenharmony_ci CMPXCHG_BUGCHECK_DECL 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci m = (volatile __u32 *) addr + (nr >> 5); 30762306a36Sopenharmony_ci bit = (1 << (nr & 31)); 30862306a36Sopenharmony_ci do { 30962306a36Sopenharmony_ci CMPXCHG_BUGCHECK(m); 31062306a36Sopenharmony_ci old = *m; 31162306a36Sopenharmony_ci new = old ^ bit; 31262306a36Sopenharmony_ci } while (cmpxchg_acq(m, old, new) != old); 31362306a36Sopenharmony_ci return (old & bit) != 0; 31462306a36Sopenharmony_ci} 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci/** 31762306a36Sopenharmony_ci * arch___test_and_change_bit - Change a bit and return its old value 31862306a36Sopenharmony_ci * @nr: Bit to change 31962306a36Sopenharmony_ci * @addr: Address to count from 32062306a36Sopenharmony_ci * 32162306a36Sopenharmony_ci * This operation is non-atomic and can be reordered. 32262306a36Sopenharmony_ci */ 32362306a36Sopenharmony_cistatic __always_inline bool 32462306a36Sopenharmony_ciarch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr) 32562306a36Sopenharmony_ci{ 32662306a36Sopenharmony_ci __u32 old, bit = (1 << (nr & 31)); 32762306a36Sopenharmony_ci __u32 *m = (__u32 *) addr + (nr >> 5); 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci old = *m; 33062306a36Sopenharmony_ci *m = old ^ bit; 33162306a36Sopenharmony_ci return (old & bit) != 0; 33262306a36Sopenharmony_ci} 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci#define arch_test_bit generic_test_bit 33562306a36Sopenharmony_ci#define arch_test_bit_acquire generic_test_bit_acquire 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci/** 33862306a36Sopenharmony_ci * ffz - find the first zero bit in a long word 33962306a36Sopenharmony_ci * @x: The long word to find the bit in 34062306a36Sopenharmony_ci * 34162306a36Sopenharmony_ci * Returns the bit-number (0..63) of the first (least significant) zero bit. 34262306a36Sopenharmony_ci * Undefined if no zero exists, so code should check against ~0UL first... 34362306a36Sopenharmony_ci */ 34462306a36Sopenharmony_cistatic inline unsigned long 34562306a36Sopenharmony_ciffz (unsigned long x) 34662306a36Sopenharmony_ci{ 34762306a36Sopenharmony_ci unsigned long result; 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci result = ia64_popcnt(x & (~x - 1)); 35062306a36Sopenharmony_ci return result; 35162306a36Sopenharmony_ci} 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci/** 35462306a36Sopenharmony_ci * __ffs - find first bit in word. 35562306a36Sopenharmony_ci * @x: The word to search 35662306a36Sopenharmony_ci * 35762306a36Sopenharmony_ci * Undefined if no bit exists, so code should check against 0 first. 35862306a36Sopenharmony_ci */ 35962306a36Sopenharmony_cistatic __inline__ unsigned long 36062306a36Sopenharmony_ci__ffs (unsigned long x) 36162306a36Sopenharmony_ci{ 36262306a36Sopenharmony_ci unsigned long result; 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci result = ia64_popcnt((x-1) & ~x); 36562306a36Sopenharmony_ci return result; 36662306a36Sopenharmony_ci} 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci#ifdef __KERNEL__ 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci/* 37162306a36Sopenharmony_ci * Return bit number of last (most-significant) bit set. Undefined 37262306a36Sopenharmony_ci * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3). 37362306a36Sopenharmony_ci */ 37462306a36Sopenharmony_cistatic inline unsigned long 37562306a36Sopenharmony_ciia64_fls (unsigned long x) 37662306a36Sopenharmony_ci{ 37762306a36Sopenharmony_ci long double d = x; 37862306a36Sopenharmony_ci long exp; 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci exp = ia64_getf_exp(d); 38162306a36Sopenharmony_ci return exp - 0xffff; 38262306a36Sopenharmony_ci} 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci/* 38562306a36Sopenharmony_ci * Find the last (most significant) bit set. Returns 0 for x==0 and 38662306a36Sopenharmony_ci * bits are numbered from 1..32 (e.g., fls(9) == 4). 38762306a36Sopenharmony_ci */ 38862306a36Sopenharmony_cistatic inline int fls(unsigned int t) 38962306a36Sopenharmony_ci{ 39062306a36Sopenharmony_ci unsigned long x = t & 0xffffffffu; 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci if (!x) 39362306a36Sopenharmony_ci return 0; 39462306a36Sopenharmony_ci x |= x >> 1; 39562306a36Sopenharmony_ci x |= x >> 2; 39662306a36Sopenharmony_ci x |= x >> 4; 39762306a36Sopenharmony_ci x |= x >> 8; 39862306a36Sopenharmony_ci x |= x >> 16; 39962306a36Sopenharmony_ci return ia64_popcnt(x); 40062306a36Sopenharmony_ci} 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci/* 40362306a36Sopenharmony_ci * Find the last (most significant) bit set. Undefined for x==0. 40462306a36Sopenharmony_ci * Bits are numbered from 0..63 (e.g., __fls(9) == 3). 40562306a36Sopenharmony_ci */ 40662306a36Sopenharmony_cistatic inline unsigned long 40762306a36Sopenharmony_ci__fls (unsigned long x) 40862306a36Sopenharmony_ci{ 40962306a36Sopenharmony_ci x |= x >> 1; 41062306a36Sopenharmony_ci x |= x >> 2; 41162306a36Sopenharmony_ci x |= x >> 4; 41262306a36Sopenharmony_ci x |= x >> 8; 41362306a36Sopenharmony_ci x |= x >> 16; 41462306a36Sopenharmony_ci x |= x >> 32; 41562306a36Sopenharmony_ci return ia64_popcnt(x) - 1; 41662306a36Sopenharmony_ci} 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ci#include <asm-generic/bitops/fls64.h> 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci#include <asm-generic/bitops/builtin-ffs.h> 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ci/* 42362306a36Sopenharmony_ci * hweightN: returns the hamming weight (i.e. the number 42462306a36Sopenharmony_ci * of bits set) of a N-bit word 42562306a36Sopenharmony_ci */ 42662306a36Sopenharmony_cistatic __inline__ unsigned long __arch_hweight64(unsigned long x) 42762306a36Sopenharmony_ci{ 42862306a36Sopenharmony_ci unsigned long result; 42962306a36Sopenharmony_ci result = ia64_popcnt(x); 43062306a36Sopenharmony_ci return result; 43162306a36Sopenharmony_ci} 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci#define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful)) 43462306a36Sopenharmony_ci#define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful)) 43562306a36Sopenharmony_ci#define __arch_hweight8(x) ((unsigned int) __arch_hweight64((x) & 0xfful)) 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci#include <asm-generic/bitops/const_hweight.h> 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci#endif /* __KERNEL__ */ 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci#ifdef __KERNEL__ 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci#include <asm-generic/bitops/non-instrumented-non-atomic.h> 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci#include <asm-generic/bitops/le.h> 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci#include <asm-generic/bitops/ext2-atomic-setbit.h> 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci#include <asm-generic/bitops/sched.h> 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci#endif /* __KERNEL__ */ 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ci#endif /* _ASM_IA64_BITOPS_H */ 454