Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp290568ybx; Wed, 30 Oct 2019 15:16:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6Pgckqt2xNmzniRmO7HYGcejW7wAYlIRmN7Mn7SjUcGAyQ1OwO1AVEbvlkraNivZXRf9/ X-Received: by 2002:aa7:c582:: with SMTP id g2mr2336484edq.25.1572473790198; Wed, 30 Oct 2019 15:16:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572473790; cv=none; d=google.com; s=arc-20160816; b=yKi/934wl7lYW2KAYl918s3IYcJFRA/DSFPHr54BrcpW9AX6IgXzs9t0/0r7y8k14q 5/a4fSmgm694wTmYcQ0w6ci2j/rlUduZg7LyT9ezHd19oxOsGLYEopwkI6Gs3Ih5fb7U Z4EH6hzIAD2vWsVPDvgzj0te1QMAvjDudAOJconuvAtlj9JXEAwVVzntaZtZlSauru9l cmRzARvIOf6p6OWl3RF/m7BDrv1qFPu2QW4hsVdZHW/LUgsscoq3o9/Tvc1EW6Fm/rDa HyRmW52ENSn7YXdMDYa2BV8/sr1QIsciRe7K6fHlA6txGcGT8AzIMeMyIeMQC/NElLR9 2s7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:reply-to; bh=C/5r1Jwfu5BrKkN6/uv30rR2cnKXdTGOw/E90QwOFCI=; b=rYJj7BvE2v4R0eNyICxg8v4faJIYIgVS4yIn06Hb9xTChdILkIUgj6AXjge6L1p3ZH R49xz4qoa9AUKsMwI1LlnuQ436iCr8kRUAxO/zGNLjF+6qpqUbU6RQRDREHIzsMiiZnD g1oR4Z71abxpScz4fiQRI2Vdv7SGU0EC3tD71U6ttHUfbLgDaTh20diCcKVxKElgkQUN uWqE1ZiNU2q+hADfkrRf7CuHX6AEpVh8Xz4GMdshDr43sse3dDRgQiWFEyCTd4CFLOSC NC4cUh3yfGbyouHl/4AfYHMN+41cTqbiD5aERSVIjkLdETssNBGtIxnauj5jjWgE7vaY hUOg== ARC-Authentication-Results: i=1; mx.google.com; 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 h50si2352057ede.423.2019.10.30.15.16.05; Wed, 30 Oct 2019 15:16:30 -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; 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 S1726667AbfJ3WNT (ORCPT + 99 others); Wed, 30 Oct 2019 18:13:19 -0400 Received: from smtp.infotech.no ([82.134.31.41]:56431 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726268AbfJ3WNT (ORCPT ); Wed, 30 Oct 2019 18:13:19 -0400 Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id 26C3E20418F; Wed, 30 Oct 2019 23:13:17 +0100 (CET) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qlYrrmIX-qhs; Wed, 30 Oct 2019 23:13:14 +0100 (CET) Received: from [192.168.48.23] (host-23-251-188-50.dyn.295.ca [23.251.188.50]) by smtp.infotech.no (Postfix) with ESMTPA id 62F13204164; Wed, 30 Oct 2019 23:13:12 +0100 (CET) Reply-To: dgilbert@interlog.com Subject: Re: [PATCH 4/9] drivers/iio: Sign extend without triggering implementation-defined behavior To: Peter Zijlstra , 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 References: <20191028200700.213753-1-bvanassche@acm.org> <20191028200700.213753-5-bvanassche@acm.org> <20191030200232.GC3079@worktop.programming.kicks-ass.net> From: Douglas Gilbert Message-ID: Date: Wed, 30 Oct 2019 18:13:11 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191030200232.GC3079@worktop.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019-10-30 4:02 p.m., Peter Zijlstra wrote: > 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. See: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2218.htm That is in C++20 and on the agenda for C2x: https://gustedt.wordpress.com/2018/11/12/c2x/ Doug Gilbert > 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) >