From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alex Gramiak Newsgroups: gmane.emacs.devel Subject: Re: Using __builtin_expect (likely/unlikely macros) Date: Tue, 16 Apr 2019 09:47:57 -0600 Message-ID: <87sguic5oy.fsf@gmail.com> References: <87a7gst973.fsf@gmail.com> <875zrgt12q.fsf@gmail.com> <6919a4c8-df76-ea1e-34db-1fa62a360e5a@cs.ucla.edu> <87h8aykdod.fsf@gmail.com> <83tveyhe41.fsf@gnu.org> <871s22jyyo.fsf@gmail.com> <83bm16gejn.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="256216"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) Cc: eggert@cs.ucla.edu, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Apr 16 17:48:49 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGQKH-0014QT-5Q for ged-emacs-devel@m.gmane.org; Tue, 16 Apr 2019 17:48:49 +0200 Original-Received: from localhost ([127.0.0.1]:38728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGQKF-0007hH-UN for ged-emacs-devel@m.gmane.org; Tue, 16 Apr 2019 11:48:47 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:43933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGQK4-0007h0-MI for emacs-devel@gnu.org; Tue, 16 Apr 2019 11:48:37 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGQK2-0008FZ-T5 for emacs-devel@gnu.org; Tue, 16 Apr 2019 11:48:36 -0400 Original-Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:43809) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hGQK1-0008Ct-UR; Tue, 16 Apr 2019 11:48:34 -0400 Original-Received: by mail-pl1-x642.google.com with SMTP id n8so10523634plp.10; Tue, 16 Apr 2019 08:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=jLlEeWh/Mqlj7bFJNexBEQxspuub1MymJE6H28DRg10=; b=FegmzHOfPsLcy6xLzEmncGqx7otLFHqs5wpjaZLyHeqLeGNQ+KJpYgm7OuHQwErIyZ ONemwM2kohy8DKp3dg/Crsi4FukRteALZNEHQiJAbQTlXoRREDh68JJhDFvLNWejrlJ1 Gejry4/g/aZaXVnJlkFXtsUoY1muS6fMZUjBVQVQWDSHF4A7yPEd9SOi42S1y+8BsTik i7tuYneFuu4S8WtUHREWq5KsHhllkUR8kTFWzSeLosQOaoZoKSzH2eriBXwBnzW+nDXA HT4JawX3KRn6+ckaRl/+bfQ8dvmWAazFsov/bXB6TGUTasUZxMQ0NC4CjUx12Phjzgog Q5IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=jLlEeWh/Mqlj7bFJNexBEQxspuub1MymJE6H28DRg10=; b=WJ0URy6C9h81mLuQB+7xg8gh7gyo541F2mXZOtDHNmadX6bExhPv6yK4aTcW7Ph7RL uWonhLPBV069w2WI19pDGp0Y+iM8XqhVmmdFtZj1dn2Tet/gcUb6MVERK256fPlQRJNj ySmmkiGk1quhqpxoKueMSNliwponAmK6Newga58z/bp+PoGKveWlotYDZU6oPR7T5HwA 3zhfDp+TdAhfArWfVgO/ZuVzOP13ufb13niDiwQ/tTLxZv75zCt07ZORWbSreFlldICL XB4n2JLYqVZdfwbQebEiOdaXGuN5JDD65lQtvTxA9nC1UDAWLRuda0rXCAUEZHwjVsjI RRSA== X-Gm-Message-State: APjAAAUdM7XdOrPfUgw3a0tsDD30kywjroI917TnM931EVOgamCdapba 3jPptxYe2NBMhYKPlk20mHA4nzP6 X-Google-Smtp-Source: APXvYqxGhO+V4bW18PSJLS11SUDVunMr6I9D5Znxdb+I6atsnq0nSYvJinRQDxzl9YR0atKBE5nK6w== X-Received: by 2002:a17:902:521:: with SMTP id 30mr52904032plf.248.1555429711803; Tue, 16 Apr 2019 08:48:31 -0700 (PDT) Original-Received: from lylat ([2604:3d09:e37f:1500:1a72:4878:e793:7302]) by smtp.gmail.com with ESMTPSA id 129sm54733717pgc.24.2019.04.16.08.48.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Apr 2019 08:48:30 -0700 (PDT) In-Reply-To: <83bm16gejn.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 16 Apr 2019 18:23:08 +0300") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::642 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:235527 Archived-At: Eli Zaretskii writes: > OK, it's possible that I don't understand the exact semantics of > __builtin_expect in these situations. > > The problem is that you used LIKELY like this: > > if (A) > do_A; > else if (B) > do_B; > else if (C) > do_C; > else if (LIKELY (D)) > do_D; > else > cant_happen (); > > Essentially, the above is a moral equivalent of a 'switch' with the > 'default' case aborting because it "cannot happen". In such code, the > order of the clauses doesn't necessarily tell anything about their > likelihood; up front, they all are equally "likely". So using LIKELY > only in the last one sends a wrong signal: that last condition is > neither more nor less likely than all the others. Actually, in some > cases it might be _less_ likely than the preceding ones, because if I > knew that some of these conditions happens much more frequently, I'd > test it first. It was my understanding that since an else if is equivalent to else { if ... }, it would only affect the last two branches. Though I could easily be wrong here. > Now, it's possible that the effect of __builtin_expect doesn't care > about this issue. The GCC manual doesn't help to figure out whether > this is the case, because it only talks about a simple case of a > single 'if' clause, and doesn't tell any details about what GCC is > allowed to do when it sees __builtin_expect. But just by looking at > how the code looks, I immediately raised a brow. Right, considering the confusion it would be counterproductive to use them in this fashion. A workaround to the confusion would be to do: else { if (LIKELY (D)) do_D; else cant_happen (); }