public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] bitops.h: add sign_extend8(), 16 and 64 functions
@ 2015-01-12 17:22 Martin Kepplinger
  2015-01-12 17:28 ` [RFC] input: gtco: use bitops' sign_extend8 Martin Kepplinger
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Martin Kepplinger @ 2015-01-12 17:22 UTC (permalink / raw)
  To: mingo
  Cc: peterz, paulmck, tytso, maxime.coquelin, linux, linux-kernel,
	Martin Kepplinger

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.

Signed-off-by: Martin Kepplinger <martink@posteo.de>
Suggested-by: Christoph Muellner <christoph.muellner@theobroma-systems.com>
---
 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)
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2015-02-12 11:12 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-12 17:22 [PATCH v2] bitops.h: add sign_extend8(), 16 and 64 functions Martin Kepplinger
2015-01-12 17:28 ` [RFC] input: gtco: use bitops' sign_extend8 Martin Kepplinger
2015-01-12 17:28   ` [RFC] rtc: use sign_extend8 instead of manual conversion Martin Kepplinger
2015-01-12 17:28   ` [RFC] media: stb0899: use sign_extend8 instead of manual work Martin Kepplinger
2015-01-12 17:28   ` [RFC] hwmon: jc42: use bitops' sign_extend16 Martin Kepplinger
2015-01-12 19:53     ` Guenter Roeck
2015-01-12 19:48 ` [PATCH v2] bitops.h: add sign_extend8(), 16 and 64 functions Guenter Roeck
2015-01-14 16:56 ` [RFC PATCH 0/4] Example changes using proposed sign_extend functions Martin Kepplinger
2015-01-14 16:56   ` [PATCH 1/4] input: gtco: use bitops' sign_extend8 Martin Kepplinger
2015-01-14 16:56   ` [PATCH 2/4] rtc: use sign_extend8 instead of manual conversion Martin Kepplinger
2015-01-14 16:56   ` [PATCH 3/4] media: stb0899: use sign_extend8 instead of manual work Martin Kepplinger
2015-01-14 16:56   ` [PATCH 4/4] hwmon: jc42: use bitops' sign_extend16 Martin Kepplinger
2015-01-18 19:06 ` [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions tip-bot for Martin Kepplinger
     [not found]   ` <CA+55aFyc7TbnLBi3rcQDrtkwg9TgDnb5dAfupMGSbTKZC6Xd0g@mail.gmail.com>
2015-01-19  1:11     ` Guenter Roeck
2015-01-19  4:00     ` Linus Torvalds
2015-01-20 12:30       ` [PATCH v3 0/2] bitops.h: add sign_extend64() API and improve doc Martin Kepplinger
2015-01-20 12:30         ` [PATCH 1/2] bitops.h: improve documentation for sign_extend32() Martin Kepplinger
2015-01-20 12:30         ` [PATCH 2/2] bitops.h: Add sign_extend64() API Martin Kepplinger
2015-01-19 10:04     ` [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions Peter Zijlstra
2015-01-21 20:22       ` H. Peter Anvin
2015-02-05  7:17         ` Ingo Molnar
2015-02-05 14:11           ` Guenter Roeck
2015-02-12 11:11             ` Example use of sign_extend64() Martin Kepplinger
2015-02-12 11:11               ` [RFC][PATCH] arch: sh: use sign_extend64() for sign extension Martin Kepplinger
2015-02-05 16:39           ` [tip:core/types] bitops: Add sign_extend8(), 16 and 64 functions H. Peter Anvin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox