From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Jens Bauer Newsgroups: gmane.lisp.guile.bugs Subject: bug#24630: guile-2.0.12: Comparison is always true warning - may cause problems. Date: Wed, 1 Mar 2017 12:34:56 +0100 Message-ID: <20170301123456521833.032250fd@plustv.dk> References: <20161006224920484742.8f069ce7@plustv.dk> <87mvd5icd7.fsf@pobox.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1488386181 21038 195.159.176.226 (1 Mar 2017 16:36:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 1 Mar 2017 16:36:21 +0000 (UTC) Cc: 24630-done@debbugs.gnu.org, Jens Bauer To: Andy Wingo Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Mar 01 17:36:16 2017 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cj7EX-0004HH-0E for guile-bugs@m.gmane.org; Wed, 01 Mar 2017 17:36:09 +0100 Original-Received: from localhost ([::1]:47550 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cj7Ec-0006s2-RK for guile-bugs@m.gmane.org; Wed, 01 Mar 2017 11:36:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59086) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cj7EV-0006rg-2h for bug-guile@gnu.org; Wed, 01 Mar 2017 11:36:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cj7EQ-00074u-VE for bug-guile@gnu.org; Wed, 01 Mar 2017 11:36:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:36422) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cj7EQ-00074W-Qo for bug-guile@gnu.org; Wed, 01 Mar 2017 11:36:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cj7EQ-00040M-Lh for bug-guile@gnu.org; Wed, 01 Mar 2017 11:36:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Jens Bauer Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 01 Mar 2017 16:36:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24630 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 24630-done@debbugs.gnu.org id=D24630.148838612515325 (code D ref 24630); Wed, 01 Mar 2017 16:36:02 +0000 Original-Received: (at 24630-done) by debbugs.gnu.org; 1 Mar 2017 16:35:25 +0000 Original-Received: from localhost ([127.0.0.1]:34617 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cj7Dm-0003z4-UN for submit@debbugs.gnu.org; Wed, 01 Mar 2017 11:35:25 -0500 Original-Received: from multitrading.dk ([92.246.25.51]:51034 helo=mail.multitrading.dk) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cj2XH-0002xY-J7 for 24630-done@debbugs.gnu.org; Wed, 01 Mar 2017 06:35:12 -0500 Original-Received: (qmail 63298 invoked from network); 1 Mar 2017 11:35:04 -0000 Original-Received: from multitrading.dk (HELO ?10.0.1.10?) (jb@multitrading.dk@92.246.25.51) by audiovideo.dk with ESMTPA; 1 Mar 2017 11:35:04 -0000 In-Reply-To: <87mvd5icd7.fsf@pobox.com> X-Mailer: GyazMail version 1.5.17 X-Mailman-Approved-At: Wed, 01 Mar 2017 11:35:22 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8633 Archived-At: Hi Andy. Thanks for looking into this. Your reply is correct and I can see that I made an error when manually reducing the expression. FIY: The warning shows up in GCC-4.2.1, not clang (I cannot run clang on any of my Macs, they're PowerPC based). -So the warning does show up when buiding using GCC-4.2.1. Love Jens On Wed, 01 Mar 2017 10:18:28 +0100, Andy Wingo wrote: > Hi, > > On Thu 06 Oct 2016 22:49, Jens Bauer writes: > >> I get the following warnings, when building on Mac OS X. >> (It should show up for all platforms, though): >> >> In file included from >> /Users/jens/open-source/Source/guile-2.0.12/libguile/numbers.c:9731: >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c: >> In function 'scm_to_int8': >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c:94: >> warning: comparison is always true due to limited range of data type >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c:94: >> warning: comparison is always true due to limited range of data type > > These are not really bugs. I mean, we shouldn't produce warnings, but > GCC doesn't warn on these, so clearly there is a heuristic which clang > has set differently; but the actual code is fine. > > In your investigations below there are some errors. I include a couple > of inline comments for your enjoyment. > >> In file included from >> /Users/jens/open-source/Source/guile-2.0.12/libguile/numbers.c:9747: >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c: >> In function 'scm_to_int16': >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c:94: >> warning: comparison is always true due to limited range of data type >> /Users/jens/open-source/Source/guile-2.0.12/libguile/conv-integer.i.c:94: >> warning: comparison is always true due to limited range of data type >> >> Notice that it's only from line 94, which reads... >> if (n >= TYPE_MIN && n <= TYPE_MAX) >> >> ... looking at the top of the file, it says: "It is only for signed >> types", so I look in ... >> >> numbers.c:9731 >> numbers.c:9747 >> ... which is int8 and int16 (signed integers); this should be as intended. >> >> The variable 'n' is declared as scm_t_signed_bits, which is a >> scm_t_intptr, which is an intptr_t, which is just a 'long'. >> >> So my guess is that the problem must be with TYPE_MIN and TYPE_MAX. >> >> In numbers.c, line 9742, they're defined as follows: >> #define TYPE scm_t_int16 >> #define TYPE_MIN SCM_T_INT16_MIN >> #define TYPE_MAX SCM_T_INT16_MAX >> >> ... looks good to me, but where's the definition of SCM_T_INT16_MIN >> and SCM_T_INT16_MAX ? >> -It seems to be in __scm.h: >> >> #define SCM_I_UTYPE_MAX(type) ((type)-1) >> #define SCM_I_TYPE_MAX(type,umax) ((type)((umax)/2)) >> #define SCM_I_TYPE_MIN(type,umax) (-((type)((umax)/2))-1) >> >> #define SCM_T_UINT8_MAX SCM_I_UTYPE_MAX(scm_t_uint8) >> #define SCM_T_INT8_MIN SCM_I_TYPE_MIN(scm_t_int8,SCM_T_UINT8_MAX) >> #define SCM_T_INT8_MAX SCM_I_TYPE_MAX(scm_t_int8,SCM_T_UINT8_MAX) >> >> #define SCM_T_UINT16_MAX SCM_I_UTYPE_MAX(scm_t_uint16) >> #define SCM_T_INT16_MIN SCM_I_TYPE_MIN(scm_t_int16,SCM_T_UINT16_MAX) >> #define SCM_T_INT16_MAX SCM_I_TYPE_MAX(scm_t_int16,SCM_T_UINT16_MAX) >> >> Now, this is where things get interesting. The macros are cool, but >> I think the use seems to be incorrect. >> >> Let's try an example (SCM_T_INT16_MIN): >> SCM_T_INT16_MIN = SCM_I_TYPE_MIN(scm_t_int16,SCM_T_UINT16_MAX) >> Expands to ... >> SCM_T_INT16_MIN = (-((scm_t_int16)((-1)/2))-1) > > SCM_T_UINT16_MAX expands to ((scm_t_uint16)-1) which expands to the > uint16_t value 0xffff. (These intermediate expansions have type in > addition to value.) SCM_T_INT16_MIN is -(0xffff/2)-1, which is > (int16_t)-0x8000. > >> ... which can be cleaned up ... >> >> SCM_T_INT16_MIN = (-(((-1)/2))-1) >> >> A signed integer of value -1 divided by 2, is the same as >> bitshifting to the right by using ASR; the result will be -1. >> >> SCM_T_INT16_MIN = (-(((-1)))-1) >> SCM_T_INT16_MIN = (-((-1))-1) >> SCM_T_INT16_MIN = (-(-1)-1) >> SCM_T_INT16_MIN = (+1-1) >> SCM_T_INT16_MIN = (0) >> >> ... Ehm ... Did I do something wrong ? >> I expected the value -32768, but got 0. >> >> Wouldn't it be correct to typecast as scm_t_uint16 instead of >> scm_t_int16 (and thus scm_t_uint8 instead of scm_t_int8) ? >> > > Happy hacking, > > Andy