pcg_random 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4224a9b36896755ece6fca2b0845557df14c600
4
- data.tar.gz: 0eee51e96175a204bcea1bb18009bc5b5132d347
3
+ metadata.gz: 80bdb7b230ebe145ca36718ca2b5ce75927611d3
4
+ data.tar.gz: f54e765d9d10ce35f10ad63f1cbaa0e8f120fc80
5
5
  SHA512:
6
- metadata.gz: bb55e8170e1e2cd965ea38f1c00e11656e440f6813d8ed3137e4c9857e5a62a0e095b07103d7934e8b35566077c6138f810c908512182ac624bd7e723bc8a64a
7
- data.tar.gz: 312dfe2f1cd5f6241c965f31fadc994f33cc44e5cc707d45e3cd5716131f681c8e7486013fd142193b2d5fea457dee5470da222a9650a201aa4e1557341cb5bb
6
+ metadata.gz: daf8fdc612fdfb584896480a5ccb5f416cad2b235bbd8f46155c568dcee6b35c911a13eb7fc1af6a7ae5be2b3bd448cbe9f99506b06a17fa05403f5baa4ebc02
7
+ data.tar.gz: 84057b096b179ffe467e470ba42ac35f84c626edf6c006f108aa9643f0e7b1e525d807a29683774be6e887844205499c7dab06aed3140c5e9f56477ebccbbff9
data/.gitignore CHANGED
@@ -8,13 +8,12 @@
8
8
  /pkg/
9
9
  /spec/reports/
10
10
  /tmp/
11
-
11
+ /vendor/
12
12
  # Compiled binaries
13
13
  *.bundle
14
14
  *.so
15
15
  *.o
16
16
  *.a
17
- *.gem
18
17
 
19
18
  # logs
20
19
  *.log
@@ -2,7 +2,7 @@ require "mkmf"
2
2
 
3
3
  RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
4
4
 
5
- $CFLAGS = "-Wall -pedantic -ansi -std=c99"
5
+ $CFLAGS = " -Wall -pedantic -ansi -std=c99"
6
6
 
7
7
  LIBDIR = RbConfig::CONFIG['libdir']
8
8
  INCLUDEDIR = RbConfig::CONFIG['includedir']
@@ -21,8 +21,12 @@ LIB_DIRS = [
21
21
  '/usr/lib',
22
22
  ]
23
23
 
24
- abort 'libpcgrandom is missing' unless find_header('pcg_variants.h')
25
- abort 'entropy.h missing' unless find_header('entropy.h')
26
-
24
+ # Typically, it installs libpcg_random.a to /usr/local/lib and
25
+ # pcg_variants.h in /usr/local/include/pcg_variants.h
27
26
  dir_config('pcg_random', HEADER_DIRS, LIB_DIRS)
27
+
28
+ abort 'libpcg_random is missing' unless have_library('pcg_random', 'pcg32_random')
29
+ abort 'entropy.h missing' unless have_header('entropy.h')
30
+ abort 'pcg_variants.h missing' unless have_header('pcg_variants.h')
31
+
28
32
  create_makefile("pcg_random/pcg_random")
@@ -7,7 +7,7 @@
7
7
  #include "pcg_random.h"
8
8
  #include "pcg_seed.h"
9
9
 
10
- VALUE rb_mPCGRandom;
10
+ VALUE rb_cPCGRandom;
11
11
 
12
12
  void
13
13
  Init_pcg_random(void)
@@ -16,9 +16,9 @@ Init_pcg_random(void)
16
16
  pcg_init_rb_constants();
17
17
 
18
18
  /* Define encapsulating module */
19
- rb_mPCGRandom = rb_define_class("PCGRandom", rb_cObject);
19
+ rb_cPCGRandom = rb_define_class("PCGRandom", rb_cObject);
20
20
 
21
21
  /* Define methods under PCGRandom */
22
- rb_define_singleton_method(rb_cPCGRandom, "new_seed", pcg_func_new_seed, 0);
22
+ rb_define_singleton_method(rb_cPCGRandom, "new_seed", pcg_func_new_seed, -1);
23
23
  rb_define_singleton_method(rb_cPCGRandom, "raw_seed", pcg_func_raw_seed, 1);
24
24
  }
@@ -6,29 +6,53 @@
6
6
  #include "rb_constants.h"
7
7
  #include "pcg_seed.h"
8
8
 
