source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrarith0.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: 6.7 KB
Line 
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)                   \
35void short_name(t,,swap)(t * a, t * b) \
36{                                      \
37    t c;                               \
38    c = *a;                            \
39    *a = *b;                           \
40    *b = c;                            \
41}
42
43type_swap(int8_t);
44type_swap(int16_t);
45type_swap(int32_t);
46type_swap(int64_t);
47type_swap(float);
48type_swap(double);
49type_swap(rgb8);
50type_swap(rgbx8);
51
52/* --------- */
53/* -- Min -- */
54/* --------- */
55
56#undef type_min
57#define type_min(t)               \
58t short_name(t,,min)(t x1, t x2)  \
59{                                 \
60    return (x1 < x2) ? x1 : x2;   \
61}                                 \
62t short_name(t,,min2)(t x1, t x2) \
63{                                 \
64    return (x1 < x2) ? x1 : x2;   \
65}                                 \
66t 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}                                                                \
70t 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}                                                                                             \
74t 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
79type_min(float);
80type_min(double);
81type_min(int8_t);
82type_min(uint8_t);
83type_min(int16_t);
84type_min(uint16_t);
85type_min(int32_t);
86type_min(uint32_t);
87
88rgb8 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;}
89rgb8 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;}
90rgb8 rgb8min3(rgb8 x1, rgb8 x2, rgb8 x3)                   {return rgb8min2(rgb8min2(x1, x2), x3);}
91rgb8 rgb8min4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4)          {return rgb8min2(rgb8min2(x1, x2), rgb8min2(x3,x4));}
92rgb8 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)               \
100t short_name(t,,max)(t x1, t x2)  \
101{                                 \
102    return (x1 > x2) ? x1 : x2;   \
103}                                 \
104t short_name(t,,max2)(t x1, t x2) \
105{                                 \
106    return (x1 > x2) ? x1 : x2;   \
107}                                 \
108t 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}                                                                \
112t 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}                                                                                             \
116t 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
121type_max(float);
122type_max(double);
123type_max(int8_t);
124type_max(uint8_t);
125type_max(int16_t);
126type_max(uint16_t);
127type_max(int32_t);
128type_max(uint32_t);
129
130ROUTINE(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;}
131ROUTINE(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;}
132ROUTINE(rgb8) rgb8max3(rgb8 x1, rgb8 x2, rgb8 x3)                   {return rgb8max2(rgb8max2(x1, x2), x3);}
133ROUTINE(rgb8) rgb8max4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4)          {return rgb8max2(rgb8max2(x1, x2), rgb8max2(x3,x4));}
134ROUTINE(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/* ------------------------------- */
141int32_t i32bit(int32_t x, int32_t n)
142/* ------------------------------- */
143{
144    return ((x >> n) & 1);
145}
146
147/* --------------------------- */
148int32_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/* ----------------------- */
162int32_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/* ----------------------------- */
174int32_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/* ---------------------------- */
189int32_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/* ---------------------------- */
202int32_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
Note: See TracBrowser for help on using the repository browser.