Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpreynolds committed Apr 23, 2015
1 parent c819786 commit b02947c
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 0 deletions.
50 changes: 50 additions & 0 deletions byte-square.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "byte-square.h"

int
main(int argc, char** argv)
{
bs_init_command_line_options(argc, argv);
return EXIT_SUCCESS;
}

void
bs_init_command_line_options(int argc, char** argv)
{
fprintf(stderr, "argc:\t\t[%d]\n", argc);
for (int argc_idx = 0; argc_idx < argc; argc_idx++) {
fprintf(stderr, "argv[%02d]:\t[%s]\n", argc_idx, argv[argc_idx]);
}

int bs_client_long_index;
int bs_client_opt = getopt_long(argc,
argv,
bs_client_opt_string,
bs_client_long_options,
&bs_client_long_index);

opterr = 0;

while (bs_client_opt != -1) {
switch (bs_client_opt) {
case 'h':
case '?':
bs_print_usage(stdout);
exit(EXIT_SUCCESS);
default:
break;
}
bs_client_opt = getopt_long(argc,
argv,
bs_client_opt_string,
bs_client_long_options,
&bs_client_long_index);
}
}

void
bs_print_usage(FILE* output_stream)
{
fprintf(output_stream,
"Usage: %s (--store-make|--store-query) --lookup-file=fn --store-file=fn\n",
bs_name);
}
21 changes: 21 additions & 0 deletions byte-square.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef BYTE_SQUARE_H_
#define BYTE_SQUARE_H_

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "mt19937.h"

void bs_init_command_line_options(int argc, char** argv);
void bs_print_usage(FILE* output_stream);

static struct option bs_client_long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};

static const char *bs_client_opt_string = "h?";

static const char *bs_name = "byte-square";

#endif // BYTE_SQUARE_H_
13 changes: 13 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BLDFLAGS = -Wall -Wextra -pedantic -std=c99
CFLAGS = -D__STDC_CONSTANT_MACROS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -O3

all:
$(CC) $(BLDFLAGS) $(CFLAGS) -c mt19937.c -o mt19937.o
$(AR) rcs mt19937.a mt19937.o
$(CC) $(BLDFLAGS) $(CFLAGS) -c byte-square.c -o byte-square.o
$(CC) $(BLDFLAGS) $(CFLAGS) byte-square.o -o byte-square mt19937.a

clean:
rm byte-square
rm -rf *.o
rm -rf *~
Binary file added mt19937.a
Binary file not shown.
163 changes: 163 additions & 0 deletions mt19937.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
A C-program for MT19937: Real number version
mt19937_generate_random_double() generates one pseudorandom
real number (double) which is uniformly distributed on
[0,1]-interval, for each call.
mt19937_seed_rng(seed) set initial values to the working area
of 624 words. Before mt19937_generate_random_double(),
mt19937_seed_rng(seed) must be called once. (seed is any
32-bit integer except for 0).
Coded by Takuji Nishimura, considering the suggestions by
Topher Cooper and Marc Rieffel in July-Aug. 1997.
------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General
Public License along with this library; if not, write to the
Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
Any feedback is very welcome. For any question, comments,
see http://www.math.keio.ac.jp/matumoto/emt.html or email
[email protected]
*/

#include "mt19937.h"

unsigned long mt[MT19937_N] = {0};
int mti = MT19937_N + 1;

/* initializing the array with a NONZERO seed */
void mt19937_seed_rng(unsigned long seed)
{
#ifdef DEBUG
fprintf(stderr, "Debug: Entering --> mt19937_seed_rng()\n");
#endif

/*
setting initial seeds to mt[N] using
the generator Line 25 of Table 1 in
[KNUTH 1981, The Art of Computer Programming
Vol. 2 (2nd Ed.), pp102]
*/
mt[0]= seed & 0xffffffff;
for (mti = 1; mti < MT19937_N; ++mti)
mt[mti] = (69069 * mt[mti - 1]) & 0xffffffff;

#ifdef DEBUG
fprintf(stderr, "Debug: Leaving --> mt19937_seed_rng()\n");
#endif
}

