Re: [PATCH v2] bitops.h: add sign_extend8(), 16 and 64 functions
On Mon, Jan 12, 2015 at 06:22:50PM +0100, Martin Kepplinger wrote: > 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 > Suggested-by: Christoph Muellner Reviewed-by: Guenter Roeck Guenter -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH v2] 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. Signed-off-by: Martin Kepplinger Suggested-by: Christoph Muellner --- 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 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v2] bitops.h: add sign_extend8(), 16 and 64 functions
On Mon, Jan 12, 2015 at 06:22:50PM +0100, Martin Kepplinger wrote: 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 mart...@posteo.de Suggested-by: Christoph Muellner christoph.muell...@theobroma-systems.com Reviewed-by: Guenter Roeck li...@roeck-us.net Guenter -- To unsubscribe from this list: send the line unsubscribe linux-kernel in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH v2] 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. Signed-off-by: Martin Kepplinger mart...@posteo.de Suggested-by: Christoph Muellner christoph.muell...@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=index8) 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=index16) 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=index32) 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=index64) 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 majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/