source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrarith1.c

Last change on this file 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: 14.0 KB
Line 
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)                                     \
40r 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
49sum_type_vector(int8_t, int32_t);
50sum_type_vector(uint8_t, uint32_t);
51sum_type_vector(int16_t, int32_t);
52sum_type_vector(uint16_t, uint32_t);
53sum_type_vector(int32_t, int32_t);
54sum_type_vector(uint32_t, uint32_t);
55sum_type_vector(float, float);
56sum_type_vector(double, double);
57
58/*
59 * ------------------
60 * --- min_vector ---
61 * ------------------
62 */
63
64#undef min_type_vector
65#define min_type_vector(t)                                 \
66t 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
78min_type_vector(int8_t);
79min_type_vector(uint8_t);
80min_type_vector(int16_t);
81min_type_vector(uint16_t);
82min_type_vector(int32_t);
83min_type_vector(uint32_t);
84min_type_vector(float);
85min_type_vector(double);
86
87
88/*
89 * ------------------
90 * --- max_vector ---
91 * ------------------
92 */
93
94#undef max_type_vector
95#define max_type_vector(t)                                 \
96t 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
108max_type_vector(int8_t);
109max_type_vector(uint8_t);
110max_type_vector(int16_t);
111max_type_vector(uint16_t);
112max_type_vector(int32_t);
113max_type_vector(uint32_t);
114max_type_vector(float);
115max_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)                                                \
127t 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
142min_type_vector_pos(int8_t);
143min_type_vector_pos(uint8_t);
144min_type_vector_pos(int16_t);
145min_type_vector_pos(uint16_t);
146min_type_vector_pos(int32_t);
147min_type_vector_pos(uint32_t);
148min_type_vector_pos(float);
149min_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)                                                \
161t 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
176max_type_vector_pos(int8_t);
177max_type_vector_pos(uint8_t);
178max_type_vector_pos(int16_t);
179max_type_vector_pos(uint16_t);
180max_type_vector_pos(int32_t);
181max_type_vector_pos(uint32_t);
182max_type_vector_pos(float);
183max_type_vector_pos(double);
184
185
186#undef add_type_vector
187#define add_type_vector(t) \
188void 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
195add_type_vector(int8_t);
196add_type_vector(uint8_t);
197add_type_vector(int16_t);
198add_type_vector(uint16_t);
199add_type_vector(int32_t);
200add_type_vector(uint32_t);
201add_type_vector(float);
202add_type_vector(double);
203
204
205#undef sub_type_vector
206#define sub_type_vector(t) \
207void 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
214sub_type_vector(int8_t);
215sub_type_vector(uint8_t);
216sub_type_vector(int16_t);
217sub_type_vector(uint16_t);
218sub_type_vector(int32_t);
219sub_type_vector(uint32_t);
220sub_type_vector(float);
221sub_type_vector(double);
222
223
224#undef mulc_type_vector
225#define mulc_type_vector(t) \
226void 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
233mulc_type_vector(int8_t);
234mulc_type_vector(uint8_t);
235mulc_type_vector(int16_t);
236mulc_type_vector(uint16_t);
237mulc_type_vector(int32_t);
238mulc_type_vector(uint32_t);
239mulc_type_vector(float);
240mulc_type_vector(double);
241
242
243#undef divc_type_vector
244#define divc_type_vector(t) \
245void 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
252divc_type_vector(int8_t);
253divc_type_vector(uint8_t);
254divc_type_vector(int16_t);
255divc_type_vector(uint16_t);
256divc_type_vector(int32_t);
257divc_type_vector(uint32_t);
258divc_type_vector(float);
259divc_type_vector(double);
260
261
262#undef cumulleft_type_vector
263#define cumulleft_type_vector(t) \
264void 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
271cumulleft_type_vector(int8_t);
272cumulleft_type_vector(uint8_t);
273cumulleft_type_vector(int16_t);
274cumulleft_type_vector(uint16_t);
275cumulleft_type_vector(int32_t);
276cumulleft_type_vector(uint32_t);
277cumulleft_type_vector(float);
278cumulleft_type_vector(double);
279
280
281#undef cumulright_type_vector
282#define cumulright_type_vector(t) \
283void 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
290cumulright_type_vector(int8_t);
291cumulright_type_vector(uint8_t);
292cumulright_type_vector(int16_t);
293cumulright_type_vector(uint16_t);
294cumulright_type_vector(int32_t);
295cumulright_type_vector(uint32_t);
296cumulright_type_vector(float);
297cumulright_type_vector(double);
298
299
300#undef mulfrac_type_vector
301#define mulfrac_type_vector(t) \
302void 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
310mulfrac_type_vector(int8_t);
311mulfrac_type_vector(uint8_t);
312mulfrac_type_vector(int16_t);
313mulfrac_type_vector(uint16_t);
314mulfrac_type_vector(int32_t);
315mulfrac_type_vector(uint32_t);
316mulfrac_type_vector(float);
317mulfrac_type_vector(double);
318
319
320/* --------------------------------------------------------------------- */
321void 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/* ----------------------------------------------------------------------- */
338void 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/* ------------------------------------------------------------------------ */
355void 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/* --------------------------------------------------------------------- */
366void 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/* --------------------------------------------------------------------- */
377void 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/* --------------------------------------------------------------------- */
390void 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/* ------------------------------------------------------------------------------------- */
403void 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
Note: See TracBrowser for help on using the repository browser.