Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp169997ybx; Wed, 30 Oct 2019 13:07:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqzAqKMHe6PbgzYBxafo4GjtuHbRNJ4qOXotxRpqharRXohT67wnJLM39RJgduZYq+5iom+s X-Received: by 2002:aa7:d8d0:: with SMTP id k16mr1265539eds.14.1572466047080; Wed, 30 Oct 2019 13:07:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572466047; cv=none; d=google.com; s=arc-20160816; b=hjecYsFqLKkwaSsD+X85C07JQ5YkOSnyASETCBysYERGO53Ql4YOPgyt+oz4aBBWxP wmUPN8T36XbjrrdBjvdQT5fyhcY2QVnFLy2h7QG1281lR+pADISxLMc3vuz5wHOMGrl/ B/s3XkMODNccnxNjs8/YCCNYYRetOidDgCdqfyLIAeWqsup1pgwylQDTyGoT3BRPPs9l Iyu35n79QzIi8WaB+dSHiGl62xL1VxUcRScMTcKnxrGAO5s3FAVxuMbdIhs14NiSvFJO cn6Y+o8Htd1zYGCi8VHkntH954PabfzT1c+IKpc1+MeFkdc4YLkhwVN0OzN4J5yYsLhM d75Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=B4MaiLsQCvLkOssBLa9fcFvK1YRehINVkEP+OJsdN4s=; b=IRIJhckPK+BtWS6NEY1r7KqPgiiNTK1BY7yVOrcHHewmo3IMMCvXPIj7T+ORe9ds6j McXRa0LAVodXGGqPsLcxBlF90Fmlwv+hxN46luKJ5Bc10EujYBxsuvM6rXoHR+uNsnzV LyChL1pdr+zM8jV0tQV/srEIhwtvfzl5/7NDwjN/z4JsLFgd8ANf//+5m7fO9yPH83YR //Bos+q+mx6jPe7x0a3p4Hqhb1I6jH5seEJU4jZeAo48KESG2Kuhaz4iZPpGRZvzC7v3 49/hAuQ0+ofKRsdgUAez+TQ5cir9WytpCIYGMLkz/o8R7K/faxkTsm1fdjCYbmKPAXSx Sm3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=HIj7hquE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w2si2126571edc.300.2019.10.30.13.07.01; Wed, 30 Oct 2019 13:07:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=HIj7hquE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726634AbfJ3UDF (ORCPT + 99 others); Wed, 30 Oct 2019 16:03:05 -0400 Received: from merlin.infradead.org ([205.233.59.134]:54700 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726261AbfJ3UDF (ORCPT ); Wed, 30 Oct 2019 16:03:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=B4MaiLsQCvLkOssBLa9fcFvK1YRehINVkEP+OJsdN4s=; b=HIj7hquEoNF9TkHFLwBPrIryd aAEkb4hPSn/FesggA5/E7GkDpT1+5w8luJJX6av7+U9on/YzxlbktgPXHQo54tjM0/9zUtZUqGYVG ozOEdGlGUDlNDwxJb1Vyqbxwa5dzHfiqsqk4j/7oS18xvJb940Ph3s0YnFii0trCJqGJ3cT6lFjRY slZKDuH1/OMIT21vGipeN+Lg4AFvCgw4yAaY2O4BqpC/MYhymWJBFrxTcEOGWMNTudgRvj/i7rheE FdvkepeN6HuO2U4giVu9TEuSJsNQHCA7HU823mkp5kdHlNwCtH7IjjKOa3b/I8qR/TcantTQ2gQkF rvAz3CyNw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=worktop.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1iPuAt-0000Aj-KO; Wed, 30 Oct 2019 20:02:35 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 1B17D980DEA; Wed, 30 Oct 2019 21:02:32 +0100 (CET) Date: Wed, 30 Oct 2019 21:02:32 +0100 From: Peter Zijlstra To: Bart Van Assche Cc: Ingo Molnar , Thomas Gleixner , Christoph Hellwig , "Martin K . Petersen" , linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler Subject: Re: [PATCH 4/9] drivers/iio: Sign extend without triggering implementation-defined behavior Message-ID: <20191030200232.GC3079@worktop.programming.kicks-ass.net> References: <20191028200700.213753-1-bvanassche@acm.org> <20191028200700.213753-5-bvanassche@acm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191028200700.213753-5-bvanassche@acm.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 28, 2019 at 01:06:55PM -0700, Bart Van Assche wrote: > From the C standard: "The result of E1 >> E2 is E1 right-shifted E2 bit > positions. If E1 has an unsigned type or if E1 has a signed type and a > nonnegative value, the value of the result is the integral part of the > quotient of E1 / 2E2 . If E1 has a signed type and a negative value, the > resulting value is implementation-defined." FWIW, we actually hard rely on this implementation defined behaviour all over the kernel. See for example the generic sign_extend{32,64}() functions. AFAIR the only reason the C standard says this is implementation defined is because it wants to support daft things like 1s complement and saturating integers. Luckily, Linux doesn't run on any such hardware and we hard rely on signed being 2s complement and tell the compiler that by using -fno-strict-overflow (which implies -fwrapv). And the only sane choice for 2s complement signed shift right is arithmetic shift right. (this recently came up in another thread, which I can't remember enough of to find just now, and I'm not sure we got a GCC person to confirm if -fwrapv does indeed guarantee arithmetic shift, the GCC documentation does not mention this)