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