2 * Copyright (C) 1996 Paul Mackerras.
3 * Adapted for ppc64 - Todd Inglett, Anton Blanchard
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <asm/bitops.h>
14 #include <linux/ext2_fs.h>
15 #include <linux/locks.h>
16 #include <linux/quotaops.h>
17 #include <asm/ppcdebug.h>
22 * Bitops are weird when viewed on big-endian systems. They were designed
23 * on little endian so the size of the bitset doesn't matter (low order bytes
24 * come first) as long as the bit in question is valid.
26 * Bits are "tested" often using the C expression (val & (1<<nr)) so we do
27 * our best to stay compatible with that. The assumption is that val will
28 * be unsigned long for such tests. As such, we assume the bits are stored
29 * as an array of unsigned long (the usual case is a single unsigned long,
30 * of course). Here's an example bitset with bit numbering:
32 * |63..........0|127........64|195.......128|255.......196|
34 * This leads to two problems. First, if an int, short or char is passed as
35 * a bitset it will be a bad memory reference since we want to store in chunks
36 * of unsigned long (64 bits here) size. Second, since these could be char
37 * arrays we might have an alignment problem. We ease the alignment problem
38 * by actually doing the operation with 32 bit values yet preserving the
39 * 64 bit long layout as shown above. Got that? Good.
42 unsigned long find_next_zero_bit(unsigned long* addr, unsigned long size,
45 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
46 unsigned long result = offset & ~63UL;
56 tmp |= ~0UL >> (64-offset);
64 while (size & ~63UL) {
79 return result + ffz(tmp);
82 unsigned long find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
84 unsigned long *p = addr + (offset >> 6);
85 unsigned long result = offset & ~63UL;
94 tmp &= (~0UL << offset);
102 while (size & ~63UL) {
113 tmp &= (~0UL >> (64 - size));
114 if (tmp == 0UL) /* Are any bits set? */
115 return result + size; /* Nope. */
117 return result + __ffs(tmp);
120 static __inline__ unsigned int ext2_ilog2(unsigned int x)
124 asm("cntlzw %0,%1" : "=r" (lz) : "r" (x));
128 static __inline__ unsigned int ext2_ffz(unsigned int x)
133 tempRC = ext2_ilog2(x & -x);
137 unsigned long find_next_zero_le_bit(unsigned long *addr, unsigned long size, unsigned long
140 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
141 unsigned int result = offset & ~31;
149 tmp = cpu_to_le32p(p++);
150 tmp |= ~0U >> (32-offset); /* bug or feature ? */
159 if ((tmp = cpu_to_le32p(p++)) != ~0)
166 tmp = cpu_to_le32p(p);
169 if (tmp == ~0) /* Are any bits zero? */
170 return result + size; /* Nope. */
172 return result + ext2_ffz(tmp);
175 void BUG_OUTLINE(char* file, unsigned line)
177 udbg_printf("BUG - kernel BUG at %s:%d! \n", __FILE__, __LINE__);
178 PPCDBG_ENTER_DEBUGGER();
179 printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__);
180 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);