Browse files

native: optimize argument parsing

  • Loading branch information...
1 parent d488d35 commit 455d4d69eab6eebf7a230170ee11c0f9ec5ad8c0 @funny-falcon committed Aug 6, 2012
Showing with 52 additions and 26 deletions.
  1. +7 −0 Rakefile
  2. +44 −25 ext/murmurhash3/murmur3.c
  3. +1 −1 lib/murmurhash3/version.rb
View
7 Rakefile
@@ -1,2 +1,9 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+
+require 'rake/testtask'
+Rake::TestTask.new do |i|
+ i.libs << 'ext'
+ i.options = '-v'
+ i.verbose = true
+end
View
69 ext/murmurhash3/murmur3.c
@@ -293,54 +293,61 @@ rb_fmix64(VALUE self, VALUE integer)
static VALUE
rb_murmur3_32_str_hash(int argc, VALUE* argv, VALUE self)
{
- VALUE rstr, rseed;
+ VALUE rstr;
uint32_t result;
- rb_scan_args(argc, argv, "11", &rstr, &rseed);
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (string[, seed])");
+ }
+ rstr = argv[0];
+ StringValue(rstr);
- result = MurmurHash3_x86_32(RSTRING_PTR(rstr), RSTRING_LEN(rstr), argc == 1 ? 0 : NUM2UINT(rseed));
+ result = MurmurHash3_x86_32(RSTRING_PTR(rstr), RSTRING_LEN(rstr), argc == 1 ? 0 : NUM2UINT(argv[1]));
return UINT2NUM(result);
}
static VALUE
rb_murmur3_32_int32_hash(int argc, VALUE* argv, VALUE self)
{
- VALUE rint, rseed;
+ VALUE rint;
uint32_t _int;
uint32_t result;
- rb_scan_args(argc, argv, "11", &rint, &rseed);
- _int = NUM2UINT(rint);
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (int32[, seed])");
+ }
+ _int = NUM2UINT(argv[0]);
- result = MurmurHash3_x86_32(&_int, 4, argc == 1 ? 0 : NUM2UINT(rseed));
+ result = MurmurHash3_x86_32(&_int, 4, argc == 1 ? 0 : NUM2UINT(argv[1]));
return UINT2NUM(result);
}
static VALUE
rb_murmur3_32_int64_hash(int argc, VALUE* argv, VALUE self)
{
- VALUE rint, rseed;
+ VALUE rint;
uint64_t _int;
uint32_t result;
- rb_scan_args(argc, argv, "11", &rint, &rseed);
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (int64[, seed])");
+ }
#if SIZEOF_LONG == 8
- _int = NUM2ULONG(rint);
+ _int = NUM2ULONG(argv[0]);
#else
- _int = NUM2ULL(rint);
+ _int = NUM2ULL(argv[0]);
#endif
- result = MurmurHash3_x86_32(&_int, 8, argc == 1 ? 0 : NUM2UINT(rseed));
+ result = MurmurHash3_x86_32(&_int, 8, argc == 1 ? 0 : NUM2UINT(argv[1]));
return UINT2NUM(result);
}
#define PREPARE_128_BIT() \
VALUE rstr, rseed, ar_result; \
uint32_t result[4]; \
- rb_scan_args(argc, argv, "11", &rstr, &rseed)
#define SWAP_128_BIT() do { \
uint32_t tmp; \
@@ -363,9 +370,16 @@ rb_murmur3_32_int64_hash(int argc, VALUE* argv, VALUE self)
static VALUE
rb_murmur3_128_str_hash(int argc, VALUE* argv, VALUE self)
{
- PREPARE_128_BIT();
+ VALUE rstr, ar_result;
+ uint32_t result[4];
+
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (string[, seed])");
+ }
+ rstr = argv[0];
+ StringValue(rstr);
- MurmurHash3_x64_128(RSTRING_PTR(rstr), RSTRING_LEN(rstr), argc == 1 ? 0 : NUM2UINT(rseed), result);
+ MurmurHash3_x64_128(RSTRING_PTR(rstr), RSTRING_LEN(rstr), argc == 1 ? 0 : NUM2UINT(argv[1]), result);
#if WORDS_BIGENDIAN
SWAP_128_BIT();
#endif
@@ -375,12 +389,14 @@ rb_murmur3_128_str_hash(int argc, VALUE* argv, VALUE self)
static VALUE
rb_murmur3_128_int32_hash(int argc, VALUE* argv, VALUE self)
{
- PREPARE_128_BIT();
+ VALUE ar_result;
+ uint32_t result[4], _int;
- {
- uint32_t _int = NUM2UINT(rstr);
- MurmurHash3_x64_128(&_int, 4, argc == 1 ? 0 : NUM2UINT(rseed), result);
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (int32[, seed])");
}
+ _int = NUM2UINT(argv[0]);
+ MurmurHash3_x64_128(&_int, 4, argc == 1 ? 0 : NUM2UINT(argv[1]), result);
#if WORDS_BIGENDIAN
SWAP_128_BIT();
#endif
@@ -390,16 +406,19 @@ rb_murmur3_128_int32_hash(int argc, VALUE* argv, VALUE self)
static VALUE
rb_murmur3_128_int64_hash(int argc, VALUE* argv, VALUE self)
{
- PREPARE_128_BIT();
+ VALUE ar_result;
+ uint32_t result[4];
+ uint64_t _int;
- {
+ if (argc == 0 || argc > 2) {
+ rb_raise(rb_eArgError, "accept 1 or 2 arguments: (int64[, seed])");
+ }
#if SIZEOF_LONG == 8
- uint64_t _int = NUM2ULONG(rstr);
+ _int = NUM2ULONG(argv[0]);
#else
- uint64_t _int = NUM2ULL(rstr);
+ _int = NUM2ULL(argv[0]);
#endif
- MurmurHash3_x64_128(&_int, 8, argc == 1 ? 0 : NUM2UINT(rseed), result);
- }
+ MurmurHash3_x64_128(&_int, 8, argc == 1 ? 0 : NUM2UINT(argv[1]), result);
#if WORDS_BIGENDIAN
SWAP_128_BIT();
#endif
View
2 lib/murmurhash3/version.rb
@@ -1,3 +1,3 @@
module MurmurHash3
- VERSION = "0.1.1.1"
+ VERSION = "0.1.2"
end

0 comments on commit 455d4d6

Please sign in to comment.