| [772] | 1 | /* --------------- */ |
|---|
| 2 | /* --- tools.c --- */ |
|---|
| 3 | /* --------------- */ |
|---|
| 4 | |
|---|
| 5 | // Copyright (c) 2013-2014 Lionel Lacassagne, All Rights Reserved |
|---|
| 6 | // Laboratoire de Recherche en Informatique |
|---|
| 7 | // Universite Paris-Sud / CNRS |
|---|
| 8 | |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | #include <stdlib.h> |
|---|
| 11 | #include <stddef.h> |
|---|
| 12 | #include <math.h> |
|---|
| 13 | |
|---|
| 14 | #ifdef CLI |
|---|
| 15 | #include "nrc_os_config.h" |
|---|
| 16 | #include "nrc.h" |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #include "ecc_features.h" |
|---|
| 20 | |
|---|
| 21 | #ifndef FREQ |
|---|
| 22 | #define FREQ 1 |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | /* ---------------- */ |
|---|
| 26 | /* --- routines --- */ |
|---|
| 27 | /* ---------------- */ |
|---|
| 28 | |
|---|
| 29 | // --------------------- |
|---|
| 30 | uint32 i32log2(uint32 x) |
|---|
| 31 | // --------------------- |
|---|
| 32 | { |
|---|
| 33 | int p = 0; // power of 2 |
|---|
| 34 | while (x > 0) { |
|---|
| 35 | x = x >> 1; |
|---|
| 36 | p = p + 1; |
|---|
| 37 | } |
|---|
| 38 | return p - 1; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /* ------------- */ |
|---|
| 42 | uint8 ui8rand(void) |
|---|
| 43 | /* ------------- */ |
|---|
| 44 | { |
|---|
| 45 | static uint32 x = 0; |
|---|
| 46 | x = (11 * x + 13) % 17; |
|---|
| 47 | return x; |
|---|
| 48 | } |
|---|
| 49 | /* --------------- */ |
|---|
| 50 | uint32 ui32rand(void) |
|---|
| 51 | /* --------------- */ |
|---|
| 52 | { |
|---|
| 53 | static uint32 x = 0; |
|---|
| 54 | x = (11 * x + 13) % 17; |
|---|
| 55 | return x; |
|---|
| 56 | } |
|---|
| 57 | /* --------------- */ |
|---|
| 58 | float32 f32rand(void) |
|---|
| 59 | /* --------------- */ |
|---|
| 60 | { |
|---|
| 61 | static float32 x = 0; |
|---|
| 62 | x = (float32) fmod(11 * x + 13, 17); |
|---|
| 63 | return x ; |
|---|
| 64 | } |
|---|
| 65 | /* --------------------------------------- */ |
|---|
| 66 | void rand_ui8vector(uint8 * X, int i0, int i1) |
|---|
| 67 | /* --------------------------------------- */ |
|---|
| 68 | { |
|---|
| 69 | int i; |
|---|
| 70 | |
|---|
| 71 | for(i = i0; i <= i1; i++) { |
|---|
| 72 | X[i] = ui8rand(); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | /* ----------------------------------------- */ |
|---|
| 76 | void rand_ui32vector(uint32 *X, int i0, int i1) |
|---|
| 77 | /* ----------------------------------------- */ |
|---|
| 78 | { |
|---|
| 79 | int i; |
|---|
| 80 | |
|---|
| 81 | for (i = i0; i <= i1; i++) { |
|---|
| 82 | X[i] = ui32rand(); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | /* ----------------------------------------- */ |
|---|
| 86 | void rand_f32vector(float32 *X, int i0, int i1) |
|---|
| 87 | /* ----------------------------------------- */ |
|---|
| 88 | { |
|---|
| 89 | int i; |
|---|
| 90 | |
|---|
| 91 | for(i = i0; i <= i1; i++) { |
|---|
| 92 | X[i] = f32rand(); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | /* --------------- */ |
|---|
| 96 | int getIter(int size) |
|---|
| 97 | /* --------------- */ |
|---|
| 98 | { |
|---|
| 99 | if (size < 32) return 1024; |
|---|
| 100 | if (size < 64) return 64 ; |
|---|
| 101 | if (size < 128) return 8; |
|---|
| 102 | if (size < 256) return 2; |
|---|
| 103 | if (size < 512) return 2; |
|---|
| 104 | if (size < 1024) return 2; |
|---|
| 105 | if (size < 2048) return 1; |
|---|
| 106 | return 2; |
|---|
| 107 | } |
|---|
| 108 | /* ----------------- */ |
|---|
| 109 | int getIterAV(int size) |
|---|
| 110 | /* ----------------- */ |
|---|
| 111 | { |
|---|
| 112 | return 3 * getIter(size); |
|---|
| 113 | } |
|---|
| 114 | /* --------------------------------- */ |
|---|
| 115 | float32 gauss(float32 sigma, float32 x) |
|---|
| 116 | /* --------------------------------- */ |
|---|
| 117 | { |
|---|
| 118 | float64 pi = 3.1415927; |
|---|
| 119 | float64 y; |
|---|
| 120 | y = 1.0 / (sqrt(2 * pi) * sigma) * exp(-(x * x) / (2 * sigma * sigma)); |
|---|
| 121 | return (float32) y; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /* --------------------- */ |
|---|
| 125 | double cpp(double t, int n) |
|---|
| 126 | /* --------------------- */ |
|---|
| 127 | { |
|---|
| 128 | double c = n; |
|---|
| 129 | double cpp = t * FREQ / c; |
|---|
| 130 | return cpp; |
|---|
| 131 | } |
|---|
| 132 | // -------------------------- |
|---|
| 133 | void printf_split12(double x) |
|---|
| 134 | // -------------------------- |
|---|
| 135 | { |
|---|
| 136 | double x3, x2, x1, x0, t = x; |
|---|
| 137 | |
|---|
| 138 | x0 = fmod(x, 1000); |
|---|
| 139 | x = floor(x / 1000); |
|---|
| 140 | x1 = fmod(x, 1000); |
|---|
| 141 | x = floor(x / 1000); |
|---|
| 142 | x2 = fmod(x, 1000); |
|---|
| 143 | x = floor(x / 1000); |
|---|
| 144 | x3 = fmod(x, 1000); |
|---|
| 145 | //printf("t = %14f\n", t); |
|---|
| 146 | if (t < 1e3) { |
|---|
| 147 | printf("%15.0f", x0); |
|---|
| 148 | return; |
|---|
| 149 | } |
|---|
| 150 | if (t < 1e6) { |
|---|
| 151 | printf("%11.0f,%03.0f", x1, x0); |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| 154 | if (t < 1e9) { |
|---|
| 155 | printf("%7.0f,%03.0f,%03.0f", x2, x1, x0); |
|---|
| 156 | return; |
|---|
| 157 | } |
|---|
| 158 | if (t < 1e12) { |
|---|
| 159 | printf("%3.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
|---|
| 160 | return; |
|---|
| 161 | } |
|---|
| 162 | printf(""); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // -------------------------- |
|---|
| 166 | void printf_split15(double x) |
|---|
| 167 | // -------------------------- |
|---|
| 168 | { |
|---|
| 169 | double x4, x3, x2, x1, x0, t = x; |
|---|
| 170 | |
|---|
| 171 | x0 = fmod(x, 1000); |
|---|
| 172 | x = floor(x / 1000); |
|---|
| 173 | x1 = fmod(x, 1000); |
|---|
| 174 | x = floor(x / 1000); |
|---|
| 175 | x2 = fmod(x, 1000); |
|---|
| 176 | x = floor(x / 1000); |
|---|
| 177 | x3 = fmod(x, 1000); |
|---|
| 178 | x = floor(x / 1000); |
|---|
| 179 | x4 = fmod(x, 1000); |
|---|
| 180 | //printf("t = %14f\n", t); |
|---|
| 181 | if (t < 1e3) { |
|---|
| 182 | printf("%19.0f", x0); |
|---|
| 183 | return; |
|---|
| 184 | } |
|---|
| 185 | if (t < 1e6) { |
|---|
| 186 | printf("%15.0f,%03.0f", x1, x0); |
|---|
| 187 | return; |
|---|
| 188 | } |
|---|
| 189 | if (t < 1e9) { |
|---|
| 190 | printf("%11.0f,%03.0f,%03.0f", x2, x1, x0); |
|---|
| 191 | return; |
|---|
| 192 | } |
|---|
| 193 | if (t < 1e12) { |
|---|
| 194 | printf("%7.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
|---|
| 195 | return; |
|---|
| 196 | } |
|---|
| 197 | if (t < 1e15) { |
|---|
| 198 | printf("%3.0f,%3.0f,%03.0f,%03.0f,%03.0f", x4,x3, x2, x1, x0); |
|---|
| 199 | return; |
|---|
| 200 | } |
|---|
| 201 | printf(""); |
|---|
| 202 | } |
|---|
| 203 | // -------------------------- |
|---|
| 204 | void printf_split18(double x) |
|---|
| 205 | // -------------------------- |
|---|
| 206 | { |
|---|
| 207 | double x5, x4, x3, x2, x1, x0, t = x; |
|---|
| 208 | |
|---|
| 209 | x0 = fmod(x, 1000); |
|---|
| 210 | x = floor(x / 1000); |
|---|
| 211 | x1 = fmod(x, 1000); |
|---|
| 212 | x = floor(x / 1000); |
|---|
| 213 | x2 = fmod(x, 1000); |
|---|
| 214 | x = floor(x / 1000); |
|---|
| 215 | x3 = fmod(x, 1000); |
|---|
| 216 | x = floor(x / 1000); |
|---|
| 217 | x4 = fmod(x, 1000); |
|---|
| 218 | x = floor(x / 1000); |
|---|
| 219 | x5 = fmod(x, 1000); |
|---|
| 220 | //printf("t = %14f\n", t); |
|---|
| 221 | if (t < 1e3) { |
|---|
| 222 | printf("%23.0f", x0); |
|---|
| 223 | return; |
|---|
| 224 | } |
|---|
| 225 | if (t < 1e6) { |
|---|
| 226 | printf("%19.0f,%03.0f", x1, x0); |
|---|
| 227 | return; |
|---|
| 228 | } |
|---|
| 229 | if (t < 1e9) { |
|---|
| 230 | printf("%15.0f,%03.0f,%03.0f", x2, x1, x0); |
|---|
| 231 | return; |
|---|
| 232 | } |
|---|
| 233 | if (t < 1e12) { |
|---|
| 234 | printf("%11.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
|---|
| 235 | return; |
|---|
| 236 | } |
|---|
| 237 | if (t < 1e15) { |
|---|
| 238 | printf("%7.0f,%3.0f,%03.0f,%03.0f,%03.0f", x4, x3, x2, x1, x0); |
|---|
| 239 | return; |
|---|
| 240 | } |
|---|
| 241 | if (t < 1e18) { |
|---|
| 242 | printf("%3.0f,%3.0f,%3.0f,%03.0f,%03.0f,%03.0f", x5, x4, x3, x2, x1, x0); |
|---|
| 243 | return; |
|---|
| 244 | } |
|---|
| 245 | printf(""); |
|---|
| 246 | } |
|---|
| 247 | // ------------------------ |
|---|
| 248 | void printf_split(double x) |
|---|
| 249 | // ------------------------ |
|---|
| 250 | { |
|---|
| 251 | printf_split12(x); |
|---|
| 252 | //printf_split15(x); |
|---|
| 253 | //printf_split18(x); |
|---|
| 254 | } |
|---|
| 255 | |
|---|