From owner-freebsd-hackers@FreeBSD.ORG Fri Dec 5 18:27:46 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 36B371065673 for ; Fri, 5 Dec 2008 18:27:46 +0000 (UTC) (envelope-from neldredge@math.ucsd.edu) Received: from euclid.ucsd.edu (euclid.ucsd.edu [132.239.145.52]) by mx1.freebsd.org (Postfix) with ESMTP id 1299C8FC17 for ; Fri, 5 Dec 2008 18:27:45 +0000 (UTC) (envelope-from neldredge@math.ucsd.edu) Received: from zeno.ucsd.edu (zeno.ucsd.edu [132.239.145.22]) by euclid.ucsd.edu (8.11.7p3+Sun/8.11.7) with ESMTP id mB5IRh624264; Fri, 5 Dec 2008 10:27:43 -0800 (PST) Received: from localhost (neldredg@localhost) by zeno.ucsd.edu (8.11.7p3+Sun/8.11.7) with ESMTP id mB5IRhr02329; Fri, 5 Dec 2008 10:27:43 -0800 (PST) X-Authentication-Warning: zeno.ucsd.edu: neldredg owned process doing -bs Date: Fri, 5 Dec 2008 10:27:43 -0800 (PST) From: Nate Eldredge X-X-Sender: neldredg@zeno.ucsd.edu To: Garrett Cooper In-Reply-To: <7d6fde3d0812050131p2e9ac761n1c76575d3a3f5792@mail.gmail.com> Message-ID: References: <7d6fde3d0812040324y3bf0901cy1f4a6d961362c314@mail.gmail.com> <20081205072229.GE18652@hoeg.nl> <7d6fde3d0812050034y43a70ce8i49fbba92f9c8943b@mail.gmail.com> <7d6fde3d0812050035u6e3ea930o9e093830a8608444@mail.gmail.com> <20081205084441.GA29312@owl.midgard.homeip.net> <7d6fde3d0812050050l57684eebkf14f252d78b68ec0@mail.gmail.com> <4938F036.4010600@gmx.de> <7d6fde3d0812050131p2e9ac761n1c76575d3a3f5792@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: FreeBSD Hackers , Christoph Mallon , Maksim Yevmenkin Subject: Re: RFC: small syscons and kbd patch X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Dec 2008 18:27:46 -0000 On Fri, 5 Dec 2008, Garrett Cooper wrote: > On Fri, Dec 5, 2008 at 1:11 AM, Christoph Mallon > wrote: >> Garrett Cooper schrieb: >>> >>> (I feel like I'm getting off on a bikeshed topic, but...) >>> >>> 1. What dialect of C was it defined in? Is it still used in the >>> standard dialect (honestly, this is the first time I've ever seen it >>> before, but then again I am a younger generation user)? >> >> Dialect? The ! operator is plain vanilla standard C. It takes a scalar >> operand and returns 1, if it compares equal to 0, otherwise it returns 0. >> !!, i.e. two consecutive ! operators, is one of the oldest tricks in the >> book, right next to (a > b) - (a < b) for comparison functions and countless >> other idioms. >> >>> 3. What's the real loss of going to `? :', beyond maybe 3 extra >>> keystrokes if it's easier for folks who may not be as experienced to >>> read? >> >> I'd like my bikeshed grass green, please. >> >> Christoph > > If you really want to split hairs, ! only negates the logic value, > whereas ~ actually negates the bits. So technically, you're not > flipping 0 to make 1 and vice versa, but instead flipping 0 to make > non-zero, etc. There is a clear distinction in hardware. > > The point was that !! isn't obvious at first glancing the C code. It's > important for code to be readable as well as functional (that's why we > have style(9)). Getting down to it I'd like to see what the compiler > optimizes each as, because I can see dumb compilers saying `!!' > translates to `not, bne => set, else set, continue', whereas `? :' > could be translated to `bne, set, else set, continue'; I'm sure gcc > has moved passed these really minute details. Out of curiosity, I tried some various compilers, including gcc on i386, amd64, and sparc; Intel's C compiler on i386; tcc (tiny, non-optimizing C compiler) on i386; and Sun's compiler (old version) on sparc. I compiled the following file: int bangbang(int x) { return !!x; } int ternary(int x) { return x ? 1 : 0; } Intel's compiler generated different code for these two functions when optimization was turned off. bangbang used a conditional set instruction, while ternary used conditional jumps. With optimization on the two were identical. All other compilers generated identical code for the two functions whether optimization was on or off. (Of course, the generated code varied between compilers; tcc's in particular was decidedly non-optimized.) I really don't think something as simple as this is worth worrying about in terms of code efficiency. Even if they weren't identical, the difference is at most a couple of instructions and a pipeline flush, and if that's a serious problem you need to be using assembly anyway. Besides, it's not a piece of code that comes up all that often. The only basis for arguing about it is style, and I think we've established that it's purely a matter of taste. In particular, there isn't a clear favorite for which is easier to read. IMHO, style(9) should remain agnostic and let the programmer decide. However, if people really feel that consistency is necessary here, I propose the following: if the cents digit of the closing price of the Dow Jones Industrial Average on this coming Monday, December 8, 2008, is even, then style(9) shall be edited to indicate that `!!x' is preferred. If odd, then style(9) shall prefer `x ? 1 : 0'. :-) -- Nate Eldredge neldredge@math.ucsd.edu