#ifndef _ASM_BITOPS_H
#define _ASM_BITOPS_H
/*
 * Copyright 1992, Linus Torvalds.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file README.legal in the main directory of this archive
 * for more details.
 */

#ifdef __i386__
/*
 * These have to be done with inline assembly: that way the bit-setting
 * is guaranteed to be atomic. All bitoperations return 0 if the bit
 * was cleared before the operation and != 0 if it was not.
 *
 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
 */

/*
 * Some hacks to defeat gcc over-optimizations..
 */
struct __dummy { unsigned long a[100]; };
#define ADDR (*(struct __dummy *) addr)

extern __inline__ int set_bit(int nr, void * addr)
{
	int oldbit;

	__asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
		:"=r" (oldbit),"=m" (ADDR)
		:"r" (nr));
	return oldbit;
}

extern __inline__ int clear_bit(int nr, void * addr)
{
	int oldbit;

	__asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
		:"=r" (oldbit),"=m" (ADDR)
		:"r" (nr));
	return oldbit;
}

/*
 * This routine doesn't need to be atomic, but it's faster to code it
 * this way.
 */
extern __inline__ int test_bit(int nr, void * addr)
{
	int oldbit;

	__asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
		:"=r" (oldbit)
		:"m" (ADDR),"r" (nr));
	return oldbit;
}

#elif defined(__mc68000__)
/*
 * mc680x0 equivalents.  Require 68020 or better.
 *
 * They don't use the standard m680x0 bit ordering.
 * Instead, the use the standard m680x0 bitfield ordering.
 *
 * Thus, bit 0 is the MSB of addr; bit 32 is the MSB of (addr+1).
 */

extern __inline__ int set_bit(int nr,void * vaddr)
{
	char retval;

	__asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
	     : "=d" (retval) : "d" (nr), "a" (vaddr));

	return retval;
}

extern __inline__ int clear_bit(int nr, void * vaddr)
{
	char retval;

	__asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
	     : "=d" (retval) : "d" (nr), "a" (vaddr));

	return retval;
}

extern __inline__ int test_bit(int nr, void * vaddr)
{
	char retval;

	__asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0"
	     : "=d" (retval) : "d" (nr), "a" (vaddr));

	return retval;
}
#else
/*
 * For the benefit of those who are trying to port Linux to another
 * architecture, here are some C-language equivalents.	You should
 * recode these in the native assmebly language, if at all possible.
 * To guarantee atomicity, these routines call cli() and sti() to
 * disable interrupts while they operate.  (You have to provide inline
 * routines to cli() and sti().)
 *
 * Also note, these routines assume that you have 32 bit integers.
 * You will have to change this if you are trying to port Linux to the
 * Alpha architecture or to a Cray.  :-)
 *
 * C language equivalents written by Theodore Ts'o, 9/26/92
 */

extern __inline__ int set_bit(int nr,void * vaddr)
{
	int	      mask, retval;
	unsigned long *addr = (unsigned long *)vaddr;
	unsigned long flags;
	
	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	save_flags (flags);
	cli();
	retval = (mask & *addr) != 0;
	*addr |= mask;
	restore_flags(flags);
	return retval;
}

extern __inline__ int clear_bit(int nr, void * vaddr)
{
	int	mask, retval;
	unsigned long *addr = (unsigned long *)vaddr;
	unsigned long flags;

	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	save_flags (flags);
	cli();
	retval = (mask & *addr) != 0;
	*addr &= ~mask;
	restore_flags (flags);
	return retval;
}

extern __inline__ int test_bit(int nr, void * vaddr)
{
	int	mask;
	unsigned long *addr = (unsigned long *)vaddr;

	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	return ((mask & *addr) != 0);
}
#endif	/* i386 */
#endif /* _ASM_BITOPS_H */
