[前][次][番号順一覧][スレッド一覧]

ruby-changes:45462

From: naruse <ko1@a...>
Date: Sun, 5 Feb 2017 03:14:49 +0900 (JST)
Subject: [ruby-changes:45462] naruse:r57534 (trunk): fix description for current implementation

naruse	2017-02-05 03:14:42 +0900 (Sun, 05 Feb 2017)

  New Revision: 57534

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57534

  Log:
    fix description for current implementation

  Modified files:
    trunk/internal.h
Index: internal.h
===================================================================
--- internal.h	(revision 57533)
+++ internal.h	(revision 57534)
@@ -360,12 +360,24 @@ rb_fix_plus_fix(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/internal.h#L360
 #ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
     long lz;
     /* NOTE
-     * (1) Fixnum's LSB is always 1.
+     * (1) `LONG2FIX(FIX2LONG(x)+FIX2LONG(y))`
+     +     = `((lx*2+1)/2 + (ly*2+1)/2)*2+1`
+     +     = `lx*2 + ly*2 + 1`
+     +     = `(lx*2+1) + (ly*2+1) - 1`
+     +     = `x + y - 1`
+     * (2) Fixnum's LSB is always 1.
      *     It means you can always run `x - 1` without overflow.
-     * (2) Of course `z = x + (y-1)` may overflow.
-     *     Now z's LSB is always 1, and the MSB of true result is also 1.
-     *     You can get true result in long as `(1<<63)|(z>>1)`,
-     *     and it equals to `(z<<63)|(z>>1)` == `ror(z)`.
+     * (3) Of course `z = x + (y-1)` may overflow.
+     *     At that time true value is
+     *     * positive: 0b0 1xxx...1, and z = 0b1xxx...1
+     *     * nevative: 0b1 0xxx...1, and z = 0b0xxx...1
+     *     To convert this true value to long,
+     *     (a) Use arithmetic shift
+     *         * positive: 0b11xxx...
+     *         * negative: 0b00xxx...
+     *     (b) invert MSB
+     *         * positive: 0b01xxx...
+     *         * negative: 0b10xxx...
      */
     if (__builtin_add_overflow((long)x, (long)y-1, &lz)) {
 	return rb_int2big(rb_overflowed_fix_to_int(lz));

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]