9
- /* Functions local to this source file */
10
- static VALUE pcg_new_seed_bytestr(void);
9
+ static VALUE pcg_new_seed_bytestr(unsigned long seed_size);
11
10
  static VALUE pcg_raw_seed_bytestr(size_t size);
12
- static VALUE pcg_rb_unpack_str_ui64(VALUE str);
11
+ static VALUE pcg_rb_unpack_str_uint(VALUE str);
13
12
 
14
13
  /*
15
- * Returns a 128-bit big-integer that stores the seed value used to seed the
14
+ * Returns a n-bit integer that stores the seed value used to seed the
16
15
  * initial state and sequence for the PCG generator.
16
+ * If no parameters are supplied it default to a 128-bit seed size
17
+ *
18
+ * @param size Number of bytes (EVEN!) that the generated seed must contain.
19
+ * Defaults to 16 Bytes = 128 bits
20
+ *
21
+ * @raise ArgumentError if size is not even
17
22
  */
18
23
  VALUE
19
- pcg_func_new_seed(void)
24
+ pcg_func_new_seed(int argc, VALUE *argv, VALUE self)
20
25
  {
21
- return pcg_new_seed_bytestr();
26
+ VALUE seed_size;
27
+ unsigned long n;
28
+
29
+ if(argc == 0)
30
+ {
31
+ return pcg_new_seed_bytestr(2 * sizeof(uint64_t));
32
+ }
33
+
34
+ rb_scan_args(argc, argv, "01", &seed_size);
35
+ n = NUM2ULONG(seed_size);
36
+
37
+ // Only accept even seed sizes since this later needs to be split into
38
+ // two parts!
39
+ if(n % 2 == 1)
40
+ {
41
+ rb_raise(rb_eArgError, "Seed size must be even! Found %lu", n);
42
+ }
43
+ return pcg_new_seed_bytestr(n * sizeof(char));
22
44
  }
23
45
 
24
46
  /*
25
- * Generates a random seed represented as a sequence of bytes
47
+ * Generates a random seed string represented as a sequence of bytes
48
+ *
49
+ * @param size Size of the bytestring to generate
26
50
  */
27
51
  VALUE
28
52
  pcg_func_raw_seed(VALUE self, VALUE byte_size)
29
53
  {
30
54
  unsigned long n = NUM2ULONG(byte_size);
31
- if( n == 0 )
55
+ if(n == 0)
32
56
  {
33
57
  return rb_str_new2("\0");
34
58
  }
@@ -42,24 +66,38 @@ pcg_func_raw_seed(VALUE self, VALUE byte_size)
42
66
  bool
43
67
  pcg_func_entropy_getbytes(void *dest, size_t size)
44
68
  {
45
- /* Get random bytes from /dev/random or a fallback source */
46
- if( !entropy_getbytes(dest, size) )
47
- {
48
- fallback_entropy_getbytes(dest, size);
49
- return true;
50
- }
51
- return false;
69
+ // Get random bytes from /dev/random or a fallback source
70
+ return entropy_getbytes(dest, size);
52
71
  }
53
72
 
54
73
  /*
55
- * Internal - Unpacks a string `str` wuth str.unpack('Q*')
56
- * Unpacks and returns the 0th entry (since it's always a 1 element array)
74
+ * Internal - Unpacks a string `str` wuth String#unpack
57
75
  */
58
76
  static VALUE
59
- pcg_rb_unpack_str_ui64(VALUE str)
77
+ pcg_rb_unpack_str_uint(VALUE str)
60
78
  {
61
- VALUE ary = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("Q*\0"));
62
- return rb_ary_entry(ary, 0);
79
+ VALUE ary, result, current, base;
80
+ unsigned long len;
81
+
82
+ // This would be an array of 8 bit fixnums, effectively representing it in
83
+ // base-256
84
+ ary = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*\0"));
85
+ len = RARRAY_LEN(ary);
86
+
87
+ base = INT2FIX(256);
88
+ result = pcg_rb_zero;
89
+
90
+ // First entry in array corresponds to highest significance
91
+ for(int i = 0; i < len; ++i)
92
+ {
93
+ current = rb_ary_entry(ary, i);
94
+ // Maybe optimize the below code based on on TYPE(result) later ?
95
+ // Currently this doesnt utilize the C-API a effectively
96
+ result = rb_funcall(result, pcg_rb_plus, 1, current);
97
+ result = rb_funcall(result, pcg_rb_mul, 1 , base);
98
+ }
99
+
100
+ return result;
63
101
  }
64
102
 
