1 | /* ------------------ */ |
---|
2 | /* --- nrarith0.c --- */ |
---|
3 | /* ------------------ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
---|
7 | * Univ Paris Sud XI, CNRS |
---|
8 | * |
---|
9 | * Distributed under the Boost Software License, Version 1.0 |
---|
10 | * see accompanying file LICENSE.txt or copy it at |
---|
11 | * http://www.boost.org/LICENSE_1_0.txt |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * History: |
---|
16 | * modif : log2 -> ilog2 (conflict with math.h on Mac OSX) |
---|
17 | */ |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stddef.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <math.h> |
---|
23 | |
---|
24 | #include "nrc_os_config.h" |
---|
25 | #include "mypredef.h" |
---|
26 | #include "nrtype.h" |
---|
27 | #include "nrdef.h" |
---|
28 | #include "nrmacro.h" |
---|
29 | #include "nrkernel.h" |
---|
30 | |
---|
31 | #include "nrarith0.h" |
---|
32 | |
---|
33 | #undef type_swap |
---|
34 | #define type_swap(t) \ |
---|
35 | void short_name(t,,swap)(t * a, t * b) \ |
---|
36 | { \ |
---|
37 | t c; \ |
---|
38 | c = *a; \ |
---|
39 | *a = *b; \ |
---|
40 | *b = c; \ |
---|
41 | } |
---|
42 | |
---|
43 | type_swap(int8_t); |
---|
44 | type_swap(int16_t); |
---|
45 | type_swap(int32_t); |
---|
46 | type_swap(int64_t); |
---|
47 | type_swap(float); |
---|
48 | type_swap(double); |
---|
49 | type_swap(rgb8); |
---|
50 | type_swap(rgbx8); |
---|
51 | |
---|
52 | /* --------- */ |
---|
53 | /* -- Min -- */ |
---|
54 | /* --------- */ |
---|
55 | |
---|
56 | #undef type_min |
---|
57 | #define type_min(t) \ |
---|
58 | t short_name(t,,min)(t x1, t x2) \ |
---|
59 | { \ |
---|
60 | return (x1 < x2) ? x1 : x2; \ |
---|
61 | } \ |
---|
62 | t short_name(t,,min2)(t x1, t x2) \ |
---|
63 | { \ |
---|
64 | return (x1 < x2) ? x1 : x2; \ |
---|
65 | } \ |
---|
66 | t short_name(t,,min3)(t x1, t x2, t x3) \ |
---|
67 | { \ |
---|
68 | return short_name(t,,min2)(short_name(t,,min2)(x1, x2), x3); \ |
---|
69 | } \ |
---|
70 | t short_name(t,,min4)(t x1, t x2, t x3, t x4) \ |
---|
71 | { \ |
---|
72 | return short_name(t,,min2)(short_name(t,,min2)(x1, x2), short_name(t,,min2)(x3, x4)); \ |
---|
73 | } \ |
---|
74 | t short_name(t,,min5)(t x1, t x2, t x3, t x4, t x5) \ |
---|
75 | { \ |
---|
76 | return short_name(t,,min3)(short_name(t,,min2)(x1, x2), short_name(t,,min2)(x3, x4), x5); \ |
---|
77 | } |
---|
78 | |
---|
79 | type_min(float); |
---|
80 | type_min(double); |
---|
81 | type_min(int8_t); |
---|
82 | type_min(uint8_t); |
---|
83 | type_min(int16_t); |
---|
84 | type_min(uint16_t); |
---|
85 | type_min(int32_t); |
---|
86 | type_min(uint32_t); |
---|
87 | |
---|
88 | rgb8 rgb8min (rgb8 x1, rgb8 x2) {rgb8 y; y.r = ui8min2(x1.r,x2.r);y.g=ui8min2(x1.g,x2.g);y.b=ui8min2(x1.b,x2.b);return y;} |
---|
89 | rgb8 rgb8min2(rgb8 x1, rgb8 x2) {rgb8 y; y.r = ui8min2(x1.r,x2.r);y.g=ui8min2(x1.g,x2.g);y.b=ui8min2(x1.b,x2.b);return y;} |
---|
90 | rgb8 rgb8min3(rgb8 x1, rgb8 x2, rgb8 x3) {return rgb8min2(rgb8min2(x1, x2), x3);} |
---|
91 | rgb8 rgb8min4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4) {return rgb8min2(rgb8min2(x1, x2), rgb8min2(x3,x4));} |
---|
92 | rgb8 rgb8min5(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4, rgb8 x5) {return rgb8min3(rgb8min2(x1, x2), rgb8min2(x3,x4), x5);} |
---|
93 | |
---|
94 | /* --------- */ |
---|
95 | /* -- Max -- */ |
---|
96 | /* --------- */ |
---|
97 | |
---|
98 | #undef type_max |
---|
99 | #define type_max(t) \ |
---|
100 | t short_name(t,,max)(t x1, t x2) \ |
---|
101 | { \ |
---|
102 | return (x1 > x2) ? x1 : x2; \ |
---|
103 | } \ |
---|
104 | t short_name(t,,max2)(t x1, t x2) \ |
---|
105 | { \ |
---|
106 | return (x1 > x2) ? x1 : x2; \ |
---|
107 | } \ |
---|
108 | t short_name(t,,max3)(t x1, t x2, t x3) \ |
---|
109 | { \ |
---|
110 | return short_name(t,,max2)(short_name(t,,max2)(x1, x2), x3); \ |
---|
111 | } \ |
---|
112 | t short_name(t,,max4)(t x1, t x2, t x3, t x4) \ |
---|
113 | { \ |
---|
114 | return short_name(t,,max2)(short_name(t,,max2)(x1, x2), short_name(t,,max2)(x3, x4)); \ |
---|
115 | } \ |
---|
116 | t short_name(t,,max5)(t x1, t x2, t x3, t x4, t x5) \ |
---|
117 | { \ |
---|
118 | return short_name(t,,max3)(short_name(t,,max2)(x1, x2), short_name(t,,max2)(x3, x4), x5); \ |
---|
119 | } |
---|
120 | |
---|
121 | type_max(float); |
---|
122 | type_max(double); |
---|
123 | type_max(int8_t); |
---|
124 | type_max(uint8_t); |
---|
125 | type_max(int16_t); |
---|
126 | type_max(uint16_t); |
---|
127 | type_max(int32_t); |
---|
128 | type_max(uint32_t); |
---|
129 | |
---|
130 | ROUTINE(rgb8) rgb8max (rgb8 x1, rgb8 x2) {rgb8 y; y.r = ui8max2(x1.r,x2.r);y.g=ui8max2(x1.g,x2.g);y.b=ui8max2(x1.b,x2.b);return y;} |
---|
131 | ROUTINE(rgb8) rgb8max2(rgb8 x1, rgb8 x2) {rgb8 y; y.r = ui8max2(x1.r,x2.r);y.g=ui8max2(x1.g,x2.g);y.b=ui8max2(x1.b,x2.b);return y;} |
---|
132 | ROUTINE(rgb8) rgb8max3(rgb8 x1, rgb8 x2, rgb8 x3) {return rgb8max2(rgb8max2(x1, x2), x3);} |
---|
133 | ROUTINE(rgb8) rgb8max4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4) {return rgb8max2(rgb8max2(x1, x2), rgb8max2(x3,x4));} |
---|
134 | ROUTINE(rgb8) rgb8max5(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4, rgb8 x5) {return rgb8max3(rgb8max2(x1, x2), rgb8max2(x3,x4), x5);} |
---|
135 | |
---|
136 | /* ----------- */ |
---|
137 | /* -- Other -- */ |
---|
138 | /* ----------- */ |
---|
139 | |
---|
140 | /* ------------------------------- */ |
---|
141 | int32_t i32bit(int32_t x, int32_t n) |
---|
142 | /* ------------------------------- */ |
---|
143 | { |
---|
144 | return ((x >> n) & 1); |
---|
145 | } |
---|
146 | |
---|
147 | /* --------------------------- */ |
---|
148 | int32_t sym_int32(int32_t x) |
---|
149 | /* --------------------------- */ |
---|
150 | { |
---|
151 | int32_t y = 0; |
---|
152 | for (int32_t i = 0; i < 31; i++) { |
---|
153 | y = y | (x & 1); |
---|
154 | x = x >> 1; |
---|
155 | } |
---|
156 | y = y | x; |
---|
157 | return y; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /* ----------------------- */ |
---|
162 | int32_t ilog2(int32_t x) |
---|
163 | /* ----------------------- */ |
---|
164 | { |
---|
165 | int32_t s = 0; |
---|
166 | while (x) { |
---|
167 | x >>= 1; |
---|
168 | s++; |
---|
169 | } |
---|
170 | return s - 1; |
---|
171 | } |
---|
172 | |
---|
173 | /* ----------------------------- */ |
---|
174 | int32_t next_power2(int32_t x) |
---|
175 | /* ----------------------------- */ |
---|
176 | { |
---|
177 | int32_t s = ilog2(x); |
---|
178 | int32_t n = 1 << s; |
---|
179 | |
---|
180 | if (x != n) { |
---|
181 | return n << 1; |
---|
182 | } |
---|
183 | else { |
---|
184 | return n; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /* ---------------------------- */ |
---|
189 | int32_t myGCD(int32_t u, int32_t v) |
---|
190 | /* ---------------------------- */ |
---|
191 | { |
---|
192 | int32_t r; |
---|
193 | while (v != 0) { |
---|
194 | r = u % v; |
---|
195 | u = v; |
---|
196 | v = r; |
---|
197 | } |
---|
198 | return u; |
---|
199 | } |
---|
200 | |
---|
201 | /* ---------------------------- */ |
---|
202 | int32_t myLCM(int32_t u, int32_t v) |
---|
203 | /* ---------------------------- */ |
---|
204 | { |
---|
205 | return (u * v) / myGCD(u, v); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | // Local Variables: |
---|
210 | // tab-width: 4 |
---|
211 | // c-basic-offset: 4 |
---|
212 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
213 | // indent-tabs-mode: nil |
---|
214 | // End: |
---|
215 | |
---|
216 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
217 | |
---|