Skip to content

Commit

Permalink
[PATCH] LOG2: Implement a general integer log2 facility in the kernel
Browse files Browse the repository at this point in the history
This facility provides three entry points:

	ilog2()		Log base 2 of unsigned long
	ilog2_u32()	Log base 2 of u32
	ilog2_u64()	Log base 2 of u64

These facilities can either be used inside functions on dynamic data:

	int do_something(long q)
	{
		...;
		y = ilog2(x)
		...;
	}

Or can be used to statically initialise global variables with constant values:

	unsigned n = ilog2(27);

When performing static initialisation, the compiler will report "error:
initializer element is not constant" if asked to take a log of zero or of
something not reducible to a constant.  They treat negative numbers as
unsigned.

When not dealing with a constant, they fall back to using fls() which permits
them to use arch-specific log calculation instructions - such as BSR on
x86/x86_64 or SCAN on FRV - if available.

[[email protected]: MMC fix]
Signed-off-by: David Howells <[email protected]>
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: David Howells <[email protected]>
Cc: Wojtek Kaniewski <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
dhowells authored and Linus Torvalds committed Dec 8, 2006
1 parent b3d7ae5 commit f0d1b0b
Show file tree
Hide file tree
Showing 36 changed files with 384 additions and 28 deletions.
8 changes: 8 additions & 0 deletions arch/alpha/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/arm26/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/avr32/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ config GENERIC_TIME
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_BUST_SPINLOCK
bool

Expand Down
8 changes: 8 additions & 0 deletions arch/cris/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/frv/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ config TIME_LOW_RES
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default y

config ARCH_HAS_ILOG2_U64
bool
default y

mainmenu "Fujitsu FR-V Kernel Configuration"

source "init/Kconfig"
Expand Down
8 changes: 8 additions & 0 deletions arch/h8300/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default n

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/i386/Kconfig.cpu
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ config RWSEM_XCHGADD_ALGORITHM
depends on !M386
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_CALIBRATE_DELAY
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/ia64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/m32r/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default n

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/m68k/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/m68knommu/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default n

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/mips/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/parisc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ config RWSEM_GENERIC_SPINLOCK
config RWSEM_XCHGADD_ALGORITHM
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_FIND_NEXT_BIT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/powerpc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/ppc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default y

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/s390/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ config RWSEM_XCHGADD_ALGORITHM
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config GENERIC_HWEIGHT
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/sh/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ config LOCKDEP_SUPPORT
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

source "init/Kconfig"

menu "System type"
Expand Down
8 changes: 8 additions & 0 deletions arch/sh64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ config RWSEM_XCHGADD_ALGORITHM
config GENERIC_ISA_DMA
bool

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

source init/Kconfig

menu "System type"
Expand Down
8 changes: 8 additions & 0 deletions arch/sparc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ config ARCH_MAY_HAVE_PC_FDC
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config SUN_PM
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/sparc64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ config ARCH_MAY_HAVE_PC_FDC
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

config AUDIT_ARCH
bool
default y
Expand Down
8 changes: 8 additions & 0 deletions arch/v850/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ config TIME_LOW_RES
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

# Turn off some random 386 crap that can affect device config
config ISA
bool
Expand Down
8 changes: 8 additions & 0 deletions arch/x86_64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ config GENERIC_BUG
default y
depends on BUG

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

source "init/Kconfig"


Expand Down
8 changes: 8 additions & 0 deletions arch/xtensa/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ config GENERIC_HARDIRQS
bool
default y

config ARCH_HAS_ILOG2_U32
bool
default n

config ARCH_HAS_ILOG2_U64
bool
default n

source "init/Kconfig"

menu "Processor type and features"
Expand Down
4 changes: 2 additions & 2 deletions drivers/infiniband/hw/mthca/mthca_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int mthca_query_device(struct ib_device *ibdev,
props->max_map_per_fmr = 255;
else
props->max_map_per_fmr =
(1 << (32 - long_log2(mdev->limits.num_mpts))) - 1;
(1 << (32 - ilog2(mdev->limits.num_mpts))) - 1;

err = 0;
out:
Expand Down Expand Up @@ -816,7 +816,7 @@ static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *uda
lkey = ucmd.lkey;
}

ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, long_log2(entries), &status);
ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries), &status);
if (status)
ret = -EINVAL;

Expand Down
4 changes: 2 additions & 2 deletions drivers/infiniband/hw/mthca/mthca_qp.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,11 @@ int mthca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,

if (mthca_is_memfree(dev)) {
if (qp->rq.max)
qp_context->rq_size_stride = long_log2(qp->rq.max) << 3;
qp_context->rq_size_stride = ilog2(qp->rq.max) << 3;
qp_context->rq_size_stride |= qp->rq.wqe_shift - 4;

if (qp->sq.max)
qp_context->sq_size_stride = long_log2(qp->sq.max) << 3;
qp_context->sq_size_stride = ilog2(qp->sq.max) << 3;
qp_context->sq_size_stride |= qp->sq.wqe_shift - 4;
}

Expand Down
Loading

0 comments on commit f0d1b0b

Please sign in to comment.