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
RevLine 
[772]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
[821]20#include "nrc_os_config.h"
[772]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
[821]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;                                                                     \
[772]46}
47
[821]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
[772]60/* ------------------ */
61/* --- max_matrix --- */
62/* ------------------ */
63
[821]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;                                                                     \
[772]77}
78
[821]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);
[772]89
[821]90
91
[772]92/* ------------------ */
93/* --- Add Matrix --- */
94/* ------------------ */
95
[821]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    }                                                                                                      \
[772]106}
[821]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
[772]120/* ----------------------------------------------------------------------------------------------- */
[821]121void add_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 ** Y, rgb8 ** Z)
[772]122/* ----------------------------------------------------------------------------------------------- */
123{
[821]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    }
[772]130}
[821]131
[772]132/* --------------------------------------------------------------------------------------------------- */
[821]133void add_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 ** Y, rgbx8 ** Z)
[772]134/* --------------------------------------------------------------------------------------------------- */
135{
[821]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    }
[772]142}
143
144/* -------------------- */
145/* --- Add constant --- */
146/* -------------------- */
147
[821]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    }                                                                                                    \
[772]157}
[821]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
[772]172/* ---------------------------------------------------------------------------------------------- */
[821]173void addc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
[772]174/* ---------------------------------------------------------------------------------------------- */
175{
[821]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    }
[772]181}
[821]182
[772]183/* -------------------------------------------------------------------------------------------------- */
[821]184void addc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
[772]185/* -------------------------------------------------------------------------------------------------- */
186{
[821]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    }
[772]192}
[821]193
194
[772]195/* ------------------ */
[821]196/* --- Sub Matrix --- */
[772]197/* ------------------ */
198
[821]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    }                                                                                                      \
[772]208}
[821]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
[772]222/* ----------------------------------------------------------------------------------------------- */
[821]223void sub_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 ** Y, rgb8 ** Z)
[772]224/* ----------------------------------------------------------------------------------------------- */
225{
[821]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    }
[772]232}
[821]233
[772]234/* --------------------------------------------------------------------------------------------------- */
[821]235void sub_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 ** Y, rgbx8 ** Z)
[772]236/* --------------------------------------------------------------------------------------------------- */
237{
[821]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    }
[772]244}
245
[821]246
[772]247/* -------------------- */
248/* --- Sub constant --- */
249/* -------------------- */
250
[821]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    }                                                                                                    \
[772]260}
[821]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
[772]275/* ---------------------------------------------------------------------------------------------- */
[821]276void subc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
[772]277/* ---------------------------------------------------------------------------------------------- */
278{
[821]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    }
[772]284}
[821]285
[772]286/* -------------------------------------------------------------------------------------------------- */
[821]287void subc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
[772]288/* -------------------------------------------------------------------------------------------------- */
289{
[821]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    }
[772]295}
[821]296
297
[772]298/* -------------------- */
299/* --- Mul constant --- */
300/* -------------------- */
301
[821]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    }                                                                                                    \
[772]311}
[821]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
[772]326/* ---------------------------------------------------------------------------------------------- */
[821]327void mulc_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb8 y, rgb8 ** Z)
[772]328/* ---------------------------------------------------------------------------------------------- */
329{
[821]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    }
[772]335}
[821]336
[772]337/* -------------------------------------------------------------------------------------------------- */
[821]338void mulc_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx8 y, rgbx8 ** Z)
[772]339/* -------------------------------------------------------------------------------------------------- */
340{
[821]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    }
[772]346}
[821]347
348
[772]349/* --------------- */
350/* --- MulFrac --- */
351/* --------------- */
352
[821]353// Y = (a * X) / b
[772]354
[821]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    }                                                                \
[772]365}
[821]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
[772]379/* ------------------------------------------------------------------------------------------------------------ */
[821]380void mulfrac_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb32 a, rgb32 b, rgb8 ** Y)
[772]381/* ------------------------------------------------------------------------------------------------------------ */
382{
[821]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}
[772]394
395/* ----------------------------------------------------------------------------------------------------------------- */
[821]396void mulfrac_rgb8xmatrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx32 a, rgbx32 b, rgbx8 ** Y)
[772]397/* ----------------------------------------------------------------------------------------------------------------- */
398{
[821]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    }
[772]409}
410
[821]411
[772]412/* ---------------- */
413/* --- MulShift --- */
414/* ---------------- */
415
[821]416// m3 = (a * m1) >> s
[772]417
[821]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    }                                                                \
[772]428}
[821]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
[772]440/* ---------------------------------------------------------------------------------------------------------------- */
[821]441void mulshift_rgb8matrix(rgb8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgb32  a, rgb32  s, rgb8 ** Y)
[772]442/* ---------------------------------------------------------------------------------------------------------------- */
443{
[821]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    }
[772]454}
[821]455
[772]456/* ----------------------------------------------------------------------------------------------------------------- */
[821]457void mulshift_rgbx8matrix(rgbx8 ** X, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, rgbx32 a, rgbx32 s, rgbx8 ** Y)
[772]458/* ----------------------------------------------------------------------------------------------------------------- */
459{
[821]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    }
[772]470}
[821]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.