[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 | |
---|
[821] | 41 | |
---|
[772] | 42 | /* ------------- */ |
---|
| 43 | uint8 ui8rand(void) |
---|
| 44 | /* ------------- */ |
---|
| 45 | { |
---|
| 46 | static uint32 x = 0; |
---|
| 47 | x = (11 * x + 13) % 17; |
---|
| 48 | return x; |
---|
| 49 | } |
---|
[821] | 50 | |
---|
| 51 | |
---|
[772] | 52 | /* --------------- */ |
---|
| 53 | uint32 ui32rand(void) |
---|
| 54 | /* --------------- */ |
---|
| 55 | { |
---|
| 56 | static uint32 x = 0; |
---|
| 57 | x = (11 * x + 13) % 17; |
---|
| 58 | return x; |
---|
| 59 | } |
---|
[821] | 60 | |
---|
| 61 | |
---|
[772] | 62 | /* --------------- */ |
---|
| 63 | float32 f32rand(void) |
---|
| 64 | /* --------------- */ |
---|
| 65 | { |
---|
| 66 | static float32 x = 0; |
---|
| 67 | x = (float32) fmod(11 * x + 13, 17); |
---|
| 68 | return x ; |
---|
| 69 | } |
---|
[821] | 70 | |
---|
| 71 | |
---|
[772] | 72 | /* --------------------------------------- */ |
---|
| 73 | void rand_ui8vector(uint8 * X, int i0, int i1) |
---|
| 74 | /* --------------------------------------- */ |
---|
| 75 | { |
---|
[821] | 76 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 77 | X[i] = ui8rand(); |
---|
| 78 | } |
---|
| 79 | } |
---|
[821] | 80 | |
---|
| 81 | |
---|
[772] | 82 | /* ----------------------------------------- */ |
---|
| 83 | void rand_ui32vector(uint32 *X, int i0, int i1) |
---|
| 84 | /* ----------------------------------------- */ |
---|
| 85 | { |
---|
[821] | 86 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 87 | X[i] = ui32rand(); |
---|
| 88 | } |
---|
| 89 | } |
---|
[821] | 90 | |
---|
| 91 | |
---|
[772] | 92 | /* ----------------------------------------- */ |
---|
| 93 | void rand_f32vector(float32 *X, int i0, int i1) |
---|
| 94 | /* ----------------------------------------- */ |
---|
| 95 | { |
---|
[821] | 96 | for (int i = i0; i <= i1; i++) { |
---|
[772] | 97 | X[i] = f32rand(); |
---|
| 98 | } |
---|
| 99 | } |
---|
[821] | 100 | |
---|
| 101 | |
---|
[772] | 102 | /* --------------- */ |
---|
| 103 | int getIter(int size) |
---|
| 104 | /* --------------- */ |
---|
| 105 | { |
---|
| 106 | if (size < 32) return 1024; |
---|
| 107 | if (size < 64) return 64 ; |
---|
| 108 | if (size < 128) return 8; |
---|
| 109 | if (size < 256) return 2; |
---|
| 110 | if (size < 512) return 2; |
---|
| 111 | if (size < 1024) return 2; |
---|
| 112 | if (size < 2048) return 1; |
---|
| 113 | return 2; |
---|
| 114 | } |
---|
[821] | 115 | |
---|
| 116 | |
---|
[772] | 117 | /* ----------------- */ |
---|
| 118 | int getIterAV(int size) |
---|
| 119 | /* ----------------- */ |
---|
| 120 | { |
---|
| 121 | return 3 * getIter(size); |
---|
| 122 | } |
---|
[821] | 123 | |
---|
| 124 | |
---|
[772] | 125 | /* --------------------------------- */ |
---|
| 126 | float32 gauss(float32 sigma, float32 x) |
---|
| 127 | /* --------------------------------- */ |
---|
| 128 | { |
---|
| 129 | float64 pi = 3.1415927; |
---|
| 130 | float64 y; |
---|
| 131 | y = 1.0 / (sqrt(2 * pi) * sigma) * exp(-(x * x) / (2 * sigma * sigma)); |
---|
| 132 | return (float32) y; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /* --------------------- */ |
---|
| 136 | double cpp(double t, int n) |
---|
| 137 | /* --------------------- */ |
---|
| 138 | { |
---|
| 139 | double c = n; |
---|
| 140 | double cpp = t * FREQ / c; |
---|
| 141 | return cpp; |
---|
| 142 | } |
---|
[821] | 143 | |
---|
| 144 | |
---|
[772] | 145 | // -------------------------- |
---|
| 146 | void printf_split12(double x) |
---|
| 147 | // -------------------------- |
---|
| 148 | { |
---|
| 149 | double x3, x2, x1, x0, t = x; |
---|
| 150 | |
---|
| 151 | x0 = fmod(x, 1000); |
---|
| 152 | x = floor(x / 1000); |
---|
| 153 | x1 = fmod(x, 1000); |
---|
| 154 | x = floor(x / 1000); |
---|
| 155 | x2 = fmod(x, 1000); |
---|
| 156 | x = floor(x / 1000); |
---|
| 157 | x3 = fmod(x, 1000); |
---|
| 158 | //printf("t = %14f\n", t); |
---|
| 159 | if (t < 1e3) { |
---|
| 160 | printf("%15.0f", x0); |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | if (t < 1e6) { |
---|
| 164 | printf("%11.0f,%03.0f", x1, x0); |
---|
| 165 | return; |
---|
| 166 | } |
---|
| 167 | if (t < 1e9) { |
---|
| 168 | printf("%7.0f,%03.0f,%03.0f", x2, x1, x0); |
---|
| 169 | return; |
---|
| 170 | } |
---|
| 171 | if (t < 1e12) { |
---|
| 172 | printf("%3.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
---|
| 173 | return; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // -------------------------- |
---|
| 178 | void printf_split15(double x) |
---|
| 179 | // -------------------------- |
---|
| 180 | { |
---|
| 181 | double x4, x3, x2, x1, x0, t = x; |
---|
| 182 | |
---|
| 183 | x0 = fmod(x, 1000); |
---|
| 184 | x = floor(x / 1000); |
---|
| 185 | x1 = fmod(x, 1000); |
---|
| 186 | x = floor(x / 1000); |
---|
| 187 | x2 = fmod(x, 1000); |
---|
| 188 | x = floor(x / 1000); |
---|
| 189 | x3 = fmod(x, 1000); |
---|
| 190 | x = floor(x / 1000); |
---|
| 191 | x4 = fmod(x, 1000); |
---|
| 192 | //printf("t = %14f\n", t); |
---|
| 193 | if (t < 1e3) { |
---|
| 194 | printf("%19.0f", x0); |
---|
| 195 | return; |
---|
| 196 | } |
---|
| 197 | if (t < 1e6) { |
---|
| 198 | printf("%15.0f,%03.0f", x1, x0); |
---|
| 199 | return; |
---|
| 200 | } |
---|
| 201 | if (t < 1e9) { |
---|
| 202 | printf("%11.0f,%03.0f,%03.0f", x2, x1, x0); |
---|
| 203 | return; |
---|
| 204 | } |
---|
| 205 | if (t < 1e12) { |
---|
| 206 | printf("%7.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | if (t < 1e15) { |
---|
| 210 | printf("%3.0f,%3.0f,%03.0f,%03.0f,%03.0f", x4,x3, x2, x1, x0); |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | // -------------------------- |
---|
| 215 | void printf_split18(double x) |
---|
| 216 | // -------------------------- |
---|
| 217 | { |
---|
| 218 | double x5, x4, x3, x2, x1, x0, t = x; |
---|
| 219 | |
---|
| 220 | x0 = fmod(x, 1000); |
---|
| 221 | x = floor(x / 1000); |
---|
| 222 | x1 = fmod(x, 1000); |
---|
| 223 | x = floor(x / 1000); |
---|
| 224 | x2 = fmod(x, 1000); |
---|
| 225 | x = floor(x / 1000); |
---|
| 226 | x3 = fmod(x, 1000); |
---|
| 227 | x = floor(x / 1000); |
---|
| 228 | x4 = fmod(x, 1000); |
---|
| 229 | x = floor(x / 1000); |
---|
| 230 | x5 = fmod(x, 1000); |
---|
| 231 | //printf("t = %14f\n", t); |
---|
| 232 | if (t < 1e3) { |
---|
| 233 | printf("%23.0f", x0); |
---|
| 234 | return; |
---|
| 235 | } |
---|
| 236 | if (t < 1e6) { |
---|
| 237 | printf("%19.0f,%03.0f", x1, x0); |
---|
| 238 | return; |
---|
| 239 | } |
---|
| 240 | if (t < 1e9) { |
---|
| 241 | printf("%15.0f,%03.0f,%03.0f", x2, x1, x0); |
---|
| 242 | return; |
---|
| 243 | } |
---|
| 244 | if (t < 1e12) { |
---|
| 245 | printf("%11.0f,%03.0f,%03.0f,%03.0f", x3, x2, x1, x0); |
---|
| 246 | return; |
---|
| 247 | } |
---|
| 248 | if (t < 1e15) { |
---|
| 249 | printf("%7.0f,%3.0f,%03.0f,%03.0f,%03.0f", x4, x3, x2, x1, x0); |
---|
| 250 | return; |
---|
| 251 | } |
---|
| 252 | if (t < 1e18) { |
---|
| 253 | printf("%3.0f,%3.0f,%3.0f,%03.0f,%03.0f,%03.0f", x5, x4, x3, x2, x1, x0); |
---|
| 254 | return; |
---|
| 255 | } |
---|
| 256 | } |
---|
[821] | 257 | |
---|
| 258 | |
---|
[772] | 259 | // ------------------------ |
---|
| 260 | void printf_split(double x) |
---|
| 261 | // ------------------------ |
---|
| 262 | { |
---|
| 263 | printf_split12(x); |
---|
| 264 | //printf_split15(x); |
---|
| 265 | //printf_split18(x); |
---|
| 266 | } |
---|
[821] | 267 | |
---|
| 268 | // Local Variables: |
---|
| 269 | // tab-width: 4 |
---|
| 270 | // c-basic-offset: 4 |
---|
| 271 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 272 | // indent-tabs-mode: nil |
---|
| 273 | // End: |
---|
| 274 | |
---|
| 275 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 276 | |
---|