Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1247974 > unrolled thread

bitops.h: improve sign extending API

Started byMartin Kepplinger <martink@posteo.de>
First post2015-10-15 18:30 +0200
Last post2015-10-15 20:10 +0200
Articles 4 — 2 participants

Back to article view | Back to linux.kernel


Contents

  bitops.h: improve sign extending API Martin Kepplinger <martink@posteo.de> - 2015-10-15 18:30 +0200
    [PATCH 2/2] bitops.h: add sign_extend64() Martin Kepplinger <martink@posteo.de> - 2015-10-15 18:30 +0200
    [PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation Martin Kepplinger <martink@posteo.de> - 2015-10-15 18:30 +0200
      Re: [PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation "George Spelvin" <linux@horizon.com> - 2015-10-15 20:10 +0200

#1247974 — bitops.h: improve sign extending API

FromMartin Kepplinger <martink@posteo.de>
Date2015-10-15 18:30 +0200
Subjectbitops.h: improve sign extending API
Message-ID<qjTzI-3md-11@gated-at.bofh.it>
PATCH 1/2 improves the doc of sign_extend32()

This should help to avoid different manual approaches to sign extension


PATCH 2/2 adds sign_extend64()

An informal example of what could follow in
arch/sh/kernel/traps_64.c after PATCH 2/2:

@@ -101,7 +102,7 @@ static int generate_and_check_address(struct pt_regs *regs,
                __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 {


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1247975 — [PATCH 2/2] bitops.h: add sign_extend64()

FromMartin Kepplinger <martink@posteo.de>
Date2015-10-15 18:30 +0200
Subject[PATCH 2/2] bitops.h: add sign_extend64()
Message-ID<qjTzI-3md-15@gated-at.bofh.it>
In reply to#1247974
Months back, this was discussed, see https://lkml.org/lkml/2015/1/18/289
The result was the 64-bit version being "likely fine", "valuable" and
"correct". The discussion only fell asleep but since there are possible
users, let's add it.

Signed-off-by: Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
---
 include/linux/bitops.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 2177c01..1672b74 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -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)
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1247977 — [PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation

FromMartin Kepplinger <martink@posteo.de>
Date2015-10-15 18:30 +0200
Subject[PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation
Message-ID<qjTzI-3md-13@gated-at.bofh.it>
In reply to#1247974
It is often overlooked that sign_extend32(), despite it's name, is safe
to use for 16 and 8 bit types aswell. This should help that sign extension
isn't done manually some other way.

Signed-off-by: Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
---
 include/linux/bitops.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index e635533..2177c01 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -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
+ *
+ * This is safe to use for 16- and 8-bit types aswell.
  */
 static inline __s32 sign_extend32(__u32 value, int index)
 {
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1248056 — Re: [PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation

From"George Spelvin" <linux@horizon.com>
Date2015-10-15 20:10 +0200
SubjectRe: [PATCH 1/2] bitops.h: Improve sign_extend32()'s documentation
Message-ID<qjV8v-5Jg-39@gated-at.bofh.it>
In reply to#1247977
"as well" is two words, not one.

Also, a third patch adding users of sign_extend64 would help;
otherwise it's just adding dead code.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web