#ifndef _ASM_IA64_BITOPS_H1#define _ASM_IA64_BITOPS_H23/*4* Copyright (C) 1998-2003 Hewlett-Packard Co5* David Mosberger-Tang <[email protected]>6*7* 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia648* O(1) scheduler patch9*/1011#ifndef _LINUX_BITOPS_H12#error only <linux/bitops.h> can be included directly13#endif1415#include <linux/compiler.h>16#include <linux/types.h>17#include <asm/intrinsics.h>1819/**20* set_bit - Atomically set a bit in memory21* @nr: the bit to set22* @addr: the address to start counting from23*24* This function is atomic and may not be reordered. See __set_bit()25* if you do not require the atomic guarantees.26* Note that @nr may be almost arbitrarily large; this function is not27* restricted to acting on a single-word quantity.28*29* The address must be (at least) "long" aligned.30* Note that there are driver (e.g., eepro100) which use these operations to31* operate on hw-defined data-structures, so we can't easily change these32* operations to force a bigger alignment.33*34* bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).35*/36static __inline__ void37set_bit (int nr, volatile void *addr)38{39__u32 bit, old, new;40volatile __u32 *m;41CMPXCHG_BUGCHECK_DECL4243m = (volatile __u32 *) addr + (nr >> 5);44bit = 1 << (nr & 31);45do {46CMPXCHG_BUGCHECK(m);47old = *m;48new = old | bit;49} while (cmpxchg_acq(m, old, new) != old);50}5152/**53* __set_bit - Set a bit in memory54* @nr: the bit to set55* @addr: the address to start counting from56*57* Unlike set_bit(), this function is non-atomic and may be reordered.58* If it's called on the same region of memory simultaneously, the effect59* may be that only one operation succeeds.60*/61static __inline__ void62__set_bit (int nr, volatile void *addr)63{64*((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31));65}6667/*68* clear_bit() has "acquire" semantics.69*/70#define smp_mb__before_clear_bit() smp_mb()71#define smp_mb__after_clear_bit() do { /* skip */; } while (0)7273/**74* clear_bit - Clears a bit in memory75* @nr: Bit to clear76* @addr: Address to start counting from77*78* clear_bit() is atomic and may not be reordered. However, it does79* not contain a memory barrier, so if it is used for locking purposes,80* you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()81* in order to ensure changes are visible on other processors.82*/83static __inline__ void84clear_bit (int nr, volatile void *addr)85{86__u32 mask, old, new;87volatile __u32 *m;88CMPXCHG_BUGCHECK_DECL8990m = (volatile __u32 *) addr + (nr >> 5);91mask = ~(1 << (nr & 31));92do {93CMPXCHG_BUGCHECK(m);94old = *m;95new = old & mask;96} while (cmpxchg_acq(m, old, new) != old);97}9899/**100* clear_bit_unlock - Clears a bit in memory with release101* @nr: Bit to clear102* @addr: Address to start counting from103*104* clear_bit_unlock() is atomic and may not be reordered. It does105* contain a memory barrier suitable for unlock type operations.106*/107static __inline__ void108clear_bit_unlock (int nr, volatile void *addr)109{110__u32 mask, old, new;111volatile __u32 *m;112CMPXCHG_BUGCHECK_DECL113114m = (volatile __u32 *) addr + (nr >> 5);115mask = ~(1 << (nr & 31));116do {117CMPXCHG_BUGCHECK(m);118old = *m;119new = old & mask;120} while (cmpxchg_rel(m, old, new) != old);121}122123/**124* __clear_bit_unlock - Non-atomically clears a bit in memory with release125* @nr: Bit to clear126* @addr: Address to start counting from127*128* Similarly to clear_bit_unlock, the implementation uses a store129* with release semantics. See also arch_spin_unlock().130*/131static __inline__ void132__clear_bit_unlock(int nr, void *addr)133{134__u32 * const m = (__u32 *) addr + (nr >> 5);135__u32 const new = *m & ~(1 << (nr & 31));136137ia64_st4_rel_nta(m, new);138}139140/**141* __clear_bit - Clears a bit in memory (non-atomic version)142* @nr: the bit to clear143* @addr: the address to start counting from144*145* Unlike clear_bit(), this function is non-atomic and may be reordered.146* If it's called on the same region of memory simultaneously, the effect147* may be that only one operation succeeds.148*/149static __inline__ void150__clear_bit (int nr, volatile void *addr)151{152*((__u32 *) addr + (nr >> 5)) &= ~(1 << (nr & 31));153}154155/**156* change_bit - Toggle a bit in memory157* @nr: Bit to toggle158* @addr: Address to start counting from159*160* change_bit() is atomic and may not be reordered.161* Note that @nr may be almost arbitrarily large; this function is not162* restricted to acting on a single-word quantity.163*/164static __inline__ void165change_bit (int nr, volatile void *addr)166{167__u32 bit, old, new;168volatile __u32 *m;169CMPXCHG_BUGCHECK_DECL170171m = (volatile __u32 *) addr + (nr >> 5);172bit = (1 << (nr & 31));173do {174CMPXCHG_BUGCHECK(m);175old = *m;176new = old ^ bit;177} while (cmpxchg_acq(m, old, new) != old);178}179180/**181* __change_bit - Toggle a bit in memory182* @nr: the bit to toggle183* @addr: the address to start counting from184*185* Unlike change_bit(), this function is non-atomic and may be reordered.186* If it's called on the same region of memory simultaneously, the effect187* may be that only one operation succeeds.188*/189static __inline__ void190__change_bit (int nr, volatile void *addr)191{192*((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));193}194195/**196* test_and_set_bit - Set a bit and return its old value197* @nr: Bit to set198* @addr: Address to count from199*200* This operation is atomic and cannot be reordered.201* It also implies the acquisition side of the memory barrier.202*/203static __inline__ int204test_and_set_bit (int nr, volatile void *addr)205{206__u32 bit, old, new;207volatile __u32 *m;208CMPXCHG_BUGCHECK_DECL209210m = (volatile __u32 *) addr + (nr >> 5);211bit = 1 << (nr & 31);212do {213CMPXCHG_BUGCHECK(m);214old = *m;215new = old | bit;216} while (cmpxchg_acq(m, old, new) != old);217return (old & bit) != 0;218}219220/**221* test_and_set_bit_lock - Set a bit and return its old value for lock222* @nr: Bit to set223* @addr: Address to count from224*225* This is the same as test_and_set_bit on ia64226*/227#define test_and_set_bit_lock test_and_set_bit228229/**230* __test_and_set_bit - Set a bit and return its old value231* @nr: Bit to set232* @addr: Address to count from233*234* This operation is non-atomic and can be reordered.235* If two examples of this operation race, one can appear to succeed236* but actually fail. You must protect multiple accesses with a lock.237*/238static __inline__ int239__test_and_set_bit (int nr, volatile void *addr)240{241__u32 *p = (__u32 *) addr + (nr >> 5);242__u32 m = 1 << (nr & 31);243int oldbitset = (*p & m) != 0;244245*p |= m;246return oldbitset;247}248249/**250* test_and_clear_bit - Clear a bit and return its old value251* @nr: Bit to clear252* @addr: Address to count from253*254* This operation is atomic and cannot be reordered.255* It also implies the acquisition side of the memory barrier.256*/257static __inline__ int258test_and_clear_bit (int nr, volatile void *addr)259{260__u32 mask, old, new;261volatile __u32 *m;262CMPXCHG_BUGCHECK_DECL263264m = (volatile __u32 *) addr + (nr >> 5);265mask = ~(1 << (nr & 31));266do {267CMPXCHG_BUGCHECK(m);268old = *m;269new = old & mask;270} while (cmpxchg_acq(m, old, new) != old);271return (old & ~mask) != 0;272}273274/**275* __test_and_clear_bit - Clear a bit and return its old value276* @nr: Bit to clear277* @addr: Address to count from278*279* This operation is non-atomic and can be reordered.280* If two examples of this operation race, one can appear to succeed281* but actually fail. You must protect multiple accesses with a lock.282*/283static __inline__ int284__test_and_clear_bit(int nr, volatile void * addr)285{286__u32 *p = (__u32 *) addr + (nr >> 5);287__u32 m = 1 << (nr & 31);288int oldbitset = (*p & m) != 0;289290*p &= ~m;291return oldbitset;292}293294/**295* test_and_change_bit - Change a bit and return its old value296* @nr: Bit to change297* @addr: Address to count from298*299* This operation is atomic and cannot be reordered.300* It also implies the acquisition side of the memory barrier.301*/302static __inline__ int303test_and_change_bit (int nr, volatile void *addr)304{305__u32 bit, old, new;306volatile __u32 *m;307CMPXCHG_BUGCHECK_DECL308309m = (volatile __u32 *) addr + (nr >> 5);310bit = (1 << (nr & 31));311do {312CMPXCHG_BUGCHECK(m);313old = *m;314new = old ^ bit;315} while (cmpxchg_acq(m, old, new) != old);316return (old & bit) != 0;317}318319/**320* __test_and_change_bit - Change a bit and return its old value321* @nr: Bit to change322* @addr: Address to count from323*324* This operation is non-atomic and can be reordered.325*/326static __inline__ int327__test_and_change_bit (int nr, void *addr)328{329__u32 old, bit = (1 << (nr & 31));330__u32 *m = (__u32 *) addr + (nr >> 5);331332old = *m;333*m = old ^ bit;334return (old & bit) != 0;335}336337static __inline__ int338test_bit (int nr, const volatile void *addr)339{340return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31));341}342343/**344* ffz - find the first zero bit in a long word345* @x: The long word to find the bit in346*347* Returns the bit-number (0..63) of the first (least significant) zero bit.348* Undefined if no zero exists, so code should check against ~0UL first...349*/350static inline unsigned long351ffz (unsigned long x)352{353unsigned long result;354355result = ia64_popcnt(x & (~x - 1));356return result;357}358359/**360* __ffs - find first bit in word.361* @x: The word to search362*363* Undefined if no bit exists, so code should check against 0 first.364*/365static __inline__ unsigned long366__ffs (unsigned long x)367{368unsigned long result;369370result = ia64_popcnt((x-1) & ~x);371return result;372}373374#ifdef __KERNEL__375376/*377* Return bit number of last (most-significant) bit set. Undefined378* for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).379*/380static inline unsigned long381ia64_fls (unsigned long x)382{383long double d = x;384long exp;385386exp = ia64_getf_exp(d);387return exp - 0xffff;388}389390/*391* Find the last (most significant) bit set. Returns 0 for x==0 and392* bits are numbered from 1..32 (e.g., fls(9) == 4).393*/394static inline int395fls (int t)396{397unsigned long x = t & 0xffffffffu;398399if (!x)400return 0;401x |= x >> 1;402x |= x >> 2;403x |= x >> 4;404x |= x >> 8;405x |= x >> 16;406return ia64_popcnt(x);407}408409/*410* Find the last (most significant) bit set. Undefined for x==0.411* Bits are numbered from 0..63 (e.g., __fls(9) == 3).412*/413static inline unsigned long414__fls (unsigned long x)415{416x |= x >> 1;417x |= x >> 2;418x |= x >> 4;419x |= x >> 8;420x |= x >> 16;421x |= x >> 32;422return ia64_popcnt(x) - 1;423}424425#include <asm-generic/bitops/fls64.h>426427/*428* ffs: find first bit set. This is defined the same way as the libc and429* compiler builtin ffs routines, therefore differs in spirit from the above430* ffz (man ffs): it operates on "int" values only and the result value is the431* bit number + 1. ffs(0) is defined to return zero.432*/433#define ffs(x) __builtin_ffs(x)434435/*436* hweightN: returns the hamming weight (i.e. the number437* of bits set) of a N-bit word438*/439static __inline__ unsigned long __arch_hweight64(unsigned long x)440{441unsigned long result;442result = ia64_popcnt(x);443return result;444}445446#define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))447#define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))448#define __arch_hweight8(x) ((unsigned int) __arch_hweight64((x) & 0xfful))449450#include <asm-generic/bitops/const_hweight.h>451452#endif /* __KERNEL__ */453454#include <asm-generic/bitops/find.h>455456#ifdef __KERNEL__457458#include <asm-generic/bitops/le.h>459460#define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)461#define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)462463#include <asm-generic/bitops/sched.h>464465#endif /* __KERNEL__ */466467#endif /* _ASM_IA64_BITOPS_H */468469470