From 5dcef7c6dd2c50cc5a96dc86efcab737fd2b7433 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sun, 19 Jan 2014 12:02:38 -0800 Subject: Adding IVAL64() and SIVAL64(). --- byteorder.h | 26 +++++++++++++++++++++++++- io.c | 6 +++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/byteorder.h b/byteorder.h index 831644fe..22e807ab 100644 --- a/byteorder.h +++ b/byteorder.h @@ -38,9 +38,11 @@ #define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8) #define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16) +#define IVAL64(buf,pos) (IVAL(buf,pos)|(int64)IVAL(buf,(pos)+4)<<32) #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8) #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) -#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) +#define SIVAL(buf,pos,val) SIVALX(buf,pos,(uint32)(val)) +#define SIVAL64(buf,pos,val) (SIVAL(buf,pos,val),SIVAL(buf,(pos)+4,(val)>>32)) #define IVALu(buf,pos) IVAL(buf,pos) #define SIVALu(buf,pos,val) SIVAL(buf,pos,val) @@ -95,6 +97,28 @@ SIVAL(char *buf, int pos, uint32 val) SIVALu((uchar*)buf, pos, val); } +static inline int64 +IVAL64(const char *buf, int pos) +{ + union { + const char *b; + const int64 *num; + } u; + u.b = buf + pos; + return *u.num; +} + +static inline void +SIVAL64(char *buf, int pos, int64 val) +{ + union { + char *b; + int64 *num; + } u; + u.b = buf + pos; + *u.num = val; +} + # endif /* !AVOID_BYTEORDER_INLINE */ #endif /* !CAREFUL_ALIGNMENT */ diff --git a/io.c b/io.c index 264824e7..354fc133 100644 --- a/io.c +++ b/io.c @@ -1785,7 +1785,7 @@ int64 read_varlong(int f, uchar min_bytes) #if SIZEOF_INT64 < 8 u.x = IVAL(u.b,0); #elif CAREFUL_ALIGNMENT - u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32); + u.x = IVAL64(u.b,0); #endif return u.x; } @@ -2037,10 +2037,10 @@ void write_varlong(int f, int64 x, uchar min_bytes) uchar bit; int cnt = 8; - SIVAL(b, 1, x); #if SIZEOF_INT64 >= 8 - SIVAL(b, 5, x >> 32); + SIVAL64(b, 1, x); #else + SIVAL(b, 1, x); if (x <= 0x7FFFFFFF && x >= 0) memset(b + 5, 0, 4); else { -- cgit v1.2.1