Skip to content

Instantly share code, notes, and snippets.

@matthewd
Created April 20, 2010 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewd/371845 to your computer and use it in GitHub Desktop.
Save matthewd/371845 to your computer and use it in GitHub Desktop.
From fe0b71f4fd860ec3cff4b25d6b61a30d1a04f63f Mon Sep 17 00:00:00 2001
From: Matthew Draper <matthew@trebex.net>
Date: Tue, 20 Apr 2010 09:48:28 +0930
Subject: [PATCH] Tidy up randomizer a bit
* Prune includes down to what's actually needed
* Define and use a native_uint instead of uintptr_t
* Don't need to check the result of into_array; that paranoia was only
really warranted when independently "guessing" the correct length
* Don't assume an alloca() will persist outside the scope it's called
---
vm/builtin/randomizer.cpp | 44 ++++++++++++++++++++------------------------
vm/builtin/randomizer.hpp | 6 ++----
vm/prelude.hpp | 1 +
3 files changed, 23 insertions(+), 28 deletions(-)
diff --git a/vm/builtin/randomizer.cpp b/vm/builtin/randomizer.cpp
index e6c4a07..6ecbe92 100644
--- a/vm/builtin/randomizer.cpp
+++ b/vm/builtin/randomizer.cpp
@@ -1,26 +1,24 @@
#include "builtin/randomizer.hpp"
-#include "ffi.hpp"
#include "vm.hpp"
#include "objectmemory.hpp"
-#include "builtin/array.hpp"
+#include "builtin/bytearray.hpp"
#include "builtin/bignum.hpp"
#include "builtin/class.hpp"
-#include "builtin/exception.hpp"
-#include "builtin/fixnum.hpp"
#include "builtin/float.hpp"
#include "builtin/integer.hpp"
-#include "builtin/memorypointer.hpp"
-#include "builtin/string.hpp"
-#include "builtin/tuple.hpp"
#include "object_utils.hpp"
+/*
+ * For gen_seed:
+ */
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
+
/*
This is based on random.c from Ruby 1.8.7, which carries the following notice:
@@ -177,14 +175,12 @@ namespace rubinius {
return y;
}
- uintptr_t Randomizer::limited_rand(uintptr_t limit) {
- uintptr_t mask = make_mask(limit);
- int i;
- uintptr_t val;
+ native_uint Randomizer::limited_rand(native_uint limit) {
+ native_uint mask = make_mask(limit);
retry:
- val = 0;
- for (i = sizeof(uintptr_t)/4-1; 0 <= i; i--) {
+ native_uint val = 0;
+ for (int i = sizeof(native_uint)/4-1; 0 <= i; i--) {
if (mask >> (i * 32)) {
val |= rb_genrand_int32() << (i * 32);
val &= mask;
@@ -278,40 +274,41 @@ namespace rubinius {
seed = as<Bignum>(seed)->abs(state);
if (seed->fixnum_p()) {
- uintptr_t s = seed->to_native();
+ native_uint s = seed->to_native();
// Remove the sign bit, for no good reason... but that's what MRI
// does.
- s &= ~((uintptr_t)1 << (sizeof(uintptr_t) * 8 - 1));
+ s &= ~((native_uint)1 << (sizeof(native_uint) * 8 - 1));
if (s <= 0xffffffffUL) {
init_genrand((uint32_t)s);
return Qnil;
}
- longs = sizeof(uintptr_t) / 4;
+ longs = sizeof(native_uint) / 4;
data = (uint32_t*)alloca(longs * 4);
for (unsigned int i = 0; i < longs; i++)
data[i] = (uint32_t)(s >> (i * 32));
+
+ init_by_array(data, longs);
} else {
big = as<Bignum>(seed);
longs = big->into_array(state, NULL, 0);
data = (uint32_t*)alloca(longs * 4);
- size_t output_count = big->into_array(state, data, longs);
- if(longs < output_count) { abort(); }
- }
+ big->into_array(state, data, longs);
- init_by_array(data, longs);
+ init_by_array(data, longs);
+ }
return Qnil;
}
Integer* Randomizer::rand_int(STATE, Integer* max) {
if (max->fixnum_p()) {
- native_int max_i = max->to_native();
- uintptr_t result = limited_rand((uintptr_t)(max_i));
+ native_uint max_i = max->to_native();
+ native_uint result = limited_rand(max_i);
return Integer::from(state, result);
}
@@ -319,8 +316,7 @@ namespace rubinius {
size_t longs = max_big->into_array(state, NULL, 0);
uint32_t* max_data = (uint32_t*)alloca(longs * 4);
- size_t output_count = max_big->into_array(state, max_data, longs);
- if(longs < output_count) { abort(); }
+ max_big->into_array(state, max_data, longs);
uint32_t* result_data = (uint32_t*)alloca(longs * 4);
diff --git a/vm/builtin/randomizer.hpp b/vm/builtin/randomizer.hpp
index 0aa2dae..e669355 100644
--- a/vm/builtin/randomizer.hpp
+++ b/vm/builtin/randomizer.hpp
@@ -2,13 +2,11 @@
#define RBX_BUILTIN_RANDOMIZER_HPP
#include "builtin/object.hpp"
-#include "builtin/bignum.hpp"
-#include "builtin/bytearray.hpp"
-#include "builtin/integer.hpp"
#include "type_info.hpp"
namespace rubinius {
class ByteArray;
+ class Float;
class Integer;
class Randomizer : public Object {
@@ -26,7 +24,7 @@ namespace rubinius {
void init_by_array(uint32_t init_key[], int key_length);
void next_state();
uint32_t rb_genrand_int32();
- uintptr_t limited_rand(uintptr_t limit);
+ native_uint limited_rand(native_uint limit);
double rb_genrand_real();
public:
diff --git a/vm/prelude.hpp b/vm/prelude.hpp
index 2a76715..28aee2d 100644
--- a/vm/prelude.hpp
+++ b/vm/prelude.hpp
@@ -27,6 +27,7 @@ namespace rubinius {
/** Platform-dependent integer type large enough for pointers too. */
typedef intptr_t native_int;
+ typedef uintptr_t native_uint;
typedef native_int hashval;
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment