From owner-svn-src-head@FreeBSD.ORG Fri Mar 30 12:30:37 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7D955106566C; Fri, 30 Mar 2012 12:30:37 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail15.syd.optusnet.com.au (mail15.syd.optusnet.com.au [211.29.132.196]) by mx1.freebsd.org (Postfix) with ESMTP id 091468FC0A; Fri, 30 Mar 2012 12:30:36 +0000 (UTC) Received: from c211-30-171-136.carlnfd1.nsw.optusnet.com.au (c211-30-171-136.carlnfd1.nsw.optusnet.com.au [211.30.171.136]) by mail15.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id q2UCUXDl030124 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 30 Mar 2012 23:30:34 +1100 Date: Fri, 30 Mar 2012 23:30:33 +1100 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Andrey Chernov In-Reply-To: <20120330082528.GA47173@vniz.net> Message-ID: <20120330231216.G1071@besplex.bde.org> References: <201203292331.q2TNVmwN014920@svn.freebsd.org> <20120330082528.GA47173@vniz.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org, Dimitry Andric Subject: Re: svn commit: r233684 - head/sys/x86/include X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Mar 2012 12:30:37 -0000 On Fri, 30 Mar 2012, Andrey Chernov wrote: > On Thu, Mar 29, 2012 at 11:31:48PM +0000, Dimitry Andric wrote: >> However, the arguments are not properly masked, which results in the >> wrong value being calculated in some instances. For example, >> bswap32(0x12345678) returns 0x7c563412, and bswap64(0x123456789abcdef0) >> returns 0xfcdefc9a7c563412. > > Is sign extension considered in that place? Shifting any signed value to > ">>" direction (char, short, int, etc.) replicates sign bit, so cast to > corresponding unsigned value must be done first, which may take less > instructions, than masking (I am not sure about this part, just > guessing). Casting in that case applies to the argument (x) not to result > (x >> YY). There are lots of casts to uint* which are supposed to be sufficent, although some shortcuts are taken, especially in the 'gen' macros. The main thing to watch out for is C90's broken sign "value-preserving" promotion rule turning unsigned types into signed ones, so that sign extension bugs may occur later. >> #define __bswap16_gen(x) (__uint16_t)((x) << 8 | (x) >> 8) For example, this macro is private, and callers are required to know that its arg needs to be uint16_t or possibly smaller, and to not forget to cast to that if necessary. Then there are no problems evaluating ((x) << 8 | (x) >> 8), but it has type plain int. But we want the result to have type uint16_t and cast to that (this cast should probably be in callers too). So the plain int doesn't escape, but whenever the uint16_t is used, it gets promoted to plain int and its users should be careful with this. >> #define __bswap32_gen(x) \ >> - (((__uint32_t)__bswap16(x) << 16) | __bswap16((x) >> 16)) >> + (((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16)) Here the cast to uint32_t is because the caller _is_ being careful with this. If the expression were plain __bswap16((x) << 16, then when __bswap16() returns 0x8000, the shift gives (plain int)0x80000000 = -0x7fffffff - 1 with 32-bit ints. This would work in practice on normal 2's complement machines, but is unportable. Note that the result of the whole expression is not cast to uint32_t. We depend on ints being precisely 32 bits, so that the the result of the expression, which is either plain int or unsigned int (provided that ints have at least 32 bits with no padding bits), is in fact precisely uint32_t. This is another reason why casting the result of the gen macros belongs in callers. (We mostly don't cast, but use one to cast down the result of the conditional expression in the 16-bit case after the default promotions cast up to plain int. Omitting the corresponding cast for the other widths again depends on ints being 32 bits.) >> #define __bswap64_gen(x) \ >> - (((__uint64_t)__bswap32(x) << 32) | __bswap32((x) >> 32)) >> + (((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32)) Now we must cast up for the completely different reason that __bswap32() returns only uint32_t ints, and with 32 bit ints the implicit upwards conversion is null, but we need to shift to 64 bits, so we must start with at least 64 bits. >> >> #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P >> #define __bswap16(x) \ Bruce