From owner-svn-src-head@FreeBSD.ORG Fri Mar 30 12:11:13 2012 Return-Path: Delivered-To: svn-src-head@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A5E8F1065670; Fri, 30 Mar 2012 12:11:13 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from tensor.andric.com (cl-327.ede-01.nl.sixxs.net [IPv6:2001:7b8:2ff:146::2]) by mx1.freebsd.org (Postfix) with ESMTP id 626C08FC16; Fri, 30 Mar 2012 12:11:13 +0000 (UTC) Received: from [IPv6:2001:7b8:3a7:0:5513:9c82:30fc:da6f] (unknown [IPv6:2001:7b8:3a7:0:5513:9c82:30fc:da6f]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 9042B5C37; Fri, 30 Mar 2012 14:11:12 +0200 (CEST) Message-ID: <4F75A2E9.4040108@FreeBSD.org> Date: Fri, 30 Mar 2012 14:11:21 +0200 From: Dimitry Andric Organization: The FreeBSD Project User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120321 Thunderbird/12.0 MIME-Version: 1.0 To: Andrey Chernov , src-committers@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, svn-src-head@FreeBSD.ORG References: <201203292331.q2TNVmwN014920@svn.freebsd.org> <20120330082528.GA47173@vniz.net> In-Reply-To: <20120330082528.GA47173@vniz.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: 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:11:13 -0000 On 2012-03-30 10:25, 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). Yes, the arguments are all converted to unsigned types where necessary. The __bswapXX_gen() macros are only used internally by the __bswapXX() macros and the __bswapXX_var() inline functions. In case of the __bswapXX() macros, you can see that the argument to __bswapXX_gen() is first explicitly cast to an unsigned type, for example with __bswap32(): #define __bswap32(x) \ (__builtin_constant_p(x) ? \ __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x)) Therefore, right shifting will not give any problems. For the call to __bswap32_var(), such casting is not needed, since the argument will be implicitly converted to __uint32_t. As Bruce has mentioned, you could add more explicit casts and additional parentheses, but those would be superfluous.