From owner-freebsd-current@freebsd.org Thu Apr 29 22:31:04 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 0F4905E0212 for ; Thu, 29 Apr 2021 22:31:04 +0000 (UTC) (envelope-from m.e.sanliturk@gmail.com) Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FWVZv0710z3Kww; Thu, 29 Apr 2021 22:31:02 +0000 (UTC) (envelope-from m.e.sanliturk@gmail.com) Received: by mail-lj1-x22b.google.com with SMTP id l22so71030649ljc.9; Thu, 29 Apr 2021 15:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9TM+9ElkU6cY/9Lv/fNFWyAwJLAgX0I+53DGZeYE2SQ=; b=rGE+Ah+0tFhuLbaUxhLt3uR0UrzWC7x6ZHZUeM81/HloXcSuix/5nmI2NOoIN2e5/b oXCA264QrIcdG2IPRmY2+lJ1FYf7/uOt5imcVcVdVSVi0ogR98+vj61oS4bItZHfVVtG RwIOhyktA/wbkwVYVehWFQ0+XLLNhr6RrPNYY2gZ6KKoGybC3z/EBjLxh1lw4dbkuCom i9+DB5zlAjmN1NagYfxQbIE/H3bPW1NZJKeAKmS2q0IzXceEnD5ryuCJvTfYKcicdZC1 uf2jXqZnZLxkt+S98rp7/DrjUjuoLKuJxn32nNlXfWlL+pY2d7i4dQdAkrkQHdkvMvhD U57Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9TM+9ElkU6cY/9Lv/fNFWyAwJLAgX0I+53DGZeYE2SQ=; b=LWFLnuWFLAKSYV2r74iZFIqVsZVmBGP4NFtqaUfVtJI9rPRgCjG+v37G27JkianUPn CkCU6hgJBAqBRqO5JQr7NIB62pG5ozAuJUHDkLYc+DbFRMgXXg7ivXtuqdfMAH8TfUDt LKCxjUXYOMtauGu0ShIeD9v1TJwSQ84law0WMYQguMvWter32nWR7/dr8f8uQBX/2sSg xGcBxpmUwODM1fcV+7DOJc2+acDC7bRLFqOejO9RZCFja8ZSde1UCqmxoJ069rMDG7WM i8ahINIYrsDHaWInx0JOBD6hbuBC0nfrQWFf8Hdjn5G19BdIucBaDjC1sKoBtNv4OyGx 9nNw== X-Gm-Message-State: AOAM530yVFt8UcT4ZSt6apusQm2en+kTVx9+khAnwhn8F7PR/voCvZVH uwrHV1GbCkyzlcZjeNRme9EoYuCp61jOMyF75kI= X-Google-Smtp-Source: ABdhPJzpEuU38+7XbXLL/EdIJoufMQ9YHKqqPNJ5BV4Frz9Ml+rM6gps0tm3MjRANiJsjZHZ7CFDXT3NmyAdX7vP57w= X-Received: by 2002:a2e:9957:: with SMTP id r23mr1458556ljj.306.1619735460785; Thu, 29 Apr 2021 15:31:00 -0700 (PDT) MIME-Version: 1.0 References: <59f81323-74c5-e6a8-a62d-6abdf78902e8@selasky.org> In-Reply-To: <59f81323-74c5-e6a8-a62d-6abdf78902e8@selasky.org> From: Mehmet Erol Sanliturk Date: Fri, 30 Apr 2021 01:30:23 +0300 Message-ID: Subject: Re: Undefined compiler behaviour or a compiler bug? To: Hans Petter Selasky Cc: FreeBSD Current , Konstantin Belousov X-Rspamd-Queue-Id: 4FWVZv0710z3Kww X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=rGE+Ah+0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of mesanliturk@gmail.com designates 2a00:1450:4864:20::22b as permitted sender) smtp.mailfrom=mesanliturk@gmail.com X-Spamd-Result: default: False [-3.00 / 15.00]; FREEMAIL_FROM(0.00)[gmail.com]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; RBL_DBL_DONT_QUERY_IPS(0.00)[2a00:1450:4864:20::22b:from]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; SUBJECT_ENDS_QUESTION(1.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; TAGGED_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; SPAMHAUS_ZRD(0.00)[2a00:1450:4864:20::22b:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::22b:from]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[freebsd-current] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.34 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 22:31:04 -0000 On Fri, Apr 30, 2021 at 12:08 AM Hans Petter Selasky wrote: > 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 > _______________________________________________ > > If _abs(a) is not an Integer , please do NOT use " equality" comparison with Integer 1 . Instead , please use "If abs ( ( _abs(a) - 1 ) ) is 'less than or equal to an Epsilon approximate to Zero' " , because equality of a floating point value to an integer value is approximately "Impossible" due to trailing bits unpredictability of floating point values . And do not rely on "printed values" of "floating point" values , because a value in memory and its printed values approximately are always different due to formatting specification . Mehmet Erol Sanliturk