Generic aligned malloc implementation.
[openjpeg.git] / src / lib / openjp2 / opj_intmath.h
index dc89895301f8a63e585d3504fdb681c25e2b9074..3f7934c470cbbf3bec7e9fab5488df7f8de74018 100644 (file)
@@ -81,6 +81,15 @@ static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32  a, OPJ_UINT32  b) {
        return (a > b) ? a : b;
 }
 
+/**
+ Get the saturated sum of two unsigned integers
+ @return Returns saturated sum of a+b
+ */
+static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b) {
+       OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b;
+       return (OPJ_UINT32)(-(OPJ_INT32)(sum >> 32)) | (OPJ_UINT32)sum;
+}
+
 /**
 Clamp an integer inside an interval
 @return
@@ -108,7 +117,7 @@ Divide an integer and round upwards
 @return Returns a divided by b
 */
 static INLINE OPJ_INT32 opj_int_ceildiv(OPJ_INT32 a, OPJ_INT32 b) {
-  assert(b);
+       assert(b);
        return (a + b - 1) / b;
 }
 
@@ -117,6 +126,7 @@ Divide an integer and round upwards
 @return Returns a divided by b
 */
 static INLINE OPJ_UINT32  opj_uint_ceildiv(OPJ_UINT32  a, OPJ_UINT32  b) {
+       assert(b);
        return (a + b - 1) / b;
 }
 
@@ -125,8 +135,25 @@ Divide an integer by a power of 2 and round upwards
 @return Returns a divided by 2^b
 */
 static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b) {
-       return (OPJ_INT32)((a + (OPJ_INT64)(1 << b) - 1) >> b);
+       return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
 }
+
+/**
+ Divide a 64bits integer by a power of 2 and round upwards
+ @return Returns a divided by 2^b
+ */
+static INLINE OPJ_INT32 opj_int64_ceildivpow2(OPJ_INT64 a, OPJ_INT32 b) {
+       return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
+}
+
+/**
+ Divide an integer by a power of 2 and round upwards
+ @return Returns a divided by 2^b
+ */
+static INLINE OPJ_UINT32 opj_uint_ceildivpow2(OPJ_UINT32 a, OPJ_UINT32 b) {
+       return (OPJ_UINT32)((a + ((OPJ_UINT64)1U << b) - 1U) >> b);
+}
+
 /**
 Divide an integer by a power of 2 and round downwards
 @return Returns a divided by 2^b
@@ -165,9 +192,27 @@ Multiply two fixed-precision rational numbers.
 @return Returns a * b
 */
 static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b) {
-    OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
-    temp += temp & 4096;
-    return (OPJ_INT32) (temp >> 13) ;
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
+       OPJ_INT64 temp = __emul(a, b);
+#else
+       OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
+#endif
+       temp += 4096;
+       assert((temp >> 13) <= (OPJ_INT64)0x7FFFFFFF);
+       assert((temp >> 13) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
+       return (OPJ_INT32) (temp >> 13);
+}
+
+static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b) {
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
+       OPJ_INT64 temp = __emul(a, b);
+#else
+       OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
+#endif
+       temp += 4096;
+       assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) <= (OPJ_INT64)0x7FFFFFFF);
+       assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
+       return (OPJ_INT32) (temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ;
 }
 
 /* ----------------------------------------------------------------------- */