double mt19937_generate_random_double()
{
#ifdef DEBUG
fprintf(stderr, "Debug: Entering --> mt19937_generate_random_double()\n");
#endif

unsigned long y;
static unsigned long mag01[2] = {0x0, MT19937_MATRIX_A};
/* mag01[x] = x * MT19937_MATRIX_A for x=0,1 */

if (mti >= MT19937_N)
{
/* generate N words at one time */
int kk;

/* if mt19937_seed_rng() has not been called, a default initial seed is used */
if (mti == MT19937_N + 1)
mt19937_seed_rng(4357);

for (kk = 0;kk < MT19937_N - MT19937_M; ++kk)
{
y = (mt[kk] & MT19937_UPPER_MASK) | (mt[kk+1] & MT19937_LOWER_MASK);
mt[kk] = mt[kk + MT19937_M] ^ (y >> 1) ^ mag01[y & 0x1];
}

for (;kk < MT19937_N - 1; ++kk)
{
y = (mt[kk] & MT19937_UPPER_MASK) | (mt[kk + 1] & MT19937_LOWER_MASK);
mt[kk] = mt[kk + (MT19937_M - MT19937_N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[MT19937_N - 1] & MT19937_UPPER_MASK) | (mt[0] & MT19937_LOWER_MASK);
mt[MT19937_N - 1] = mt[MT19937_M - 1] ^ (y >> 1) ^ mag01[y & 0x1];

mti = 0;
}

y = mt[mti++];
y ^= MT19937_SHIFT_U(y);
y ^= MT19937_SHIFT_S(y) & MT19937_MASK_B;
y ^= MT19937_SHIFT_T(y) & MT19937_MASK_C;
y ^= MT19937_SHIFT_L(y);

#ifdef DEBUG
fprintf(stderr, "Debug: Leaving --> mt19937_generate_random_double()\n");
#endif

return (double) y / (unsigned long) 0xffffffff;
}

unsigned long mt19937_generate_random_ulong()
{
#ifdef DEBUG
fprintf(stderr, "Debug: Entering --> mt19937_generate_random_ulong()\n");
#endif

unsigned long y;
static unsigned long mag01[2] = {0x0, MT19937_MATRIX_A};
/* mag01[x] = x * MT19937_MATRIX_A for x=0,1 */

if (mti >= MT19937_N)
{
/* generate N words at one time */
int kk;

/* if mt19937_seed_rng() has not been called, a default initial seed is used */
if (mti == MT19937_N + 1)
mt19937_seed_rng(4357);

for (kk = 0;kk < MT19937_N - MT19937_M; ++kk)
{
y = (mt[kk] & MT19937_UPPER_MASK) | (mt[kk+1] & MT19937_LOWER_MASK);
mt[kk] = mt[kk + MT19937_M] ^ (y >> 1) ^ mag01[y & 0x1];
}

for (;kk < MT19937_N - 1; ++kk)
{
y = (mt[kk] & MT19937_UPPER_MASK) | (mt[kk + 1] & MT19937_LOWER_MASK);
mt[kk] = mt[kk + (MT19937_M - MT19937_N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[MT19937_N - 1] & MT19937_UPPER_MASK) | (mt[0] & MT19937_LOWER_MASK);
mt[MT19937_N - 1] = mt[MT19937_M - 1] ^ (y >> 1) ^ mag01[y & 0x1];

mti = 0;
}

y = mt[mti++];
y ^= MT19937_SHIFT_U(y);
y ^= MT19937_SHIFT_S(y) & MT19937_MASK_B;
y ^= MT19937_SHIFT_T(y) & MT19937_MASK_C;
y ^= MT19937_SHIFT_L(y);

#ifdef DEBUG
fprintf(stderr, "Debug: Leaving --> mt19937_generate_random_ulong()\n");
#endif

return y;
}
36 changes: 36 additions & 0 deletions mt19937.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef MT19937_H
#define MT19937_H

#include <stdio.h>

/* Period parameters */
#define MT19937_N 624
#define MT19937_M 397
#define MT19937_MATRIX_A 0x9908b0df /* constant vector a */
#define MT19937_UPPER_MASK 0x80000000 /* most significant w-r bits */
#define MT19937_LOWER_MASK 0x7fffffff /* least significant r bits */

/* Tempering parameters */
#define MT19937_MASK_B 0x9d2c5680
#define MT19937_MASK_C 0xefc60000
#define MT19937_SHIFT_U(y) (y >> 11)
#define MT19937_SHIFT_S(y) (y << 7)
#define MT19937_SHIFT_T(y) (y << 15)
#define MT19937_SHIFT_L(y) (y >> 18)

extern unsigned long mt[MT19937_N]; /* the array for the state vector */
extern int mti; /* mti == N+1 means mt[N] is not initialized */

#ifdef __cplusplus
extern "C" {
#endif

void mt19937_seed_rng(unsigned long seed);
double mt19937_generate_random_double();
unsigned long mt19937_generate_random_ulong();

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit b02947c

Please sign in to comment.