OpenJPEG 2.1.2

opj_intmath.h

Go to the documentation of this file.
00001 /*
00002  * The copyright in this software is being made available under the 2-clauses 
00003  * BSD License, included below. This software may be subject to other third 
00004  * party and contributor rights, including patent rights, and no such rights
00005  * are granted under this license.
00006  *
00007  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
00008  * Copyright (c) 2002-2014, Professor Benoit Macq
00009  * Copyright (c) 2001-2003, David Janssens
00010  * Copyright (c) 2002-2003, Yannick Verschueren
00011  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
00012  * Copyright (c) 2003-2014, Antonin Descampe
00013  * Copyright (c) 2005, Herve Drolon, FreeImage Team
00014  * All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions
00018  * are met:
00019  * 1. Redistributions of source code must retain the above copyright
00020  *    notice, this list of conditions and the following disclaimer.
00021  * 2. Redistributions in binary form must reproduce the above copyright
00022  *    notice, this list of conditions and the following disclaimer in the
00023  *    documentation and/or other materials provided with the distribution.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  * POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 #ifndef __INT_H
00038 #define __INT_H
00039 
00048 
00051 /* ----------------------------------------------------------------------- */
00056 static INLINE OPJ_INT32 opj_int_min(OPJ_INT32 a, OPJ_INT32 b) {
00057         return a < b ? a : b;
00058 }
00059 
00064 static INLINE OPJ_UINT32 opj_uint_min(OPJ_UINT32 a, OPJ_UINT32 b) {
00065         return a < b ? a : b;
00066 }
00067 
00072 static INLINE OPJ_INT32 opj_int_max(OPJ_INT32 a, OPJ_INT32 b) {
00073         return (a > b) ? a : b;
00074 }
00075 
00080 static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32  a, OPJ_UINT32  b) {
00081         return (a > b) ? a : b;
00082 }
00083 
00088 static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b) {
00089         OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b;
00090         return (OPJ_UINT32)(-(OPJ_INT32)(sum >> 32)) | (OPJ_UINT32)sum;
00091 }
00092 
00102 static INLINE OPJ_INT32 opj_int_clamp(OPJ_INT32 a, OPJ_INT32 min, OPJ_INT32 max) {
00103         if (a < min)
00104                 return min;
00105         if (a > max)
00106                 return max;
00107         return a;
00108 }
00112 static INLINE OPJ_INT32 opj_int_abs(OPJ_INT32 a) {
00113         return a < 0 ? -a : a;
00114 }
00119 static INLINE OPJ_INT32 opj_int_ceildiv(OPJ_INT32 a, OPJ_INT32 b) {
00120         assert(b);
00121         return (a + b - 1) / b;
00122 }
00123 
00128 static INLINE OPJ_UINT32  opj_uint_ceildiv(OPJ_UINT32  a, OPJ_UINT32  b) {
00129         assert(b);
00130         return (a + b - 1) / b;
00131 }
00132 
00137 static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b) {
00138         return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
00139 }
00140 
00145 static INLINE OPJ_INT32 opj_int64_ceildivpow2(OPJ_INT64 a, OPJ_INT32 b) {
00146         return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
00147 }
00148 
00153 static INLINE OPJ_UINT32 opj_uint_ceildivpow2(OPJ_UINT32 a, OPJ_UINT32 b) {
00154         return (OPJ_UINT32)((a + ((OPJ_UINT64)1U << b) - 1U) >> b);
00155 }
00156 
00161 static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) {
00162         return a >> b;
00163 }
00168 static INLINE OPJ_INT32 opj_int_floorlog2(OPJ_INT32 a) {
00169         OPJ_INT32 l;
00170         for (l = 0; a > 1; l++) {
00171                 a >>= 1;
00172         }
00173         return l;
00174 }
00179 static INLINE OPJ_UINT32  opj_uint_floorlog2(OPJ_UINT32  a) {
00180         OPJ_UINT32  l;
00181         for (l = 0; a > 1; ++l)
00182         {
00183                 a >>= 1;
00184         }
00185         return l;
00186 }
00187 
00194 static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b) {
00195 #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
00196         OPJ_INT64 temp = __emul(a, b);
00197 #else
00198         OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
00199 #endif
00200         temp += 4096;
00201         assert((temp >> 13) <= (OPJ_INT64)0x7FFFFFFF);
00202         assert((temp >> 13) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
00203         return (OPJ_INT32) (temp >> 13);
00204 }
00205 
00206 static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b) {
00207 #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
00208         OPJ_INT64 temp = __emul(a, b);
00209 #else
00210         OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
00211 #endif
00212         temp += 4096;
00213         assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) <= (OPJ_INT64)0x7FFFFFFF);
00214         assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
00215         return (OPJ_INT32) (temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ;
00216 }
00217 
00218 /* ----------------------------------------------------------------------- */
00222 
00223 #endif