rcx

miscellaneous C library
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 8458a01b297853e80ec071bae61cc34efebf57a4
parent 4992f6d8b0053151cdb7d3749025eaeb436033b8
Author: Robert Russell <robert@rr3.xyz>
Date:   Sat,  4 Jan 2025 20:58:08 -0800

Add sign extension

Diffstat:
Minc/bits.h | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/inc/bits.h b/inc/bits.h @@ -288,6 +288,25 @@ r_swap64(u64 n) { #endif +/* ----- Sign extension ----- */ + +static inline u16 r_sext_16_8(u8 x) { return (u16)(i16)(i8)x; } + +static inline u32 r_sext_32_8(u8 x) { return (u32)(i32)(i8)x; } +static inline u32 r_sext_32_16(u16 x) { return (u32)(i32)(i16)x; } + +static inline u64 r_sext_64_8(u8 x) { return (u64)(i64)(i8)x; } +static inline u64 r_sext_64_16(u16 x) { return (u64)(i64)(i16)x; } +static inline u64 r_sext_64_32(u32 x) { return (u64)(i64)(i32)x; } + +#ifdef R_HAVE_128 +static inline u128 r_sext_128_8(u8 x) { return (u128)(i128)(i8)x; } +static inline u128 r_sext_128_16(u16 x) { return (u128)(i128)(i16)x; } +static inline u128 r_sext_128_32(u32 x) { return (u128)(i128)(i32)x; } +static inline u128 r_sext_128_64(u64 x) { return (u128)(i128)(i64)x; } +#endif + + /* ----- Endian conversions ----- */ /* There is 2x redundancy here (e.g., ltoh = htol), but this allows code using