Skip to contentSkip to conversation

bitops.h: add sign_extend8(), 16 and 64 functions

PATCH In review Martin Kepplinger 7 participants 25 messages Jan 12, 2015 – Feb 12, 2015 1 message not in the archive

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

PATCH v2 1/1 bitops.h: add sign_extend8(), 16 and 64 functions

This adds helper functions for sign-extending signed values of any lower (hardware-)given size to s8, s16 or s64 respectively, just like sign_extend32() for s32.

---
 include/linux/bitops.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/bitops.h +33 -0
index 5d858e0..9c31680 100644
@@ -161,6 +161,28 @@ static inline __u8 ror8(__u8 word, unsigned int shift)
 }
 
 /**
+ * sign_extend8 - sign extend a 8-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<8) to sign bit
+ */
+static inline __s8 sign_extend8(__u8 value, int index)
+{
+	__u8 shift = 7 - index;
+	return (__s8)(value << shift) >> shift;
+}
+
+/**
+ * sign_extend16 - sign extend a 16-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<16) to sign bit
+ */
+static inline __s16 sign_extend16(__u16 value, int index)
+{
+	__u8 shift = 15 - index;
+	return (__s16)(value << shift) >> shift;
+}
+
+/**
  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
  * @value: value to sign extend
  * @index: 0 based bit index (0<=index<32) to sign bit
@@ -171,6 +193,17 @@ static inline __s32 sign_extend32(__u32 value, int index)
 	return (__s32)(value << shift) >> shift;
 }
 
+/**
+ * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<64) to sign bit
+ */
+static inline __s64 sign_extend64(__u64 value, int index)
+{
+	__u8 shift = 63 - index;
+	return (__s64)(value << shift) >> shift;
+}
+
 static inline unsigned fls_long(unsigned long l)
 {
 	if (sizeof(l) == 4)
Signature · 1 line
2.1.4
#1 lore raw

RFC 1/1 input: gtco: use bitops' sign_extend8

Example change, using new sign_extend functions.

---
 drivers/input/tablet/gtco.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/input/tablet/gtco.c +3 -8
index 8580456..25b3834 100644
@@ -59,7 +59,7 @@ Scott Hill [email protected]
 #include <asm/uaccess.h>
 #include <asm/unaligned.h>
 #include <asm/byteorder.h>
-
+#include <linux/bitops.h>
 
 #include <linux/usb/input.h>
 
@@ -666,13 +666,8 @@ static void gtco_urb_callback(struct urb *urbinfo)
 		case 4:
 			/* Tilt */
 
-			/* Sign extend these 7 bit numbers.  */
-			if (device->buffer[6] & 0x40)
-				device->buffer[6] |= 0x80;
-
-			if (device->buffer[7] & 0x40)
-				device->buffer[7] |= 0x80;
-
+			device->buffer[6] = sign_extend8(device->buffer[6], 6);
+			device->buffer[7] = sign_extend8(device->buffer[6], 6);
 
 			valsigned = (device->buffer[6]);
 			input_report_abs(inputdev, ABS_TILT_X, (s32)valsigned);
Signature · 1 line
2.1.4
#2 lore raw

RFC 1/1 hwmon: jc42: use bitops' sign_extend16

Example change, using new sign_extend functions.

---
 drivers/hwmon/jc42.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/hwmon/jc42.c +2 -5
index 388f8bc..d0582a3 100644
@@ -31,6 +31,7 @@
 #include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
+#include <linux/bitops.h>
 
 /* Addresses to scan */
 static const unsigned short normal_i2c[] = {
@@ -213,11 +214,7 @@ static u16 jc42_temp_to_reg(int temp, bool extended)
 
 static int jc42_temp_from_reg(s16 reg)
 {
-	reg &= 0x1fff;
-
-	/* sign extend register */
-	if (reg & 0x1000)
-		reg |= 0xf000;
+	reg = sign_extend16(reg, 12);
 
 	/* convert from 0.0625 to 0.001 resolution */
 	return reg * 125 / 2;
Signature · 1 line
2.1.4
#4 lore raw

RFC 1/1 rtc: use sign_extend8 instead of manual conversion

In reply to Martin Kepplinger

Example change, using new sign_extend functions.

---
 drivers/rtc/rtc-x1205.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-x1205.c +2 -2
index b1de58e..3ec0b95 100644
@@ -22,6 +22,7 @@
 #include <linux/rtc.h>
 #include <linux/delay.h>
 #include <linux/module.h>
+#include <linux/bitops.h>
 
 #define DRV_VERSION "1.0.8"
 
@@ -366,8 +367,7 @@ static int x1205_get_atrim(struct i2c_client *client, int *trim)
 	 * perform sign extension. The formula is
 	 * Catr = (atr * 0.25pF) + 11.00pF.
 	 */
-	if (atr & 0x20)
-		atr |= 0xC0;
+	atr = sign_extend8(atr, 5);
 
 	dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __func__, atr, atr);
 
Signature · 1 line
2.1.4
#5 lore raw

RFC 1/1 media: stb0899: use sign_extend8 instead of manual work

In reply to Martin Kepplinger

Example change, using new sign_extend functions.

---
 drivers/media/dvb-frontends/stb0899_algo.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/dvb-frontends/stb0899_algo.c +2 -3
index 93596e0..7bbcfde 100644
@@ -19,6 +19,7 @@
 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include <linux/bitops.h>
 #include "stb0899_drv.h"
 #include "stb0899_priv.h"
 #include "stb0899_reg.h"
@@ -1490,9 +1491,7 @@ enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
 		/* Store signal parameters	*/
 		offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
 
-		/* sign extend 30 bit value before using it in calculations */
-		if (offsetfreq & (1 << 29))
-			offsetfreq |= -1 << 30;
+		offsetfreq = sign_extend32(offset_freq, 29);
 
 		offsetfreq = offsetfreq / ((1 << 30) / 1000);
 		offsetfreq *= (internal->master_clk / 1000000);
Signature · 1 line
2.1.4
maintainer Reviewed-by on v1 1/1
#3 lore raw

Re: [RFC] hwmon: jc42: use bitops' sign_extend16

In reply to Martin Kepplinger
Quoted 1 line · Martin Kepplinger

Example change, using new sign_extend functions.

No need to drop the Signed-off: line; it only means that I can not take the patch even if I want to. Also, if the patch itself is final, no need to state that it is an example in the description. Instead, use an introductory mail (RFC PATCH 0/x] to explain the background.

Otherwise looks good; feel free to add

to the next version.

Thanks,
Guenter

#7 lore raw

cover Example changes using proposed sign_extend functions

In reply to Martin Kepplinger

These are 4 of probably many examples of what could be changed if the proposed sign_extend8, 16 and 64 are added to bitops.h, now as real patches.

Again, keep in mind, only take them if "PATCH v2 add sign_exted8, 16 and 64" is included first.

#8 lore raw

PATCH 1/4 input: gtco: use bitops' sign_extend8

---
 drivers/input/tablet/gtco.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/input/tablet/gtco.c +3 -8
index 8580456..25b3834 100644
@@ -59,7 +59,7 @@ Scott Hill [email protected]
 #include <asm/uaccess.h>
 #include <asm/unaligned.h>
 #include <asm/byteorder.h>
-
+#include <linux/bitops.h>
 
 #include <linux/usb/input.h>
 
@@ -666,13 +666,8 @@ static void gtco_urb_callback(struct urb *urbinfo)
 		case 4:
 			/* Tilt */
 
-			/* Sign extend these 7 bit numbers.  */
-			if (device->buffer[6] & 0x40)
-				device->buffer[6] |= 0x80;
-
-			if (device->buffer[7] & 0x40)
-				device->buffer[7] |= 0x80;
-
+			device->buffer[6] = sign_extend8(device->buffer[6], 6);
+			device->buffer[7] = sign_extend8(device->buffer[6], 6);
 
 			valsigned = (device->buffer[6]);
 			input_report_abs(inputdev, ABS_TILT_X, (s32)valsigned);
Signature · 1 line
2.1.4
#11 lore raw

PATCH 4/4 hwmon: jc42: use bitops' sign_extend16

In reply to Martin Kepplinger
---
 drivers/hwmon/jc42.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/hwmon/jc42.c +2 -5
index 388f8bc..d0582a3 100644
@@ -31,6 +31,7 @@
 #include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
+#include <linux/bitops.h>
 
 /* Addresses to scan */
 static const unsigned short normal_i2c[] = {
@@ -213,11 +214,7 @@ static u16 jc42_temp_to_reg(int temp, bool extended)
 
 static int jc42_temp_from_reg(s16 reg)
 {
-	reg &= 0x1fff;
-
-	/* sign extend register */
-	if (reg & 0x1000)
-		reg |= 0xf000;
+	reg = sign_extend16(reg, 12);
 
 	/* convert from 0.0625 to 0.001 resolution */
 	return reg * 125 / 2;
Signature · 1 line
2.1.4
#9 lore raw

PATCH 2/4 rtc: use sign_extend8 instead of manual conversion

In reply to Martin Kepplinger
---
 drivers/rtc/rtc-x1205.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-x1205.c +2 -2
index b1de58e..3ec0b95 100644
@@ -22,6 +22,7 @@
 #include <linux/rtc.h>
 #include <linux/delay.h>
 #include <linux/module.h>
+#include <linux/bitops.h>
 
 #define DRV_VERSION "1.0.8"
 
@@ -366,8 +367,7 @@ static int x1205_get_atrim(struct i2c_client *client, int *trim)
 	 * perform sign extension. The formula is
 	 * Catr = (atr * 0.25pF) + 11.00pF.
 	 */
-	if (atr & 0x20)
-		atr |= 0xC0;
+	atr = sign_extend8(atr, 5);
 
 	dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __func__, atr, atr);
 
Signature · 1 line
2.1.4
#10 lore raw

PATCH 3/4 media: stb0899: use sign_extend8 instead of manual work

In reply to Martin Kepplinger
---
 drivers/media/dvb-frontends/stb0899_algo.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/dvb-frontends/stb0899_algo.c +2 -3
index 93596e0..7bbcfde 100644
@@ -19,6 +19,7 @@
 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include <linux/bitops.h>
 #include "stb0899_drv.h"
 #include "stb0899_priv.h"
 #include "stb0899_reg.h"
@@ -1490,9 +1491,7 @@ enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
 		/* Store signal parameters	*/
 		offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
 
-		/* sign extend 30 bit value before using it in calculations */
-		if (offsetfreq & (1 << 29))
-			offsetfreq |= -1 << 30;
+		offsetfreq = sign_extend32(offset_freq, 29);
 
 		offsetfreq = offsetfreq / ((1 << 30) / 1000);
 		offsetfreq *= (internal->master_clk / 1000000);
Signature · 1 line
2.1.4
#12 lore raw

[tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to Martin Kepplinger
bitops: Add sign_extend8(), 16 and 64 functions
Commit-ID:  7e9358073d3f0ed0a028c48aa54009b3296dffc9
Gitweb:     http://git.kernel.org/tip/7e9358073d3f0ed0a028c48aa54009b3296dffc9
Author:     Martin Kepplinger <[email protected]>
AuthorDate: Mon, 12 Jan 2015 18:22:50 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sun, 18 Jan 2015 20:03:51 +0100

bitops: Add sign_extend8(), 16 and 64 functions

This adds helper functions for sign-extending signed values of any lower (hardware-)given size to s8, s16 or s64 respectively, just like sign_extend32() for s32.

This completes the sign_extend*() API family to work on all bit sizes like most other bitops APIs do.

---
 include/linux/bitops.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/bitops.h +33 -0
index 5d858e0..9c31680 100644
@@ -161,6 +161,28 @@ static inline __u8 ror8(__u8 word, unsigned int shift)
 }
 
 /**
+ * sign_extend8 - sign extend a 8-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<8) to sign bit
+ */
+static inline __s8 sign_extend8(__u8 value, int index)
+{
+	__u8 shift = 7 - index;
+	return (__s8)(value << shift) >> shift;
+}
+
+/**
+ * sign_extend16 - sign extend a 16-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<16) to sign bit
+ */
+static inline __s16 sign_extend16(__u16 value, int index)
+{
+	__u8 shift = 15 - index;
+	return (__s16)(value << shift) >> shift;
+}
+
+/**
  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
  * @value: value to sign extend
  * @index: 0 based bit index (0<=index<32) to sign bit
@@ -171,6 +193,17 @@ static inline __s32 sign_extend32(__u32 value, int index)
 	return (__s32)(value << shift) >> shift;
 }
 
+/**
+ * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<64) to sign bit
+ */
+static inline __s64 sign_extend64(__u64 value, int index)
+{
+	__u8 shift = 63 - index;
+	return (__s64)(value << shift) >> shift;
+}
+
 static inline unsigned fls_long(unsigned long l)
 {
 	if (sizeof(l) == 4)
maintainer
#14 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to a message not in the archive
Quoted 2 lines · Linus Torvalds

Why?

Probably because some of us are, unfortunately, sometimes clueless :-(.

Guenter

Quoted 91 lines

The 8- and 16- bit versions are the same as the 32-bit one. This seems pointless. If you want something where the sign is in bit 3, they all return the same value, just the return type differs, but that's really a *caller* thing, no?

Linus

On Jan 19, 2015 7:07 AM, "tip-bot for Martin Kepplinger" <[email protected] <mailto:[email protected]>> wrote:

Commit-ID: 7e9358073d3f0ed0a028c48aa54009b3296dffc9
Gitweb: http://git.kernel.org/tip/7e9358073d3f0ed0a028c48aa54009b3296dffc9
Author: Martin Kepplinger <[email protected] <mailto:[email protected]>>
AuthorDate: Mon, 12 Jan 2015 18:22:50 +0100
Committer: Ingo Molnar <[email protected] <mailto:[email protected]>>
CommitDate: Sun, 18 Jan 2015 20:03:51 +0100

bitops: Add sign_extend8(), 16 and 64 functions

This adds helper functions for sign-extending signed values of any lower (hardware-)given size to s8, s16 or s64 respectively, just like sign_extend32() for s32.

This completes the sign_extend*() API family to work on all bit sizes like most other bitops APIs do.

Suggested-by Christoph Muellner <[email protected] <mailto:[email protected]>>
Signed-off-by Martin Kepplinger <[email protected] <mailto:[email protected]>>
Reviewed-by Guenter Roeck <[email protected] <mailto:[email protected]>>
Signed-off-by Peter Zijlstra (Intel) <[email protected] <mailto:[email protected]>>
Cc John Sullivan <[email protected] <mailto:[email protected]>>
Cc Linus Torvalds <[email protected] <mailto:[email protected]>>
Cc Maxime COQUELIN <[email protected] <mailto:[email protected]>>
Cc Paul E. McKenney <[email protected] <mailto:[email protected]>>
Cc Rasmus Villemoes <[email protected] <mailto:[email protected]>>
Cc Theodore Ts'o <[email protected] <mailto:[email protected]>>
Cc Andrew Morton <[email protected] <mailto:[email protected]>>

Signed-off-by <http://lkml.kernel.org/r/[email protected] Signed-off-by>: Ingo Molnar <[email protected] <mailto:[email protected]>>
---
include/linux/bitops.h | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 5d858e0..9c31680 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -161,6 +161,28 @@ static inline __u8 ror8(__u8 word, unsigned int shift)
}

/**
+ * sign_extend8 - sign extend a 8-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<8) to sign bit
+ */
+static inline __s8 sign_extend8(__u8 value, int index)
+{
+ __u8 shift = 7 - index;
+ return (__s8)(value << shift) >> shift;
+}
+
+/**
+ * sign_extend16 - sign extend a 16-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<16) to sign bit
+ */
+static inline __s16 sign_extend16(__u16 value, int index)
+{
+ __u8 shift = 15 - index;
+ return (__s16)(value << shift) >> shift;
+}
+
+/**
* sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
* @value: value to sign extend
* @index: 0 based bit index (0<=index<32) to sign bit
@@ -171,6 +193,17 @@ static inline __s32 sign_extend32(__u32 value, int index)
return (__s32)(value << shift) >> shift;
}

+/**
+ * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<64) to sign bit
+ */
+static inline __s64 sign_extend64(__u64 value, int index)
+{
+ __u8 shift = 63 - index;
+ return (__s64)(value << shift) >> shift;
+}
+
static inline unsigned fls_long(unsigned long l)
{
if (sizeof(l) == 4)

#15 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to a message not in the archive
Quoted 2 lines · Linus Torvalds

The 8- and 16- bit versions are the same as the 32-bit one.

Side note: the 64-bit one is actually different on 32-bit architectures, not because it would generate a different value, but because the 64-bit overhead likely kills you. It might be that the compiler is smart enough that we'd *only* need the 64-bit version, but for non-constant bit numbers, it's likely hard for gcc to generate sane single-register versions, and so the 64-bit version is likely fine.

But I don't see the 8-bit and 16-bit versions generating better code on any sane architecture.

Linus

#19 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to a message not in the archive
Quoted 6 lines · Linus Torvalds

Why?

The 8- and 16- bit versions are the same as the 32-bit one. This seems pointless. If you want something where the sign is in bit 3, they all return the same value, just the return type differs, but that's really a *caller* thing, no?

Even for the 8bit ones? Since we have the *H and *L register we have more 8 bit regs than we have 16/32 bit regs and it might just be worth it.

Since these are inlines the whole calling convention which would clobber the whole of eax can go away.

Now granted, this is all very tenuous at best. A more convincing argument might be that of documentation; calling sign_extend32() on something you know to be a byte might be less intuitive.

#17 lore raw

PATCH v3 1/2 bitops.h: improve documentation for sign_extend32()

---
 include/linux/bitops.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/bitops.h +2 -0
index 5d858e0..336f22b 100644
@@ -164,6 +164,8 @@ static inline __u8 ror8(__u8 word, unsigned int shift)
  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
  * @value: value to sign extend
  * @index: 0 based bit index (0<=index<32) to sign bit
+ *
+ * Safe to use for sign extending to 8 and 16 bit data types aswell.
  */
 static inline __s32 sign_extend32(__u32 value, int index)
 {
Signature · 1 line
2.1.4
#18 lore raw

PATCH v3 2/2 bitops.h: Add sign_extend64() API

In reply to Martin Kepplinger

Add sign_extend64() for sign extending any (hardware-)given signed value greater than 32 bits to s64.

---
 include/linux/bitops.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/bitops.h +11 -0
index 336f22b..2fd47c8 100644
@@ -173,6 +173,17 @@ static inline __s32 sign_extend32(__u32 value, int index)
 	return (__s32)(value << shift) >> shift;
 }
 
+/**
+ * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
+ * @value: value to sign extend
+ * @index: 0 based bit index (0<=index<64) to sign bit
+ */
+static inline __s64 sign_extend64(__u64 value, int index)
+{
+	__u8 shift = 63 - index;
+	return (__s64)(value << shift) >> shift;
+}
+
 static inline unsigned fls_long(unsigned long l)
 {
 	if (sizeof(l) == 4)
Signature · 1 line
2.1.4
#20 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 6 lines · Linus Torvalds

Why?

The 8- and 16- bit versions are the same as the 32-bit one. This seems pointless. If you want something where the sign is in bit 3, they all return the same value, just the return type differs, but that's really a *caller* thing, no?

Even for the 8bit ones? Since we have the *H and *L register we have more 8 bit regs than we have 16/32 bit regs and it might just be worth it.

Fewer, actually. gcc doesn't really use the H registers much, and instead considers 8-bit values to occupy the whole register, but that means only four are available in 32-bit mode.

-hpa

#21 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

Quoted 9 lines · H. Peter Anvin
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Linus Torvalds

Why?

The 8- and 16- bit versions are the same as the 32-bit one. This seems pointless. If you want something where the sign is in bit 3, they all return the same value, just the return type differs, but that's really a *caller* thing, no?

Quoted 5 lines
Quoted 3 lines

Even for the 8bit ones? Since we have the *H and *L register we have more 8 bit regs than we have 16/32 bit regs and it might just be worth it.

Fewer, actually. gcc doesn't really use the H registers much,

Is that true for other compilers as well?

Quoted 3 lines

and instead considers 8-bit values to occupy the whole register, but that means only four are available in 32-bit mode.

So where are we with this? Should I consider:

7e9358073d3f ("bitops: Add sign_extend8(), 16 and 64 functions")

NAK-ed due to having marginal benefits, or due to having no benefits whatsoever?

How about the two patch series from Martin Keppling - that does seem to be both beneficial and correct, agreed?

Thanks,

Ingo

maintainer
#22 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

Quoted 26 lines · Ingo Molnar
Quoted 9 lines · H. Peter Anvin
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Linus Torvalds

Why?

The 8- and 16- bit versions are the same as the 32-bit one. This seems pointless. If you want something where the sign is in bit 3, they all return the same value, just the return type differs, but that's really a *caller* thing, no?

Quoted 5 lines
Quoted 3 lines

Even for the 8bit ones? Since we have the *H and *L register we have more 8 bit regs than we have 16/32 bit regs and it might just be worth it.

Fewer, actually. gcc doesn't really use the H registers much,

Is that true for other compilers as well?

Quoted 3 lines

and instead considers 8-bit values to occupy the whole register, but that means only four are available in 32-bit mode.

So where are we with this? Should I consider:

Quoted 8 lines
   7e9358073d3f ("bitops: Add sign_extend8(), 16 and 64 functions")

NAK-ed due to having marginal benefits, or due to having no benefits whatsoever?

How about the two patch series from Martin Keppling - that does seem to be both beneficial and correct, agreed?

Do you mean the two patches improving the documentation of sign_extend32 and adding sign_extend64 ? I thought those would be valuable.

The discussion resulted in sign_extend32() being used for non-32-bit operations, so that by itself was quite useful.

Guenter

#25 lore raw

Re: [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions

In reply to Ingo Molnar
Quoted 9 lines · Ingo Molnar
Quoted 6 lines
Quoted 4 lines

Even for the 8bit ones? Since we have the *H and *L register we have more 8 bit regs than we have 16/32 bit regs and it might just be worth it.

Fewer, actually. gcc doesn't really use the H registers much,

Is that true for other compilers as well?

Probably. It wrecks havoc on register allocation, and it doesn't buy that much, especially on 64 bits where all the registers can be used for 8-bit operations.

-hpa

#23 lore raw

Example use of sign_extend64()

In reply to Guenter Roeck
Quoted 1 line

From a first look, there are quite some places in arch where sign extension

is done. I append one example for review although I didn't even build this. Don't use it unreviewed!

I guess the doc-addition to sign_extend32() would be valuable for it to be used more often in long term.

#24 lore raw

RFC PATCH 1/1 arch: sh: use sign_extend64() for sign extension

---
 arch/sh/kernel/traps_64.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sh/kernel/traps_64.c +2 -1
index 112ea11..4e04261 100644
@@ -25,6 +25,7 @@
 #include <linux/sysctl.h>
 #include <linux/module.h>
 #include <linux/perf_event.h>
+#include <linux/bitops.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 #include <asm/alignment.h>
@@ -101,7 +102,7 @@ static int generate_and_check_address(struct pt_regs *regs,
 	if (displacement_not_indexed) {
 		__s64 displacement;
 		displacement = (opcode >> 10) & 0x3ff;
-		displacement = ((displacement << 54) >> 54); /* sign extend */
+		displacement = sign_extend64(displacement, 9);
 		addr = (__u64)((__s64)base_address + (displacement << width_shift));
 	} else {
 		__u64 offset;
Signature · 1 line
2.1.4