source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrarith2.c @ 821

Last change on this file since 821 was 821, checked in by meunier, 8 years ago
  • Added several versions of rosenfeld: { SLOW, FAST } x { FEATURES, NO_FEATURES }
  • Added native linux compilation support
  • Added a script to check results natively
  • Started to refactor nrc code
File size: 18.2 KB
Line 
1/* ------------------ */
2/* --- nrarith2.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#include <stdio.h>
15#include <stddef.h>
16#include <stdlib.h>
17#include <malloc.h>
18#include <math.h> // fabs
19
20#include "nrc_os_config.h"
21#include "mypredef.h"
22#include "nrtype.h"
23#include "nrdef.h"
24#include "nrmacro.h"
25#include "nrkernel.h"
26
27#include "nrarith2.h"
28
29/* ------------------ */
30/* --- min_matrix --- */
31/* ------------------ */
32
33#undef min_type_matrix
34#define min_type_matrix(t) \
35t short_name(t,min_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \
36{                                                                                       \
37    t minimum = m[nrl][nrh];                                                            \
38    for (int32_t i = nrl; i <= nrh; i++) {                                              \
39        for (int32_t j = ncl; j <= nch; j++) {                                          \
40            if (m[i][j] < minimum) {                                                    \
41                minimum = m[i][j];                                                      \
42            }                                                                           \
43        }                                                                               \
44    }                                                                                   \
45    return minimum;                                                                     \
46}
47
48min_type_matrix(int8_t);
49min_type_matrix(uint8_t);
50min_type_matrix(int16_t);
51min_type_matrix(uint16_t);
52min_type_matrix(int32_t);
53min_type_matrix(uint32_t);
54min_type_matrix(int64_t);
55min_type_matrix(uint64_t);
56min_type_matrix(float);
57min_type_matrix(double);
58
59
60/* ------------------ */
61/* --- max_matrix --- */
62/* ------------------ */
63
64#undef max_type_matrix
65#define max_type_matrix(t) \
66t short_name(t,max_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \
67{                                                                                       \
68    t maximum = m[nrl][nrh];                                                            \
69    for (int32_t i = nrl; i <= nrh; i++) {                                              \
70        for (int32_t j = ncl; j <= nch; j++) {                                          \
71            if (m[i][j] > maximum) {                                                    \
72                maximum = m[i][j];                                                      \
73            }                                                                           \
74        }                                                                               \
75    }                                                                                   \
76    return maximum;                                                                     \
77}
78
79max_type_matrix(int8_t);
80max_type_matrix(uint8_t);
81max_type_matrix(int16_t);
82max_type_matrix(uint16_t);
83max_type_matrix(int32_t);
84max_type_matrix(uint32_t);
85max_type_matrix(int64_t);
86max_type_matrix(uint64_t);
87max_type_matrix(float);
88max_type_matrix(double);
89
90
91
92/* ------------------ */
93/* --- Add Matrix --- */
94/* ------------------ */
95
96
97#undef add_type_matrix
98#define add_type_matrix(t) \
99void short_name(t,add_,matrix)(t ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, t ** Y, t ** Z) \
100{                                                                                                          \
101    for (int32_t i = nrl; i <= nrh; i++) {                                                                 \
102        for (int32_t j = ncl; j <= nch; j++) {                                                             \
103            Z[i][j] = X[i][j] + Y[i][j];                                                                   \
104        }                                                                                                  \
105    }                                                                                                      \
106}
107
108add_type_matrix(int8_t);
109add_type_matrix(uint8_t);
110add_type_matrix(int16_t);
111add_type_matrix(uint16_t);
112add_type_matrix(int32_t);
113add_type_matrix(uint32_t);
114add_type_matrix(int64_t);
115add_type_matrix(uint64_t);
116add_type_matrix(float);
117add_type_matrix(double);
118
119
120/* ----------------------------------------------------------------------------------------------- */
121void add_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 ** Y, rgb8 ** Z)
122/* ----------------------------------------------------------------------------------------------- */
123{
124    for (int32_t i = nrl; i <= nrh; i++) {
125        for (int32_t j = ncl; j <= nch; j++) {
126            //z = x + y;
127            RGB8_ADD(X[i][j], Y[i][j], Z[i][j]);
128        }
129    }
130}
131
132/* --------------------------------------------------------------------------------------------------- */
133void add_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 ** Y, rgbx8 ** Z)
134/* --------------------------------------------------------------------------------------------------- */
135{
136    for (int32_t i = nrl; i <= nrh; i++) {
137        for (int32_t j = ncl; j <= nch; j++) {
138            //z = x + y;
139            RGBX8_ADD(X[i][j], Y[i][j], Z[i][j]);
140        }
141    }
142}
143
144/* -------------------- */
145/* --- Add constant --- */
146/* -------------------- */
147
148#undef addc_type_matrix
149#define addc_type_matrix(t) \
150void short_name(t,addc_,matrix)(t ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, t y, t ** Z) \
151{                                                                                                        \
152    for (int32_t i = nrl; i <= nrh; i++) {                                                               \
153        for (int32_t j = ncl; j <= nch; j++) {                                                           \
154            Z[i][j] = X[i][j] + y;                                                                       \
155        }                                                                                                \
156    }                                                                                                    \
157}
158
159addc_type_matrix(int8_t);
160addc_type_matrix(uint8_t);
161addc_type_matrix(int16_t);
162addc_type_matrix(uint16_t);
163addc_type_matrix(int32_t);
164addc_type_matrix(uint32_t);
165addc_type_matrix(int64_t);
166addc_type_matrix(uint64_t);
167addc_type_matrix(float);
168addc_type_matrix(double);
169
170
171
172/* ---------------------------------------------------------------------------------------------- */
173void addc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
174/* ---------------------------------------------------------------------------------------------- */
175{
176    for (int32_t i = nrl; i <= nrh; i++) {
177        for (int32_t j = ncl; j <= nch; j++) {
178            RGB8_ADD(X[i][j], y, Z[i][j]);
179        }
180    }
181}
182
183/* -------------------------------------------------------------------------------------------------- */
184void addc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
185/* -------------------------------------------------------------------------------------------------- */
186{
187    for (int32_t i = nrl; i <= nrh; i++) {
188        for (int32_t j = ncl; j <= nch; j++) {
189            RGBX8_ADD(X[i][j], y, Z[i][j]);
190        }
191    }
192}
193
194
195/* ------------------ */
196/* --- Sub Matrix --- */
197/* ------------------ */
198
199#undef sub_type_matrix
200#define sub_type_matrix(t) \
201void short_name(t,sub_,matrix)(t ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, t ** Y, t ** Z) \
202{                                                                                                          \
203    for (int32_t i = nrl; i <= nrh; i++) {                                                                 \
204        for (int32_t j = ncl; j <= nch; j++) {                                                             \
205            Z[i][j] = X[i][j] - Y[i][j];                                                                   \
206        }                                                                                                  \
207    }                                                                                                      \
208}
209
210sub_type_matrix(int8_t);
211sub_type_matrix(uint8_t);
212sub_type_matrix(int16_t);
213sub_type_matrix(uint16_t);
214sub_type_matrix(int32_t);
215sub_type_matrix(uint32_t);
216sub_type_matrix(int64_t);
217sub_type_matrix(uint64_t);
218sub_type_matrix(float);
219sub_type_matrix(double);
220
221
222/* ----------------------------------------------------------------------------------------------- */
223void sub_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 ** Y, rgb8 ** Z)
224/* ----------------------------------------------------------------------------------------------- */
225{
226    for (int32_t i = nrl; i <= nrh; i++) {
227        for (int32_t j = ncl; j <= nch; j++) {
228            //z = x + y;
229            RGB8_SUB(X[i][j], Y[i][j], Z[i][j]);
230        }
231    }
232}
233
234/* --------------------------------------------------------------------------------------------------- */
235void sub_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 ** Y, rgbx8 ** Z)
236/* --------------------------------------------------------------------------------------------------- */
237{
238    for (int32_t i = nrl; i <= nrh; i++) {
239        for (int32_t j = ncl; j <= nch; j++) {
240            //z = x + y;
241            RGBX8_SUB(X[i][j], Y[i][j], Z[i][j]);
242        }
243    }
244}
245
246
247/* -------------------- */
248/* --- Sub constant --- */
249/* -------------------- */
250
251#undef subc_type_matrix
252#define subc_type_matrix(t) \
253void short_name(t,subc_,matrix)(t ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, t y, t ** Z) \
254{                                                                                                        \
255    for (int32_t i = nrl; i <= nrh; i++) {                                                               \
256        for (int32_t j = ncl; j <= nch; j++) {                                                           \
257            Z[i][j] = X[i][j] - y;                                                                       \
258        }                                                                                                \
259    }                                                                                                    \
260}
261
262subc_type_matrix(int8_t);
263subc_type_matrix(uint8_t);
264subc_type_matrix(int16_t);
265subc_type_matrix(uint16_t);
266subc_type_matrix(int32_t);
267subc_type_matrix(uint32_t);
268subc_type_matrix(int64_t);
269subc_type_matrix(uint64_t);
270subc_type_matrix(float);
271subc_type_matrix(double);
272
273
274
275/* ---------------------------------------------------------------------------------------------- */
276void subc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
277/* ---------------------------------------------------------------------------------------------- */
278{
279    for (int32_t i = nrl; i <= nrh; i++) {
280        for (int32_t j = ncl; j <= nch; j++) {
281            RGB8_SUB(X[i][j], y, Z[i][j]);
282        }
283    }
284}
285
286/* -------------------------------------------------------------------------------------------------- */
287void subc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
288/* -------------------------------------------------------------------------------------------------- */
289{
290    for (int32_t i = nrl; i <= nrh; i++) {
291        for (int32_t j = ncl; j <= nch; j++) {
292            RGBX8_SUB(X[i][j], y, Z[i][j]);
293        }
294    }
295}
296
297
298/* -------------------- */
299/* --- Mul constant --- */
300/* -------------------- */
301
302#undef mulc_type_matrix
303#define mulc_type_matrix(t) \
304void short_name(t,mulc_,matrix)(t ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, t y, t ** Z) \
305{                                                                                                        \
306    for (int32_t i = nrl; i <= nrh; i++) {                                                               \
307        for (int32_t j = ncl; j <= nch; j++) {                                                           \
308            Z[i][j] = X[i][j] * y;                                                                       \
309        }                                                                                                \
310    }                                                                                                    \
311}
312
313mulc_type_matrix(int8_t);
314mulc_type_matrix(uint8_t);
315mulc_type_matrix(int16_t);
316mulc_type_matrix(uint16_t);
317mulc_type_matrix(int32_t);
318mulc_type_matrix(uint32_t);
319mulc_type_matrix(int64_t);
320mulc_type_matrix(uint64_t);
321mulc_type_matrix(float);
322mulc_type_matrix(double);
323
324
325
326/* ---------------------------------------------------------------------------------------------- */
327void mulc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
328/* ---------------------------------------------------------------------------------------------- */
329{
330    for (int32_t i = nrl; i <= nrh; i++) {
331        for (int32_t j = ncl; j <= nch; j++) {
332            RGB8_MUL(X[i][j], y, Z[i][j]);
333        }
334    }
335}
336
337/* -------------------------------------------------------------------------------------------------- */
338void mulc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
339/* -------------------------------------------------------------------------------------------------- */
340{
341    for (int32_t i = nrl; i <= nrh; i++) {
342        for (int32_t j = ncl; j <= nch; j++) {
343            RGBX8_MUL(X[i][j], y, Z[i][j]);
344        }
345    }
346}
347
348
349/* --------------- */
350/* --- MulFrac --- */
351/* --------------- */
352
353// Y = (a * X) / b
354
355#undef mulfrac_type_matrix
356#define mulfrac_type_matrix(t) \
357void short_name(t,mulfrac_,matrix)(t ** X, int32_t nrl, int32_t nrh, \
358        int32_t ncl, int32_t nch, int32_t a, int32_t b, t ** Y)      \
359{                                                                    \
360    for (int32_t i = nrl; i <= nrh; i++) {                           \
361        for (int32_t j = ncl; j <= nch; j++) {                       \
362            Y[i][j] = (t) ((a * X[i][j]) / b);                       \
363        }                                                            \
364    }                                                                \
365}
366
367mulfrac_type_matrix(int8_t);
368mulfrac_type_matrix(uint8_t);
369mulfrac_type_matrix(int16_t);
370mulfrac_type_matrix(uint16_t);
371mulfrac_type_matrix(int32_t);
372mulfrac_type_matrix(uint32_t);
373mulfrac_type_matrix(int64_t);
374mulfrac_type_matrix(uint64_t);
375mulfrac_type_matrix(float);
376mulfrac_type_matrix(double);
377
378
379/* ------------------------------------------------------------------------------------------------------------ */
380void mulfrac_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb32 a, rgb32 b, rgb8 ** Y)
381/* ------------------------------------------------------------------------------------------------------------ */
382{
383    rgb32 y32;
384    rgb8 x8, y8;
385    for (int32_t i = nrl; i <= nrh; i++) {
386        for (int32_t j = ncl; j <= nch; j++) {
387            x8 = X[i][j];
388            RGB8_MULFRAC(x8, a, b, y32);
389            RGB32CAST8(y32, y8);
390            Y[i][j] = y8;
391        }
392    }
393}
394
395/* ----------------------------------------------------------------------------------------------------------------- */
396void mulfrac_rgb8xmatrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx32 a, rgbx32 b, rgbx8 ** Y)
397/* ----------------------------------------------------------------------------------------------------------------- */
398{
399    rgbx32 y32;
400    rgbx8 x8, y8;
401    for (int32_t i = nrl; i <= nrh; i++) {
402        for (int32_t j = ncl; j <= nch; j++) {
403            x8 = X[i][j];
404            RGBX8_MULFRAC(x8, a, b, y32);
405            RGBX32CAST8(y32, y8);
406            Y[i][j] = y8;
407        }
408    }
409}
410
411
412/* ---------------- */
413/* --- MulShift --- */
414/* ---------------- */
415
416// m3 = (a * m1) >> s
417
418#undef mulshift_type_matrix
419#define mulshift_type_matrix(t) \
420void short_name(t,mulshift_,matrix)(t ** X, int32_t nrl, int32_t nrh, \
421        int32_t ncl, int32_t nch, int32_t a, int32_t s, t ** Y)      \
422{                                                                    \
423    for (int32_t i = nrl; i <= nrh; i++) {                           \
424        for (int32_t j = ncl; j <= nch; j++) {                       \
425            Y[i][j] = (t) ((a * X[i][j]) >> s);                      \
426        }                                                            \
427    }                                                                \
428}
429
430mulshift_type_matrix(int8_t);
431mulshift_type_matrix(uint8_t);
432mulshift_type_matrix(int16_t);
433mulshift_type_matrix(uint16_t);
434mulshift_type_matrix(int32_t);
435mulshift_type_matrix(uint32_t);
436mulshift_type_matrix(int64_t);
437mulshift_type_matrix(uint64_t);
438
439
440/* ---------------------------------------------------------------------------------------------------------------- */
441void mulshift_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb32  a, rgb32  s, rgb8 ** Y)
442/* ---------------------------------------------------------------------------------------------------------------- */
443{
444    rgb32 y32;
445    rgb8 x8, y8;
446    for (int32_t i = nrl; i <= nrh; i++) {
447        for (int32_t j = ncl; j <= nch; j++) {
448            x8 = X[i][j];
449            RGB8_MULSHIFT(x8, a, s, y32);
450            RGB32CAST8(y32, y8);
451            Y[i][j] = y8;
452        }
453    }
454}
455
456/* ----------------------------------------------------------------------------------------------------------------- */
457void mulshift_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx32 a, rgbx32 s, rgbx8 ** Y)
458/* ----------------------------------------------------------------------------------------------------------------- */
459{
460    rgbx32 y32;
461    rgbx8 x8, y8;
462    for (int32_t i = nrl; i <= nrh; i++) {
463        for (int32_t j = ncl; j <= nch; j++) {
464            x8 = X[i][j];
465            RGBX8_MULSHIFT(x8, a, s, y32);
466            RGBX32CAST8(y32, y8);
467            Y[i][j] = y8;
468        }
469    }
470}
471
472// Local Variables:
473// tab-width: 4
474// c-basic-offset: 4
475// c-file-offsets:((innamespace . 0)(inline-open . 0))
476// indent-tabs-mode: nil
477// End:
478
479// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
480
Note: See TracBrowser for help on using the repository browser.