65
103
  /*
@@ -70,25 +108,27 @@ static VALUE
70
108
  pcg_raw_seed_bytestr(size_t size)
71
109
  {
72
110
  char *bytestr = (char *) malloc(size + 1);
111
+ VALUE result;
112
+
73
113
  memset(bytestr, 0, size + 1);
74
114
  pcg_func_entropy_getbytes((void *)bytestr, size);
75
115
 
76
- return rb_str_new2(bytestr);
116
+ result = rb_str_new2(bytestr);
117
+ free(bytestr);
118
+
119
+ return result;
77
120
  }
78
121
 
79
122
  /*
80
123
  * Internal - Two random integers are sourced from /dev/random or a fallback
81
- * technique.
124
+ * source
82
125
  * Seed generated by manipulating a 16byte string & unpacking it to Q*
83
- *
84
- * @see pcg_new_seed_bigmul() for alternative techniques
85
126
  */
86
127
  static VALUE
87
- pcg_new_seed_bytestr(void)
128
+ pcg_new_seed_bytestr(unsigned long seed_size)
88
129
  {
89
130
  VALUE seed_bytes_str;
90
-
91
- seed_bytes_str = pcg_raw_seed_bytestr(2 * sizeof(uint64_t));
92
- return pcg_rb_unpack_str_ui64(seed_bytes_str);
131
+ seed_bytes_str = pcg_raw_seed_bytestr(seed_size);
132
+ return pcg_rb_unpack_str_uint(seed_bytes_str);
93
133
  }
94
134
 
@@ -4,7 +4,7 @@
4
4
  #include <ruby.h>
5
5
  #include <stdbool.h>
6
6
 
7
- VALUE pcg_func_new_seed(void);
7
+ VALUE pcg_func_new_seed(int argc, VALUE *argv, VALUE self);
8
8
  bool pcg_func_entropy_getbytes(void* dest, size_t size);
9
9
  VALUE pcg_func_raw_seed(VALUE self, VALUE byte_size);
10
10
 
@@ -2,12 +2,25 @@
2
2
  #include "rb_constants.h"
3
3
 
4
4
  VALUE pcg_rb_zero,
5
- pcg_rb_one;
5
+ pcg_rb_one,
6
+ pcg_rb_big_zero,
7
+ pcg_rb_big_one;
8
+
9
+
10
+ ID pcg_rb_plus,
11
+ pcg_rb_mul;
6
12
 
7
13
  void
8
14
  pcg_init_rb_constants(void)
9
15
  {
10
- /* Commonly used ruby objects */
11
- pcg_rb_zero = INT2FIX(0u);
12
- pcg_rb_one = INT2FIX(1u);
16
+ // Commonly used ruby objects
17
+ pcg_rb_zero = INT2FIX(0);
18
+ pcg_rb_one = INT2FIX(1);
19
+
20
+ pcg_rb_big_zero = rb_uint2big(0);
21
+ pcg_rb_big_one = rb_uint2big(1);
22
+
23
+ // Commonly called functions
24
+ pcg_rb_plus = rb_intern("+");
25
+ pcg_rb_mul = rb_intern("*");
13
26
  }
@@ -4,7 +4,12 @@
4
4
  #include <ruby.h>
5
5
 
6
6
  extern VALUE pcg_rb_zero,
7
- pcg_rb_one;
7
+ pcg_rb_one,
8
+ pcg_rb_big_zero,
9
+ pcg_rb_big_one;
10
+
11
+ extern ID pcg_rb_plus,
12
+ pcg_rb_mul;
8
13
 
9
14
  void pcg_init_rb_constants(void);
10
15
 
@@ -1,3 +1,3 @@
1
1
  class PCGRandom
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/pcg_random.gemspec CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_development_dependency 'bundler', '~> 1.11'
32
32
  spec.add_development_dependency 'rake', '~> 10.0'
33
- spec.add_development_dependency 'rake-compiler', '~> 0'
33
+ spec.add_development_dependency 'rake-compiler'
34
34
  spec.add_development_dependency 'rspec', '~> 3.0'
35
- spec.add_development_dependency 'pry', '~> 0'
35
+ spec.add_development_dependency 'pry'
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pcg_random
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaibhav Yenamandra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-16 00:00:00.000000000 Z
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,14 +42,14 @@ dependencies:
42
42
  name: rake-compiler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
@@ -70,14 +70,14 @@ dependencies:
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: PCG is a family of simple fast space-efficient statistically good algorithms