Merge pull request #548 from mayeut/master
[openjpeg.git] / src / lib / openjp3d / int.h
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2001-2003, David Janssens
8  * Copyright (c) 2002-2003, Yannick Verschueren
9  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10  * Copyright (c) 2005, Herve Drolon, FreeImage Team
11  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 #ifndef __INT_H
36 #define __INT_H
37 /**
38 @file int.h
39 @brief Implementation of operations on integers (INT)
40
41 The functions in INT.H have for goal to realize operations on integers.
42 */
43
44 /** @defgroup INT INT - Implementation of operations on integers */
45 /*@{*/
46
47 /** @name Funciones generales (see also openjp3d.h) */
48 /*@{*/
49 /* ----------------------------------------------------------------------- */
50 /**
51 Get the minimum of two integers
52 @return Returns a if a < b else b
53 */
54 static  int int_min(int a, int b) {
55         return a < b ? a : b;
56 }
57 /**
58 Get the maximum of two integers
59 @return Returns a if a > b else b
60 */
61 static  int int_max(int a, int b) {
62         return (a > b) ? a : b;
63 }
64 /**
65 Clamp an integer inside an interval
66 @return
67 <ul>
68 <li>Returns a if (min < a < max)
69 <li>Returns max if (a > max)
70 <li>Returns min if (a < min) 
71 </ul>
72 */
73 static int int_clamp(int a, int min, int max) {
74         if (a < min)
75                 return min;
76         if (a > max)
77                 return max;
78         return a;
79 }
80 /**
81 @return Get absolute value of integer
82 */
83 static  int int_abs(int a) {
84         return a < 0 ? -a : a;
85 }
86
87 static double dbl_abs(double a) {
88         return a < 0 ? -a : a;
89 }
90 /**
91 Divide an integer and round upwards
92 @return Returns a divided by b
93 */
94 static  int int_ceildiv(int a, int b) {
95         return (a + b - 1) / b;
96 }
97 /**
98 Divide an integer by a power of 2 and round upwards
99 @return Returns a divided by 2^b
100 */
101 static  int int_ceildivpow2(int a, int b) {
102         return (a + (1 << b) - 1) >> b;
103 }
104 /**
105 Divide an integer by a power of 2 and round downwards
106 @return Returns a divided by 2^b
107 */
108 static  int int_floordivpow2(int a, int b) {
109         return a >> b;
110 }
111 /**
112 Get logarithm of an integer and round downwards
113 @return Returns log2(a)
114 */
115 static  int int_floorlog2(int a) {
116         int l;
117         for (l = 0; a > 1; l++) {
118                 a >>= 1;
119         }
120         return l;
121 }
122 /* ----------------------------------------------------------------------- */
123 /*@}*/
124
125 /*@}*/
126
127 #endif