[macruby-changes] [2023] MacRuby/branches/experimental
source_changes at macosforge.org
source_changes at macosforge.org
Tue Jul 14 16:48:55 PDT 2009
Revision: 2023
http://trac.macosforge.org/projects/ruby/changeset/2023
Author: pthomson at apple.com
Date: 2009-07-14 16:48:55 -0700 (Tue, 14 Jul 2009)
Log Message:
-----------
Implemented Fixnum#popcnt, a MacRuby extension to Fixnum that counts the number of set bits in a number.
Modified Paths:
--------------
MacRuby/branches/experimental/numeric.c
MacRuby/branches/experimental/spec/macruby/core/number_spec.rb
Modified: MacRuby/branches/experimental/numeric.c
===================================================================
--- MacRuby/branches/experimental/numeric.c 2009-07-14 20:14:45 UTC (rev 2022)
+++ MacRuby/branches/experimental/numeric.c 2009-07-14 23:48:55 UTC (rev 2023)
@@ -3113,6 +3113,12 @@
return Qfalse;
}
+static VALUE
+fix_popcnt(VALUE num, SEL sel)
+{
+ return INT2FIX(__builtin_popcountl(FIX2ULONG(num)));
+}
+
/*
* call-seq:
* fix.odd? -> true or false
@@ -3339,6 +3345,7 @@
rb_objc_define_method(rb_cFixnum, "odd?", fix_odd_p, 0);
rb_objc_define_method(rb_cFixnum, "even?", fix_even_p, 0);
rb_objc_define_method(rb_cFixnum, "succ", fix_succ, 0);
+ rb_objc_define_method(rb_cFixnum, "popcnt", fix_popcnt, 0);
rb_cFloat = rb_define_class("Float", rb_cNumeric);
Modified: MacRuby/branches/experimental/spec/macruby/core/number_spec.rb
===================================================================
--- MacRuby/branches/experimental/spec/macruby/core/number_spec.rb 2009-07-14 20:14:45 UTC (rev 2022)
+++ MacRuby/branches/experimental/spec/macruby/core/number_spec.rb 2009-07-14 23:48:55 UTC (rev 2023)
@@ -10,3 +10,13 @@
end
# TODO cover the Numeric interface on top of NSNumber
+
+describe "Fixnum#popcnt" do
+ it "counts the number of set bits in a number" do
+ 5.popcnt.should == 2
+ 0.popcnt.should == 0
+ 1024.popcnt.should == 1
+ # popcnt can, and should, vary depending on machine word size for negative numbers.
+ -1.popcnt.should <= 64
+ end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090714/fc99b19d/attachment.html>
More information about the macruby-changes
mailing list