1 | /* ------------------ */ |
---|
2 | /* --- nralloc1.c --- */ |
---|
3 | /* ------------------ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
---|
7 | * Univ Paris Sud XI, CNRS |
---|
8 | * Distributed under the Boost Software License, Version 1.0 |
---|
9 | * see accompanying file LICENSE.txt or copy it at |
---|
10 | * http://www.boost.org/LICENSE_1_0.txt |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * History: |
---|
15 | *2002/06/11 ajout des fonctions endline |
---|
16 | */ |
---|
17 | #include <stdio.h> |
---|
18 | #include <stddef.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <malloc.h> |
---|
21 | #include <math.h> // fabs |
---|
22 | |
---|
23 | #include "nrc_os_config.h" |
---|
24 | #include "mypredef.h" |
---|
25 | #include "nrtype.h" |
---|
26 | #include "nrdef.h" |
---|
27 | #include "nrmacro.h" |
---|
28 | #include "nrkernel.h" |
---|
29 | |
---|
30 | #include "nrarith1.h" |
---|
31 | |
---|
32 | /* |
---|
33 | * ------------------ |
---|
34 | * --- sum_vector --- |
---|
35 | * ------------------ |
---|
36 | */ |
---|
37 | |
---|
38 | #undef sum_type_vector |
---|
39 | #define sum_type_vector(t,r) \ |
---|
40 | r short_name(t,sum_,vector)(t * v, int32_t nl, int32_t nh) \ |
---|
41 | { \ |
---|
42 | r s = 0; \ |
---|
43 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
44 | s += v[i]; \ |
---|
45 | } \ |
---|
46 | return s; \ |
---|
47 | } |
---|
48 | |
---|
49 | sum_type_vector(int8_t, int32_t); |
---|
50 | sum_type_vector(uint8_t, uint32_t); |
---|
51 | sum_type_vector(int16_t, int32_t); |
---|
52 | sum_type_vector(uint16_t, uint32_t); |
---|
53 | sum_type_vector(int32_t, int32_t); |
---|
54 | sum_type_vector(uint32_t, uint32_t); |
---|
55 | sum_type_vector(float, float); |
---|
56 | sum_type_vector(double, double); |
---|
57 | |
---|
58 | /* |
---|
59 | * ------------------ |
---|
60 | * --- min_vector --- |
---|
61 | * ------------------ |
---|
62 | */ |
---|
63 | |
---|
64 | #undef min_type_vector |
---|
65 | #define min_type_vector(t) \ |
---|
66 | t short_name(t,min_,vector)(t * v, int32_t nl, int32_t nh) \ |
---|
67 | { \ |
---|
68 | t m = v[nl]; \ |
---|
69 | for (int32_t i = nl + 1; i <= nh; i++) { \ |
---|
70 | if (v[i] < m) { \ |
---|
71 | m = v[i]; \ |
---|
72 | } \ |
---|
73 | } \ |
---|
74 | return m; \ |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | min_type_vector(int8_t); |
---|
79 | min_type_vector(uint8_t); |
---|
80 | min_type_vector(int16_t); |
---|
81 | min_type_vector(uint16_t); |
---|
82 | min_type_vector(int32_t); |
---|
83 | min_type_vector(uint32_t); |
---|
84 | min_type_vector(float); |
---|
85 | min_type_vector(double); |
---|
86 | |
---|
87 | |
---|
88 | /* |
---|
89 | * ------------------ |
---|
90 | * --- max_vector --- |
---|
91 | * ------------------ |
---|
92 | */ |
---|
93 | |
---|
94 | #undef max_type_vector |
---|
95 | #define max_type_vector(t) \ |
---|
96 | t short_name(t,max_,vector)(t * v, int32_t nl, int32_t nh) \ |
---|
97 | { \ |
---|
98 | t m = v[nl]; \ |
---|
99 | for (int32_t i = nl + 1; i <= nh; i++) { \ |
---|
100 | if (v[i] > m) { \ |
---|
101 | m = v[i]; \ |
---|
102 | } \ |
---|
103 | } \ |
---|
104 | return m; \ |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | max_type_vector(int8_t); |
---|
109 | max_type_vector(uint8_t); |
---|
110 | max_type_vector(int16_t); |
---|
111 | max_type_vector(uint16_t); |
---|
112 | max_type_vector(int32_t); |
---|
113 | max_type_vector(uint32_t); |
---|
114 | max_type_vector(float); |
---|
115 | max_type_vector(double); |
---|
116 | |
---|
117 | |
---|
118 | /* |
---|
119 | * ---------------------- |
---|
120 | * --- min_vector_pos --- |
---|
121 | * ---------------------- |
---|
122 | */ |
---|
123 | |
---|
124 | |
---|
125 | #undef min_type_vector_pos |
---|
126 | #define min_type_vector_pos(t) \ |
---|
127 | t short_name(t,min_,vector_pos)(t * v, int32_t nl, int32_t nh, int32_t * pos) \ |
---|
128 | { \ |
---|
129 | t m = v[nl]; \ |
---|
130 | int32_t p = nl; \ |
---|
131 | for (int32_t i = nl + 1; i <= nh; i++) { \ |
---|
132 | if (v[i] < m) { \ |
---|
133 | m = v[i]; \ |
---|
134 | p = i; \ |
---|
135 | } \ |
---|
136 | } \ |
---|
137 | *pos = p; \ |
---|
138 | return m; \ |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | min_type_vector_pos(int8_t); |
---|
143 | min_type_vector_pos(uint8_t); |
---|
144 | min_type_vector_pos(int16_t); |
---|
145 | min_type_vector_pos(uint16_t); |
---|
146 | min_type_vector_pos(int32_t); |
---|
147 | min_type_vector_pos(uint32_t); |
---|
148 | min_type_vector_pos(float); |
---|
149 | min_type_vector_pos(double); |
---|
150 | |
---|
151 | |
---|
152 | /* |
---|
153 | * ---------------------- |
---|
154 | * --- max_vector_pos --- |
---|
155 | * ---------------------- |
---|
156 | */ |
---|
157 | |
---|
158 | |
---|
159 | #undef max_type_vector_pos |
---|
160 | #define max_type_vector_pos(t) \ |
---|
161 | t short_name(t,max_,vector_pos)(t * v, int32_t nl, int32_t nh, int32_t * pos) \ |
---|
162 | { \ |
---|
163 | t m = v[nl]; \ |
---|
164 | int32_t p = nl; \ |
---|
165 | for (int32_t i = nl + 1; i <= nh; i++) { \ |
---|
166 | if (v[i] > m) { \ |
---|
167 | m = v[i]; \ |
---|
168 | p = i; \ |
---|
169 | } \ |
---|
170 | } \ |
---|
171 | *pos = p; \ |
---|
172 | return m; \ |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | max_type_vector_pos(int8_t); |
---|
177 | max_type_vector_pos(uint8_t); |
---|
178 | max_type_vector_pos(int16_t); |
---|
179 | max_type_vector_pos(uint16_t); |
---|
180 | max_type_vector_pos(int32_t); |
---|
181 | max_type_vector_pos(uint32_t); |
---|
182 | max_type_vector_pos(float); |
---|
183 | max_type_vector_pos(double); |
---|
184 | |
---|
185 | |
---|
186 | #undef add_type_vector |
---|
187 | #define add_type_vector(t) \ |
---|
188 | void short_name(t,add_,vector)(t * S1, int32_t nl, int32_t nh, t * S2, t * D) \ |
---|
189 | { \ |
---|
190 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
191 | D[i] = S1[i] + S2[i]; \ |
---|
192 | } \ |
---|
193 | } |
---|
194 | |
---|
195 | add_type_vector(int8_t); |
---|
196 | add_type_vector(uint8_t); |
---|
197 | add_type_vector(int16_t); |
---|
198 | add_type_vector(uint16_t); |
---|
199 | add_type_vector(int32_t); |
---|
200 | add_type_vector(uint32_t); |
---|
201 | add_type_vector(float); |
---|
202 | add_type_vector(double); |
---|
203 | |
---|
204 | |
---|
205 | #undef sub_type_vector |
---|
206 | #define sub_type_vector(t) \ |
---|
207 | void short_name(t,sub_,vector)(t * S1, int32_t nl, int32_t nh, t * S2, t * D) \ |
---|
208 | { \ |
---|
209 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
210 | D[i] = S1[i] - S2[i]; \ |
---|
211 | } \ |
---|
212 | } |
---|
213 | |
---|
214 | sub_type_vector(int8_t); |
---|
215 | sub_type_vector(uint8_t); |
---|
216 | sub_type_vector(int16_t); |
---|
217 | sub_type_vector(uint16_t); |
---|
218 | sub_type_vector(int32_t); |
---|
219 | sub_type_vector(uint32_t); |
---|
220 | sub_type_vector(float); |
---|
221 | sub_type_vector(double); |
---|
222 | |
---|
223 | |
---|
224 | #undef mulc_type_vector |
---|
225 | #define mulc_type_vector(t) \ |
---|
226 | void short_name(t,mulc_,vector)(t * S, int32_t nl, int32_t nh, int32_t c, t * D) \ |
---|
227 | { \ |
---|
228 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
229 | D[i] = S[i] * c; \ |
---|
230 | } \ |
---|
231 | } |
---|
232 | |
---|
233 | mulc_type_vector(int8_t); |
---|
234 | mulc_type_vector(uint8_t); |
---|
235 | mulc_type_vector(int16_t); |
---|
236 | mulc_type_vector(uint16_t); |
---|
237 | mulc_type_vector(int32_t); |
---|
238 | mulc_type_vector(uint32_t); |
---|
239 | mulc_type_vector(float); |
---|
240 | mulc_type_vector(double); |
---|
241 | |
---|
242 | |
---|
243 | #undef divc_type_vector |
---|
244 | #define divc_type_vector(t) \ |
---|
245 | void short_name(t,divc_,vector)(t * S, int32_t nl, int32_t nh, int32_t c, t * D) \ |
---|
246 | { \ |
---|
247 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
248 | D[i] = S[i] / c; \ |
---|
249 | } \ |
---|
250 | } |
---|
251 | |
---|
252 | divc_type_vector(int8_t); |
---|
253 | divc_type_vector(uint8_t); |
---|
254 | divc_type_vector(int16_t); |
---|
255 | divc_type_vector(uint16_t); |
---|
256 | divc_type_vector(int32_t); |
---|
257 | divc_type_vector(uint32_t); |
---|
258 | divc_type_vector(float); |
---|
259 | divc_type_vector(double); |
---|
260 | |
---|
261 | |
---|
262 | #undef cumulleft_type_vector |
---|
263 | #define cumulleft_type_vector(t) \ |
---|
264 | void short_name(t,cumulleft_,vector)(t * S, int32_t nl, int32_t nh, int32_t * D) \ |
---|
265 | { \ |
---|
266 | for (int32_t i = nh - 1; i >= nl; i--) { \ |
---|
267 | D[i] += S[i + 1]; \ |
---|
268 | } \ |
---|
269 | } |
---|
270 | |
---|
271 | cumulleft_type_vector(int8_t); |
---|
272 | cumulleft_type_vector(uint8_t); |
---|
273 | cumulleft_type_vector(int16_t); |
---|
274 | cumulleft_type_vector(uint16_t); |
---|
275 | cumulleft_type_vector(int32_t); |
---|
276 | cumulleft_type_vector(uint32_t); |
---|
277 | cumulleft_type_vector(float); |
---|
278 | cumulleft_type_vector(double); |
---|
279 | |
---|
280 | |
---|
281 | #undef cumulright_type_vector |
---|
282 | #define cumulright_type_vector(t) \ |
---|
283 | void short_name(t,cumulright_,vector)(t * S, int32_t nl, int32_t nh, int32_t * D) \ |
---|
284 | { \ |
---|
285 | for (int32_t i = nl + 1; i <= nh; i++) { \ |
---|
286 | D[i] += S[i - 1]; \ |
---|
287 | } \ |
---|
288 | } |
---|
289 | |
---|
290 | cumulright_type_vector(int8_t); |
---|
291 | cumulright_type_vector(uint8_t); |
---|
292 | cumulright_type_vector(int16_t); |
---|
293 | cumulright_type_vector(uint16_t); |
---|
294 | cumulright_type_vector(int32_t); |
---|
295 | cumulright_type_vector(uint32_t); |
---|
296 | cumulright_type_vector(float); |
---|
297 | cumulright_type_vector(double); |
---|
298 | |
---|
299 | |
---|
300 | #undef mulfrac_type_vector |
---|
301 | #define mulfrac_type_vector(t) \ |
---|
302 | void short_name(t,mulfrac_,vector)(t * S, int32_t nl, int32_t nh, int32_t a, int32_t b, t * D) \ |
---|
303 | { \ |
---|
304 | for (int32_t i = nl; i <= nh; i++) { \ |
---|
305 | D[i] = (a * S[i]) / b; \ |
---|
306 | } \ |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | mulfrac_type_vector(int8_t); |
---|
311 | mulfrac_type_vector(uint8_t); |
---|
312 | mulfrac_type_vector(int16_t); |
---|
313 | mulfrac_type_vector(uint16_t); |
---|
314 | mulfrac_type_vector(int32_t); |
---|
315 | mulfrac_type_vector(uint32_t); |
---|
316 | mulfrac_type_vector(float); |
---|
317 | mulfrac_type_vector(double); |
---|
318 | |
---|
319 | |
---|
320 | /* --------------------------------------------------------------------- */ |
---|
321 | void beta_sum_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, rgb32 * D) |
---|
322 | /* --------------------------------------------------------------------- */ |
---|
323 | { |
---|
324 | int32_t r, g, b; |
---|
325 | int32_t s; |
---|
326 | for (int32_t i = nl; i <= nh; i++) { |
---|
327 | r = S[i].r; |
---|
328 | g = S[i].g; |
---|
329 | b = S[i].b; |
---|
330 | s = r + g + b; |
---|
331 | D[i].r = s; |
---|
332 | D[i].g = s; |
---|
333 | D[i].b = s; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | /* ----------------------------------------------------------------------- */ |
---|
338 | void beta_average_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, rgb32 * D) |
---|
339 | /* ----------------------------------------------------------------------- */ |
---|
340 | { |
---|
341 | int32_t r, g, b; |
---|
342 | int32_t s; |
---|
343 | for (int32_t i = nl; i <= nh; i++) { |
---|
344 | r = S[i].r; |
---|
345 | g = S[i].g; |
---|
346 | b = S[i].b; |
---|
347 | s = (r + g + b) / 3; |
---|
348 | D[i].r = s; |
---|
349 | D[i].g = s; |
---|
350 | D[i].b = s; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | /* ------------------------------------------------------------------------ */ |
---|
355 | void mulc_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, int32_t c, rgb32 * D) |
---|
356 | /* ------------------------------------------------------------------------ */ |
---|
357 | { |
---|
358 | for (int32_t i = nl; i <= nh; i++) { |
---|
359 | D[i].r = c * S[i].r; |
---|
360 | D[i].g = c * S[i].g; |
---|
361 | D[i].b = c * S[i].b; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | /* --------------------------------------------------------------------- */ |
---|
366 | void divc_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, int32_t c, rgb32 * D) |
---|
367 | /* --------------------------------------------------------------------- */ |
---|
368 | { |
---|
369 | for (int32_t i = nl; i <= nh; i++) { |
---|
370 | D[i].r = S[i].r / c; |
---|
371 | D[i].g = S[i].g / c; |
---|
372 | D[i].b = S[i].b / c; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | /* --------------------------------------------------------------------- */ |
---|
377 | void cumulleft_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, rgb32 * D) |
---|
378 | /* --------------------------------------------------------------------- */ |
---|
379 | { |
---|
380 | // for histogram |
---|
381 | for (int32_t i = nh - 1; i >= nl; i--) { |
---|
382 | D[i].r += S[i + 1].r; |
---|
383 | D[i].g += S[i + 1].g; |
---|
384 | D[i].b += S[i + 1].b; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | /* --------------------------------------------------------------------- */ |
---|
390 | void cumulright_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, rgb32 * D) |
---|
391 | /* --------------------------------------------------------------------- */ |
---|
392 | { |
---|
393 | // for histogram |
---|
394 | for (int32_t i = nl + 1; i <= nh; i++) { |
---|
395 | D[i].r += S[i - 1].r; |
---|
396 | D[i].g += S[i - 1].g; |
---|
397 | D[i].b += S[i - 1].b; |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | /* ------------------------------------------------------------------------------------- */ |
---|
403 | void mulfrac_rgb32vector(rgb32 * S, int32_t nl, int32_t nh, int32_t a, int32_t b, rgb32 * D) |
---|
404 | /* ------------------------------------------------------------------------------------- */ |
---|
405 | { |
---|
406 | for (int32_t i = nl; i <= nh; i++) { |
---|
407 | D[i].r = (a * S[i].r) / b; |
---|
408 | D[i].g = (a * S[i].g) / b; |
---|
409 | D[i].b = (a * S[i].b) / b; |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | // Local Variables: |
---|
414 | // tab-width: 4 |
---|
415 | // c-basic-offset: 4 |
---|
416 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
417 | // indent-tabs-mode: nil |
---|
418 | // End: |
---|
419 | |
---|
420 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
421 | |
---|