From owner-freebsd-current@freebsd.org Thu Apr 29 21:08:08 2021 Return-Path: Delivered-To: freebsd-current@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B10AE636468 for ; Thu, 29 Apr 2021 21:08:08 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [88.99.82.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4FWSlC5ZvBz3GRJ; Thu, 29 Apr 2021 21:08:07 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2020.home.selasky.org (unknown [178.17.145.105]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id CBB412600C9; Thu, 29 Apr 2021 23:07:59 +0200 (CEST) To: FreeBSD Current , Konstantin Belousov From: Hans Petter Selasky Subject: Undefined compiler behaviour or a compiler bug? Message-ID: <59f81323-74c5-e6a8-a62d-6abdf78902e8@selasky.org> Date: Thu, 29 Apr 2021 23:06:42 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:78.0) Gecko/20100101 Thunderbird/78.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4FWSlC5ZvBz3GRJ X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of hps@selasky.org designates 88.99.82.50 as permitted sender) smtp.mailfrom=hps@selasky.org X-Spamd-Result: default: False [-2.28 / 15.00]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[88.99.82.50:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_ALLOW(-0.20)[+a:mail.turbocat.net]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[selasky.org]; ARC_NA(0.00)[]; SPAMHAUS_ZRD(0.00)[88.99.82.50:from:127.0.2.255]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; TO_DN_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCPT_COUNT_TWO(0.00)[2]; NEURAL_HAM_SHORT(-0.98)[-0.978]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:88.99.0.0/16, country:DE]; SUBJECT_ENDS_QUESTION(1.00)[]; MAILMAN_DEST(0.00)[freebsd-current]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Apr 2021 21:08:08 -0000 Hi, Can someone please explain what C-compiler flag I'm missing, to make this simple C-program terminate? I have a function _abs() which at some point is equal to one, but the compiler thinks it is not required to emit the test for the while() at all, looking at the assembly code! A printf() clearly verifies that the _abs() function has returned one. I even added an own if() check, but apparently the compiler is lost! cat << EOF > test.c #include #include typedef struct { int32_t x; int32_t y; } c32_t; static c32_t _mul(c32_t a, c32_t b) { return (c32_t){ a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x }; } static uint32_t _abs(c32_t a) { return (a.x * a.x + a.y * a.y); } int main() { c32_t a = {4, 1}; c32_t b = {}; uint32_t l1 = 0; while (_abs(a) != 1) { uint32_t l0 = _abs(a) & ~1; /* figure out least significant bit */ l0 &= ~(l0 - 1); if (l0 == 0 || l0 > l1) { l1 = l0; if (_abs(a) == 1) printf("VALUE IS ONE\n"); printf("Y:0x%08x ABS:0x%08x\n", a.y, _abs(a)); } a.y += 2; } size_t count = 0; do { count++; b = _mul(b, a); } while (b.x != 1 || b.y != 0); printf("%d,%d,0x%x COUNT=%zd\n", a.x, a.y, _abs(a), count); return (0); } EOF clang11 -O2 test.c && ./a.out --HPS