1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Generate C test fixtures.
*
* ## Notes
*
* - Run this script from the directory in which fixtures should be written.
*
*/
#include <stdlib.h>
#include <stdio.h>
/**
* Generates a linearly spaced numeric array of doubles.
*
* ## Notes
*
* - Assumes that the output array has at least 2 elements.
* - Output array is guaranteed to include both the start and end values.
*
*
* @param out output array
* @param len array length
* @param start first value
* @param end last value
*/
void linspace_f64( double *out, const unsigned int len, const double start, const double end ) {
unsigned int i;
double incr;
incr = (end-start) / (len-1);
for ( i = 0; i < len-1; i++ ) {
out[ i ] = start + (incr*i);
}
out[ i ] = end;
}
/**
* Generates a linearly spaced numeric array of integers.
*
* ## Notes
*
* - Assumes that the output array has at least 2 elements.
* - Output array is guaranteed to include both the start and end values.
*
*
* @param out output array
* @param len array length
* @param start first value
* @param end last value
*/
void linspace_ui32( unsigned int *out, const unsigned int len, const unsigned int start, const unsigned int end ) {
unsigned int incr;
unsigned int i;
incr = (end-start) / (len-1);
for ( i = 0; i < len-1; i++ ) {
out[ i ] = start + (incr*i);
}
out[ i ] = end;
}
/**
* Generates a random number on the interval [0,1].
*
* @return random number
*/
double rand_double() {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
}
/**
* Generates an array of pseudorandom doubles drawn from a uniform distribution.
*
* @param out output array
* @param len array length
* @param a lower bound (inclusive)
* @param b upper bound (exclusive)
*/
void rand_array_f64( double *out, const unsigned int len, const double a, const double b ) {
unsigned int i;
double delta;
delta = b - a;
for ( i = 0; i < len; i++ ) {
out[ i ] = a + ( rand_double()*delta );
}
}
/**
* Generates an array of pseudorandom integers drawn from a uniform distribution.
*
* ## Notes
*
* - WARNING: the method used here is not particularly robust, as some integer values may be sampled more frequently than others.
*
*
* @param out output array
* @param len array length
* @param a lower bound (inclusive)
* @param b upper bound (exclusive)
*/
void rand_array_ui32( unsigned int *out, const unsigned int len, const unsigned int a, const unsigned int b ) {
unsigned int i;
unsigned int r;
double delta;
delta = (double)b - (double)a;
for ( i = 0; i < len; i++ ) {
r = (unsigned int)( delta * rand_double() ); // truncation
out[ i ] = a + r;
}
}
/**
* Casts an array of integers to an array of doubles.
*
* @param out output array
* @param x input array
* @param len array length
*/
void ui32_to_f64( double *out, unsigned int *x, unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
out[ i ] = (double) x[ i ];
}
}
/**
* Casts an array of doubles to an array of integers.
*
* @param out output array
* @param x input array
* @param len array length
*/
void f64_to_ui32( unsigned int *out, double *x, unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
out[ i ] = (unsigned int) x[ i ];
}
}
/**
* Writes an array of doubles to a file as a series of comma-separated values.
*
* @param f file to write to
* @param x array of doubles
* @param len array length
*/
void write_array_f64( FILE *f, const double *x, const unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
fprintf( f, "%.17g", x[ i ] );
if ( i < len-1 ) {
fprintf( f, "," );
}
}
}
/**
* Writes an array of unsigned integers to a file as a series of comma-separated values.
*
* @param f file to write to
* @param x array of integers
* @param len array length
*/
void write_array_ui32( FILE *f, const unsigned int *x, const unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
fprintf( f, "%u", x[ i ] );
if ( i < len-1 ) {
fprintf( f, "," );
}
}
}
/**
* Writes an array of signed integers to a file as a series of comma-separated values.
*
* @param f file to write to
* @param x array of integers
* @param len array length
*/
void write_array_i32( FILE *f, const int *x, const unsigned int len ) {
unsigned int i;
for ( i = 0; i < len; i++ ) {
fprintf( f, "%d", x[ i ] );
if ( i < len-1 ) {
fprintf( f, "," );
}
}
}
/**
* Writes a named array of doubles to a file as JSON.
*
* @param f file to write to
* @param name array name
* @param x data
* @param len array length
*/
void write_named_array_f64( FILE *f, const char *name, const double *x, const unsigned int len ) {
fprintf( f, "\"%s\":[", name );
write_array_f64( f, x, len );
fprintf( f, "]" );
}
/**
* Writes a named array of integers to a file as JSON.
*
* @param f file to write to
* @param name array name
* @param x data
* @param len array length
*/
void write_named_array_ui32( FILE *f, const char *name, const unsigned int *x, const unsigned int len ) {
fprintf( f, "\"%s\":[", name );
write_array_ui32( f, x, len );
fprintf( f, "]" );
}
/**
* Writes data to a file as JSON.
*
* ## Notes
*
* - This function SHOULD be tailored to the input data (e.g., input types, output types, number of arguments, etc) and may vary from use case to use case.
*
*
* @param f file to write to
* @param a domain
* @param b domain
* @param y results
* @param len array length
*/
void write_data_as_json( FILE *f, const unsigned int *a, const unsigned int *b, const unsigned int *y, const unsigned int len ) {
int i;
fprintf( f, "{" );
write_named_array_ui32( f, "a", a, len );
fprintf( f, "," );
write_named_array_ui32( f, "b", b, len );
fprintf( f, "," );
fprintf( f, "\"expected\":[" );
for ( i = 0; i < len; i++ ) {
fprintf( f, "[" );
write_array_i32( f, &y[ i * 2 ], 2 );
fprintf( f, "]" );
if ( i < len - 1 ) {
fprintf( f, "," );
}
}
fprintf( f, "]" );
fprintf( f, "}" );
}
/**
* Computes the double word product of two (signed) words.
*
* ## References
*
* - [muldws.c](http://www.hackersdelight.org/hdcodetxt/muldws.c.txt)
*
* @param w output array
* @param u first word
* @param v second word
*/
void muldws1( int *w, const int u, const int v ) {
unsigned int u1;
unsigned int v1;
unsigned int w1;
unsigned int w2;
unsigned int w3;
unsigned int k;
unsigned int t;
int u0;
int v0;
u0 = u >> 16;
u1 = u & 0xFFFF;
v0 = v >> 16;
v1 = v & 0xFFFF;
t = u1*v1;
w3 = t & 0xFFFF;
k = t >> 16;
t = ( u0*v1 ) + k;
w2 = t & 0xFFFF;
w1 = (int)t >> 16;
t = ( u1*v0 ) + w2;
k = (int)t >> 16;
w[ 0 ] = ( u0*v0 ) + w1 + k;
w[ 1 ] = (t << 16) + w3;
return;
}
/**
* Generates test fixtures.
*
* @param a domain
* @param b domain
* @param len number of values in the domain
* @param name output filename
*/
void generate( unsigned int *a, unsigned int *b, const unsigned int len, const char *name ) {
unsigned int i;
unsigned int *y;
FILE *f;
// Allocate an output array:
y = (unsigned int*) malloc( len * sizeof(unsigned int) * 2 );
if ( y == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
// Generate fixture data:
for ( i = 0; i < len; i++ ) {
muldws1( (int*) &y[ i*2 ], a[ i ], b[ i ] );
}
// Open a new file:
f = fopen( name, "w" );
if ( f == NULL ) {
printf( "Error opening file.\n" );
exit( 1 );
}
// Write data as JSON:
write_data_as_json( f, a, b, y, len );
// Close the file:
fclose( f );
// Free allocated memory:
free( y );
}
/**
* Main execution sequence.
*/
int main( void ) {
unsigned int len;
unsigned int *a;
unsigned int *b;
// Define the array length:
len = 5000;
// Allocate arrays:
a = (unsigned int*) malloc( len * sizeof(unsigned int) );
if ( a == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
b = (unsigned int*) malloc( len * sizeof(unsigned int) );
if ( b == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
// Generate fixture data:
rand_array_ui32( a, len, 0, 4294967295 );
rand_array_ui32( b, len, 0, 4294967295 );
generate( a, b, len, "data.json" );
// Free allocated memory:
free( a );
free( b );
return 0;
}
|