X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fopenjp2%2Fopj_intmath.h;h=cce7a3cafa720d5cae928fad7d317d20edf58aee;hb=HEAD;hp=ad13597661319d8eda186143e3fb7b2c0019bdd3;hpb=968e36bbd989fdf789c6a02c80c747346bb851c5;p=openjpeg.git diff --git a/src/lib/openjp2/opj_intmath.h b/src/lib/openjp2/opj_intmath.h index ad135976..cce7a3ca 100644 --- a/src/lib/openjp2/opj_intmath.h +++ b/src/lib/openjp2/opj_intmath.h @@ -170,7 +170,18 @@ Divide an integer and round upwards static INLINE OPJ_UINT32 opj_uint_ceildiv(OPJ_UINT32 a, OPJ_UINT32 b) { assert(b); - return (a + b - 1) / b; + return (OPJ_UINT32)(((OPJ_UINT64)a + b - 1) / b); +} + +/** +Divide an integer and round upwards +@return Returns a divided by b +*/ +static INLINE OPJ_UINT32 opj_uint64_ceildiv_res_uint32(OPJ_UINT64 a, + OPJ_UINT64 b) +{ + assert(b); + return (OPJ_UINT32)((a + b - 1) / b); } /** @@ -208,6 +219,16 @@ static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) { return a >> b; } + +/** +Divide an integer by a power of 2 and round downwards +@return Returns a divided by 2^b +*/ +static INLINE OPJ_UINT32 opj_uint_floordivpow2(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return a >> b; +} + /** Get logarithm of an integer and round downwards @return Returns log2(a) @@ -266,6 +287,44 @@ static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b) return (OPJ_INT32)(temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ; } +/** +Addition two signed integers with a wrap-around behaviour. +Assumes complement-to-two signed integers. +@param a +@param b +@return Returns a + b +*/ +static INLINE OPJ_INT32 opj_int_add_no_overflow(OPJ_INT32 a, OPJ_INT32 b) +{ + void* pa = &a; + void* pb = &b; + OPJ_UINT32* upa = (OPJ_UINT32*)pa; + OPJ_UINT32* upb = (OPJ_UINT32*)pb; + OPJ_UINT32 ures = *upa + *upb; + void* pures = &ures; + OPJ_INT32* ipres = (OPJ_INT32*)pures; + return *ipres; +} + +/** +Subtract two signed integers with a wrap-around behaviour. +Assumes complement-to-two signed integers. +@param a +@param b +@return Returns a - b +*/ +static INLINE OPJ_INT32 opj_int_sub_no_overflow(OPJ_INT32 a, OPJ_INT32 b) +{ + void* pa = &a; + void* pb = &b; + OPJ_UINT32* upa = (OPJ_UINT32*)pa; + OPJ_UINT32* upb = (OPJ_UINT32*)pb; + OPJ_UINT32 ures = *upa - *upb; + void* pures = &ures; + OPJ_INT32* ipres = (OPJ_INT32*)pures; + return *ipres; +} + /* ----------------------------------------------------------------------- */ /*@}*/