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

ruby-changes:74126

From: Nobuyoshi <ko1@a...>
Date: Wed, 19 Oct 2022 22:56:01 +0900 (JST)
Subject: [ruby-changes:74126] 412e3c7a8d (master): Assert for RTEST that Qnil and Qfalse differ just 1 bit

https://git.ruby-lang.org/ruby.git/commit/?id=412e3c7a8d

From 412e3c7a8db275567eaceece6c48dde3aedf2ae6 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 19 Oct 2022 18:45:26 +0900
Subject: Assert for RTEST that Qnil and Qfalse differ just 1 bit

---
 include/ruby/internal/special_consts.h | 9 +++++++++
 ruby.c                                 | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/include/ruby/internal/special_consts.h b/include/ruby/internal/special_consts.h
index 38934e4da3..70276278eb 100644
--- a/include/ruby/internal/special_consts.h
+++ b/include/ruby/internal/special_consts.h
@@ -136,6 +136,7 @@ static inline bool https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/special_consts.h#L136
 RB_TEST(VALUE obj)
 {
     /*
+     * if USE_FLONUM
      *  Qfalse:  ....0000 0000
      *  Qnil:    ....0000 1000
      * ~Qnil:    ....1111 0111
@@ -143,6 +144,14 @@ RB_TEST(VALUE obj) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/special_consts.h#L144
      * ----------------------------
      *  RTEST(v) ....xxxx 0xxx
      *
+     * if ! USE_FLONUM
+     *  Qfalse:  ....0000 0000
+     *  Qnil:    ....0000 0100
+     * ~Qnil:    ....1111 1011
+     *  v        ....xxxx xxxx
+     * ----------------------------
+     *  RTEST(v) ....xxxx x0xx
+     *
      *  RTEST(v) can be 0 if and only if (v == Qfalse || v == Qnil).
      */
     return obj & ~RUBY_Qnil;
diff --git a/ruby.c b/ruby.c
index 0a3248f95f..03aeb9b75b 100644
--- a/ruby.c
+++ b/ruby.c
@@ -62,6 +62,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby.c#L62
 #include "ruby/version.h"
 #include "ruby/internal/error.h"
 
+#define singlebit_only_p(x) !((x) & ((x)-1))
+STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(Qnil^Qfalse));
+
 #ifndef MAXPATHLEN
 # define MAXPATHLEN 1024
 #endif
-- 
cgit v1.2.3


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

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