Skip to contentSkip to conversation

MPILIB: Provide count_leading/trailing_zeros() based on arch functions

PATCH In review David Howells 4 participants 7 messages Jul 20, 2012 – Aug 10, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 MPILIB: Provide count_leading/trailing_zeros() based on arch functions

Provide count_leading/trailing_zeros() macros based on extant arch bit scanning functions rather than reimplementing from scratch in MPILIB.

Whilst we're at it, turn count_foo_zeros(n, x) into n = count_foo_zeros(x).

Also move the definition to asm-generic as other people may be interested in using it.

Cc David S. Miller <[email protected]>
Cc Dmitry Kasatkin <[email protected]>
Cc Arnd Bergmann <[email protected]>

---

 include/asm-generic/bitops/count_zeros.h |   57 ++++++++++++
 lib/mpi/longlong.h                       |  142 +-----------------------------
 lib/mpi/mpi-bit.c                        |    2 
 lib/mpi/mpi-pow.c                        |    4 -
 4 files changed, 64 insertions(+), 141 deletions(-)
 create mode 100644 include/asm-generic/bitops/count_zeros.h
include/asm-generic/bitops/count_zeros.h new file +57 -0
new file mode 100644
index 0000000..7f8e1d0
@@ -0,0 +1,57 @@
+/* Count leading and trailing zeros functions
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#ifndef _ASM_GENERIC_BITOPS_COUNT_ZEROS_H_
+#define _ASM_GENERIC_BITOPS_COUNT_ZEROS_H_
+
+#include <linux/bitops.h>
+
+/**
+ * count_leading_zeros - Count the number of zeros from the MSB back
+ * @x: The value
+ *
+ * Count the number of leading zeros from the MSB going towards the LSB in @x.
+ *
+ * If the MSB of @x is set, the result is 0.
+ * If only the LSB of @x is set, then the result is BITS_PER_LONG-1.
+ * If @x is 0 then the result is COUNT_LEADING_ZEROS_0.
+ */
+static inline int count_leading_zeros(unsigned long x)
+{
+	if (sizeof(x) == 4)
+		return BITS_PER_LONG - fls(x);
+	else
+		return BITS_PER_LONG - fls64(x);
+}
+
+#define COUNT_LEADING_ZEROS_0 BITS_PER_LONG
+
+/**
+ * count_trailing_zeros - Count the number of zeros from the LSB forwards
+ * @x: The value
+ *
+ * Count the number of trailing zeros from the LSB going towards the MSB in @x.
+ *
+ * If the LSB of @x is set, the result is 0.
+ * If only the MSB of @x is set, then the result is BITS_PER_LONG-1.
+ * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0.
+ */
+static inline int count_trailing_zeros(unsigned long x)
+{
+#define COUNT_TRAILING_ZEROS_0 (-1)
+
+	if (sizeof(x) == 4)
+		return ffs(x);
+	else
+		return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0;
+}
+
+#endif /* _ASM_GENERIC_BITOPS_COUNT_ZEROS_H_ */
lib/mpi/longlong.h +4 -138

230 lines not on this page · raw message

lib/mpi/mpi-bit.c +1 -1
index 5687248..503537e 100644
@@ -45,7 +45,7 @@ unsigned mpi_get_nbits(MPI a)
 	if (a->nlimbs) {
 		mpi_limb_t alimb = a->d[a->nlimbs - 1];
 		if (alimb)
-			count_leading_zeros(n, alimb);
+			n = count_leading_zeros(alimb);
 		else
 			n = BITS_PER_MPI_LIMB;
 		n = BITS_PER_MPI_LIMB - n + (a->nlimbs - 1) * BITS_PER_MPI_LIMB;
lib/mpi/mpi-pow.c +2 -2
index 67f3e79..5464c87 100644
@@ -77,7 +77,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
 	mp = mp_marker = mpi_alloc_limb_space(msize);
 	if (!mp)
 		goto enomem;
-	count_leading_zeros(mod_shift_cnt, mod->d[msize - 1]);
+	mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
 	if (mod_shift_cnt)
 		mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
 	else
@@ -169,7 +169,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
 
 		i = esize - 1;
 		e = ep[i];
-		count_leading_zeros(c, e);
+		c = count_leading_zeros(e);
 		e = (e << c) << 1;	/* shift the exp bits to the left, lose msb */
 		c = BITS_PER_MPI_LIMB - 1 - c;
 
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · David Howells

Provide count_leading/trailing_zeros() macros based on extant arch bit scanning functions rather than reimplementing from scratch in MPILIB.

Whilst we're at it, turn count_foo_zeros(n, x) into n = count_foo_zeros(x).

Quoted 7 lines

Also move the definition to asm-generic as other people may be interested in using it.

Cc David S. Miller <[email protected]>
Cc Dmitry Kasatkin <[email protected]>
Cc Arnd Bergmann <[email protected]>

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

#2 lore raw
Quoted 4 lines · Arnd Bergmann

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

Some arches (such as Sparc, I think) have count-leading-zero instructions.

David

#3 lore raw

From: David Howells <[email protected]>
Date: Fri, 20 Jul 2012 15:21:39 +0100

Quoted 8 lines
Quoted 4 lines · Arnd Bergmann

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

Some arches (such as Sparc, I think) have count-leading-zero instructions.

Yes, newer sparc64 chips have leading-zero-detect, and I was pretty sure that powerpc had something similar. It's called count-leading- zeros or something like that.

maintainer
#6 lore raw
In reply to David Howells
Quoted 9 lines · David Howells
Quoted 4 lines · Arnd Bergmann

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

Some arches (such as Sparc, I think) have count-leading-zero instructions.

But I guess they would still use the same definition of

+static inline int count_leading_zeros(unsigned long x) +{
+ if (sizeof(x) == 4)
+ return BITS_PER_LONG - fls(x);
+ else
+ return BITS_PER_LONG - fls64(x);
+}

and just provide their own fls().

Arnd

#4 lore raw
In reply to David Miller
Quoted 7 lines · David Miller
Quoted 7 lines
Quoted 4 lines · Arnd Bergmann

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

Quoted 5 lines
Quoted 1 line

Some arches (such as Sparc, I think) have count-leading-zero instructions.

Yes, newer sparc64 chips have leading-zero-detect, and I was pretty sure that powerpc had something similar. It's called count-leading- zeros or something like that.

And gcc has a __builtin_clz.

#5 lore raw

From: Jan Engelhardt <[email protected]>
Date: Fri, 10 Aug 2012 14:51:49 +0200 (CEST)

Quoted 9 lines · David Miller
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Arnd Bergmann

I don't generally like to put stuff into asm-generic when it's unlikely to be overridden by architectures. It would really belong into include/linux, but then again we have all the other bitops in asm-generic as well, so whatever...

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Some arches (such as Sparc, I think) have count-leading-zero instructions.

Yes, newer sparc64 chips have leading-zero-detect, and I was pretty sure that powerpc had something similar. It's called count-leading- zeros or something like that.

And gcc has a __builtin_clz.

Which I can't use. I have to patch the code at run time based upon whether the cpu has the 'lzd' instruction or not.