Changeset 821 for soft/giet_vm/applications/rosenfeld/nrc2/src/nrarith0.c
- Timestamp:
- May 6, 2016, 3:06:29 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/rosenfeld/nrc2/src/nrarith0.c
r772 r821 22 22 #include <math.h> 23 23 24 #include "nrc_os_config.h" 24 25 #include "mypredef.h" 25 26 #include "nrtype.h" … … 30 31 #include "nrarith0.h" 31 32 32 ROUTINE(void) i8swap(int8 *a, int8 *b) { int8 *t; t=a; a=b; b=t;} 33 ROUTINE(void) i16swap(int16 *a, int16 *b) { int16 *t; t=a; a=b; b=t;} 34 ROUTINE(void) i32swap(int32 *a, int32 *b) { int32 *t; t=a; a=b; b=t;} 35 ROUTINE(void) i64swap(int64 *a, int64 *b) { int64 *t; t=a; a=b; b=t;} 36 ROUTINE(void) f32swap(float32 *a, float32 *b) { float32 *t; t=a; a=b; b=t;} 37 ROUTINE(void) f64swap(float64 *a, float64 *b) { float64 *t; t=a; a=b; b=t;} 38 ROUTINE(void) rgb8swap(rgb8 *a, rgb8 *b) { rgb8 *t; t=a; a=b; b=t;} 39 ROUTINE(void) rgbx8swap(rgbx8 *a, rgbx8 *b) { rgbx8 *t; t=a; a=b; b=t;} 33 #undef type_swap 34 #define type_swap(t) \ 35 void short_name(t,,swap)(t * a, t * b) \ 36 { \ 37 t c; \ 38 c = *a; \ 39 *a = *b; \ 40 *b = c; \ 41 } 42 43 type_swap(int8_t); 44 type_swap(int16_t); 45 type_swap(int32_t); 46 type_swap(int64_t); 47 type_swap(float); 48 type_swap(double); 49 type_swap(rgb8); 50 type_swap(rgbx8); 40 51 41 52 /* --------- */ … … 43 54 /* --------- */ 44 55 45 ROUTINE(float32) f32min (float32 x1, float32 x2) {if (x1<x2) return x1; else return x2;} 46 ROUTINE(float32) f32min2 (float32 x1, float32 x2) {if (x1<x2) return x1; else return x2;} 47 ROUTINE(float32) f32min3 (float32 x1, float32 x2, float32 x3) {return f32min2(f32min2(x1, x2), x3);} 48 ROUTINE(float32) f32min4 (float32 x1, float32 x2, float32 x3, float32 x4) {return f32min2(f32min2(x1, x2), f32min2(x3, x4));} 49 ROUTINE(float32) f32min5 (float32 x1, float32 x2, float32 x3, float32 x4, float32 x5) {return f32min3(f32min2(x1, x2), f32min2(x3, x4), x5);} 50 51 ROUTINE(float64) f64min (float64 x1, float64 x2) {if (x1<x2) return x1; else return x2;} 52 ROUTINE(float64) f64min2 (float64 x1, float64 x2) {if (x1<x2) return x1; else return x2;} 53 ROUTINE(float64) f64min3 (float64 x1, float64 x2, float64 x3) {return f64min2(f64min2(x1, x2), x3);} 54 ROUTINE(float64) f64min4 (float64 x1, float64 x2, float64 x3, float64 x4) {return f64min2(f64min2(x1, x2), f64min2(x3, x4));} 55 ROUTINE(float64) f64min5 (float64 x1, float64 x2, float64 x3, float64 x4, float64 x5) {return f64min3(f64min2(x1, x2), f64min2(x3, x4), x5);} 56 57 ROUTINE(uint8) ui8min (uint8 x1, uint8 x2) {if (x1<x2) return x1; else return x2;} 58 ROUTINE(uint8) ui8min2(uint8 x1, uint8 x2) {if (x1<x2) return x1; else return x2;} 59 ROUTINE(uint8) ui8min3(uint8 x1, uint8 x2, uint8 x3) {return ui8min2(ui8min2(x1, x2), x3);} 60 ROUTINE(uint8) ui8min4(uint8 x1, uint8 x2, uint8 x3, uint8 x4) {return ui8min2(ui8min2(x1, x2), ui8min2(x3, x4));} 61 ROUTINE(uint8) ui8min5(uint8 x1, uint8 x2, uint8 x3, uint8 x4, uint8 x5) {return ui8min3(ui8min2(x1, x2), ui8min2(x3, x4), x5);} 62 63 ROUTINE(uint16) ui16min (uint16 x1, uint16 x2) {if (x1<x2) return x1; else return x2;} 64 ROUTINE(uint16) ui16min2(uint16 x1, uint16 x2) {if (x1<x2) return x1; else return x2;} 65 ROUTINE(uint16) ui16min3(uint16 x1, uint16 x2, uint16 x3) {return ui16min2(ui16min2(x1, x2), x3);} 66 ROUTINE(uint16) ui16min4(uint16 x1, uint16 x2, uint16 x3, uint16 x4) {return ui16min2(ui16min2(x1, x2), ui16min2(x3, x4));} 67 ROUTINE(uint16) ui16min5(uint16 x1, uint16 x2, uint16 x3, uint16 x4, uint16 x5) {return ui16min3(ui16min2(x1, x2), ui16min2(x3, x4), x5);} 68 69 ROUTINE(int32) ui32min (uint32 x1, uint32 x2) {if (x1<x2) return x1; else return x2;} 70 ROUTINE(int32) ui32min2(uint32 x1, uint32 x2) {if (x1<x2) return x1; else return x2;} 71 ROUTINE(int32) ui32min3(uint32 x1, uint32 x2, uint32 x3) {return ui32min2(ui32min2(x1, x2), x3);} 72 ROUTINE(int32) ui32min4(uint32 x1, uint32 x2, uint32 x3, uint32 x4) {return ui32min2(ui32min2(x1, x2), ui32min2(x3, x4));} 73 ROUTINE(int32) ui32min5(uint32 x1, uint32 x2, uint32 x3, uint32 x4, uint32 x5) {return ui32min3(ui32min2(x1, x2), ui32min2(x3, x4), x5);} 74 75 ROUTINE(rgb8) 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;} 76 ROUTINE(rgb8) 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;} 77 ROUTINE(rgb8) rgb8min3(rgb8 x1, rgb8 x2, rgb8 x3) {return rgb8min2(rgb8min2(x1, x2), x3);} 78 ROUTINE(rgb8) rgb8min4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4) {return rgb8min2(rgb8min2(x1, x2), rgb8min2(x3,x4));} 79 ROUTINE(rgb8) rgb8min5(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4, rgb8 x5) {return rgb8min3(rgb8min2(x1, x2), rgb8min2(x3,x4), x5);} 56 #undef type_min 57 #define type_min(t) \ 58 t short_name(t,,min)(t x1, t x2) \ 59 { \ 60 return (x1 < x2) ? x1 : x2; \ 61 } \ 62 t short_name(t,,min2)(t x1, t x2) \ 63 { \ 64 return (x1 < x2) ? x1 : x2; \ 65 } \ 66 t 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 } \ 70 t 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 } \ 74 t 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 79 type_min(float); 80 type_min(double); 81 type_min(int8_t); 82 type_min(uint8_t); 83 type_min(int16_t); 84 type_min(uint16_t); 85 type_min(int32_t); 86 type_min(uint32_t); 87 88 rgb8 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;} 89 rgb8 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;} 90 rgb8 rgb8min3(rgb8 x1, rgb8 x2, rgb8 x3) {return rgb8min2(rgb8min2(x1, x2), x3);} 91 rgb8 rgb8min4(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4) {return rgb8min2(rgb8min2(x1, x2), rgb8min2(x3,x4));} 92 rgb8 rgb8min5(rgb8 x1, rgb8 x2, rgb8 x3, rgb8 x4, rgb8 x5) {return rgb8min3(rgb8min2(x1, x2), rgb8min2(x3,x4), x5);} 80 93 81 94 /* --------- */ … … 83 96 /* --------- */ 84 97 85 ROUTINE(float32) f32max (float32 x1, float32 x2) {if (x1>x2) return x1; else return x2;} 86 ROUTINE(float32) f32max2(float32 x1, float32 x2) {if (x1>x2) return x1; else return x2;} 87 ROUTINE(float32) f32max3(float32 x1, float32 x2, float32 x3) {return f32max2(f32max2(x1, x2), x3);} 88 ROUTINE(float32) f32max4(float32 x1, float32 x2, float32 x3, float32 x4) {return f32max2(f32max2(x1, x2), f32max2(x3, x4));} 89 ROUTINE(float32) f32max5(float32 x1, float32 x2, float32 x3, float32 x4, float32 x5) {return f32max3(f32max2(x1, x2), f32max2(x3, x4), x5);} 90 91 ROUTINE(float64) f64max (float64 x1, float64 x2) {if (x1>x2) return x1; else return x2;} 92 ROUTINE(float64) f64max2 (float64 x1, float64 x2) {if (x1>x2) return x1; else return x2;} 93 ROUTINE(float64) f64max3 (float64 x1, float64 x2, float64 x3) {return f64max2(f64max2(x1, x2), x3);} 94 ROUTINE(float64) f64max4 (float64 x1, float64 x2, float64 x3, float64 x4) {return f64max2(f64max2(x1, x2), f64max2(x3, x4));} 95 ROUTINE(float64) f64max5 (float64 x1, float64 x2, float64 x3, float64 x4, float64 x5) {return f64max3(f64max2(x1, x2), f64max2(x3, x4), x5);} 96 97 ROUTINE(uint8) ui8max (uint8 x1, uint8 x2) {if (x1>x2) return x1; else return x2;} 98 ROUTINE(uint8) ui8max2(uint8 x1, uint8 x2) {if (x1>x2) return x1; else return x2;} 99 ROUTINE(uint8) ui8max3(uint8 x1, uint8 x2, uint8 x3) {return ui8max2(ui8max2(x1, x2), x3);} 100 ROUTINE(uint8) ui8max4(uint8 x1, uint8 x2, uint8 x3, uint8 x4) {return ui8max2(ui8max2(x1, x2), ui8max2(x3, x4));} 101 ROUTINE(uint8) ui8max5(uint8 x1, uint8 x2, uint8 x3, uint8 x4, uint8 x5) {return ui8max3(ui8max2(x1, x2), ui8max2(x3, x4), x5);} 102 103 ROUTINE(uint16) ui16max (uint16 x1, uint16 x2) {if (x1>x2) return x1; else return x2;} 104 ROUTINE(uint16) ui16max2(uint16 x1, uint16 x2) {if (x1>x2) return x1; else return x2;} 105 ROUTINE(uint16) ui16max3(uint16 x1, uint16 x2, uint16 x3) {return ui16max2(ui16max2(x1, x2), x3);} 106 ROUTINE(uint16) ui16max4(uint16 x1, uint16 x2, uint16 x3, uint16 x4) {return ui16max2(ui16max2(x1, x2), ui16max2(x3, x4));} 107 ROUTINE(uint16) ui16max5(uint16 x1, uint16 x2, uint16 x3, uint16 x4, uint16 x5) {return ui16max3(ui16max2(x1, x2), ui16max2(x3, x4), x5);} 108 109 ROUTINE(int32) ui32max (uint32 x1, uint32 x2) {if (x1>x2) return x1; else return x2;} 110 ROUTINE(int32) ui32max2(uint32 x1, uint32 x2) {if (x1>x2) return x1; else return x2;} 111 ROUTINE(int32) ui32max3(uint32 x1, uint32 x2, uint32 x3) {return ui32max2(ui32max2(x1, x2), x3);} 112 ROUTINE(int32) ui32max4(uint32 x1, uint32 x2, uint32 x3, uint32 x4) {return ui32max2(ui32max2(x1, x2), ui32max2(x3, x4));} 113 ROUTINE(int32) ui32max5(uint32 x1, uint32 x2, uint32 x3, uint32 x4, int32 x5) {return ui32max3(ui32max2(x1, x2), ui32max2(x3, x4), x5);} 98 #undef type_max 99 #define type_max(t) \ 100 t short_name(t,,max)(t x1, t x2) \ 101 { \ 102 return (x1 > x2) ? x1 : x2; \ 103 } \ 104 t short_name(t,,max2)(t x1, t x2) \ 105 { \ 106 return (x1 > x2) ? x1 : x2; \ 107 } \ 108 t 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 } \ 112 t 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 } \ 116 t 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 121 type_max(float); 122 type_max(double); 123 type_max(int8_t); 124 type_max(uint8_t); 125 type_max(int16_t); 126 type_max(uint16_t); 127 type_max(int32_t); 128 type_max(uint32_t); 114 129 115 130 ROUTINE(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;} … … 124 139 125 140 /* ------------------------------- */ 126 ROUTINE(int32) i32bit(int32 x, int n)141 int32_t i32bit(int32_t x, int32_t n) 127 142 /* ------------------------------- */ 128 143 { 129 return ((x>>n)&1); 130 } 144 return ((x >> n) & 1); 145 } 146 131 147 /* --------------------------- */ 132 ROUTINE(int32) sym_int32(int32x)148 int32_t sym_int32(int32_t x) 133 149 /* --------------------------- */ 134 150 { 135 int i; 136 int32 y = 0; 137 for(i=0; i<31; i++) { 138 y = y | (x & 1); 139 x = x >> 1; 140 } 141 y = y | x; 142 return y; 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; 143 158 } 144 159 145 160 146 161 /* ----------------------- */ 147 ROUTINE(int) ilog2(int x)162 int32_t ilog2(int32_t x) 148 163 /* ----------------------- */ 149 164 { 150 int s = 0; 151 while(x) { 152 x >>= 1; 153 s++; 154 } 155 return s - 1; 156 } 165 int32_t s = 0; 166 while (x) { 167 x >>= 1; 168 s++; 169 } 170 return s - 1; 171 } 172 157 173 /* ----------------------------- */ 158 ROUTINE(int) next_power2(int x)174 int32_t next_power2(int32_t x) 159 175 /* ----------------------------- */ 160 176 { 161 int s = ilog2(x); 162 int n = 1 << s; 163 164 if(x != n) 165 return n << 1; 166 else 167 return n; 168 } 169 /* ---------------------------- */ 170 ROUTINE(int) gcd(int u, int v) 171 /* ---------------------------- */ 172 { 173 int r; 174 while(v) { 175 r = u % v; 176 u = v; 177 v = r; 178 } 179 return u; 180 } 181 /* ---------------------------- */ 182 ROUTINE(int) lcm(int u, int v) 183 /* ---------------------------- */ 184 { 185 return (u*v)/gcd(u,v); 186 } 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 /* ---------------------------- */ 189 int32_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 /* ---------------------------- */ 202 int32_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 TracChangeset
for help on using the changeset viewer.