Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2290474iof; Wed, 8 Jun 2022 01:34:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywuzcTJG4E2CHM4ymClxuSAnEjsEcyE77sllsK8dQFJutZTYJcZ6LamK+CheGnbxycxrrF X-Received: by 2002:a17:90b:3c6:b0:1e2:e9fc:4e79 with SMTP id go6-20020a17090b03c600b001e2e9fc4e79mr56610673pjb.192.1654677284247; Wed, 08 Jun 2022 01:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654677284; cv=none; d=google.com; s=arc-20160816; b=vVi02mNi7PufisDMxWudmjJx242NegSk95hQdLGuV5SnlZbvdrTXUh8H5aQIcrotXS QqcNrJiKbiuh90Gme4ZkptRSzO5J+THh+4/I7g5LOPUqmfJ8Y2/yLH0d8RRaJl6quoFd whx/Htx+7IunPx7sQoXXi5sTMqJtuMf0rnAVdOhvXTsb3o0vH+cSv9Eq8xsF1y8OJfbu MdgI9DWKZ/5sq6kh7J7O57tfR6Pq2pjktHUNAkRUK2uI4YE/nkL+H08L+IITx3feLZGL PAPKXL6i/5ax4MHNNyJS0MNwSkuefETSoch4p939V00cqHwb8Up54Iu4o60QuVYSorco 7qKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=+Aq0BEXW9Vx+qdJvDk57eAicNJ2BKjCbJLuilgiXECE=; b=bdKwFGOUqyhjtN01mwekX220GG6Gu37Jnb1ruNS1plUIAVBqK/6jETOZ9wB1gvU+78 acIu4bj+uvSuyqBsNdXCEutlLowAjpRPKfy/0qY8yVk+ApfY5mvmyn8IOIqIkIZKPwQ6 i75GRXyFI1tDnqjbb0lgdW2EuHm0KTLgVk2aAiHG0DIv/d9ZYNC0Xiu98JvJPdky+DrE l/NSbKGxSLYP0ZTzpsdBae8R4yhoPxFxeBmSL4Dz/744P8o9xSqE0IMetAPYH6FLfUzb G/SyV8gyHhoDYDzuDt/+X2u6kq7NWxDOWH3w14A1PqgFikWcVwWmxJ/IaWOhkdvg2CqO Nxiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=JhoGfEVi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id h5-20020a056a00000500b0051beefce3cbsi3152820pfk.63.2022.06.08.01.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 01:34:44 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=JhoGfEVi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2C98A1E8EB3; Wed, 8 Jun 2022 01:01:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240668AbiFHHEY (ORCPT + 99 others); Wed, 8 Jun 2022 03:04:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236354AbiFHGDz (ORCPT ); Wed, 8 Jun 2022 02:03:55 -0400 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A25347B491 for ; Tue, 7 Jun 2022 21:54:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=+Aq0BEXW9Vx+qdJvDk57eAicNJ2BKjCbJLuilgiXECE=; b=JhoGfEViWh+8/+pz1wq98sMeQv EpyDPOIMiUcj6oAzclYz7wHD+SJn5oDpUu1dWWNvi1W6fEAetRQejsyUxlXh2Kieu3Zg8VA5AdQO/ 1biPimiuOp6Rsf6q+aTCd3oGSvhtorbSo9INV3gSqR91Ytt6TN8f8C26dZ8lnjzVEsZ8PN+1JxJ3r uBVR95r4TED4GhuhGJNJXqU2BhIVT0wSO1gOoy8Rev+s/01ocQ0xxvRhc1a/DuTeWfKX9CY2i3ITq tW6fwDendyi3TRLkvx1K4hwV8QIbH4Z44/60n0zCtNdaBVweVcJC/W09Binc7n4X5NRICV7fFsEn+ vGC3VREw==; Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nyni9-004zX5-2C; Wed, 08 Jun 2022 04:54:29 +0000 Date: Wed, 8 Jun 2022 04:54:29 +0000 From: Al Viro To: Andrew Morton Cc: Nick Desaulniers , Justin Stitt , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Richard Smith Subject: Re: [PATCH] include/uapi/linux/swab.h: add __u16 cast to __swab16 conditional Message-ID: References: <20220607222006.22719-1-jstitt007@gmail.com> <20220607152744.d7c801d092529309500ac9a6@linux-foundation.org> <20220607162128.b5d4aa70f4a8a7610ce29250@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220607162128.b5d4aa70f4a8a7610ce29250@linux-foundation.org> Sender: Al Viro X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 07, 2022 at 04:21:28PM -0700, Andrew Morton wrote: > > 6.5.15/5 > > >> If both the second and third operands have arithmetic type, the result type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result. > > 6.3.1.8/1 > > >> Otherwise, the integer promotions are performed on both operands. > > 6.3.1.1/2 > > >> If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. > > Geeze. Can we please turn this into English and add it to the changelog? > > Is it saying that an expression > > int ? u16 : u16 > > has type int? Or something else? What did we do wrong here and is it > possible to correct our types rather than adding a cast? Not quite. Same rules as u16 + u16 - on architectures where int is wider than 16 bits it's (int)u16 + (int)u16 and yields int, on 16bit ones it's (unsigned int)u16 + (unsigned int)u16 and yields unsigned int. You *can't* get smaller-than-int out of ? :, same as you can't get it out of addition, etc. __builtin_choose_expr() would do it, but I would take a cast over that ugliness. FWIW, it might make sense for clang to keep track of the following property: expression has the same value as it would if integer promotions in it had been replaced with integer promotion of result. Example: with unsigned short x, y, mask; expresion "x & y" is interpreted as and_int((int)x, (int)y), which is equal to (int)and_u16(x, y), so that expression has the property in question. "x != 12 ? x : y" has the same property. "x + y", OTOH, doesn't - if x and y are both 32768, x + y is add_int((int)x, (int)y), i.e. 65536, while (int)add_u16(x, y) would be 0. For a somewhat more subtle example, (x & ~mask) | (y & mask) is interpreted as or_int(and_int((int)x, not_int((int)mask)), and_int((int)y, (int)mask)) which is equal to (int)or_u16(and_u16(x,not_u16(mask)), and_u16(y, mask)) IOW, the property in question holds for that one, despite having a subexpression (~mask) that does *NOT* have that property. (int)not_u16(0) is 0xffff and not_int((int)0) is (assuming 32bit int) 0xffffffff. Upper 16 bits get fouled; applying & with known-16bit launders them off... That predicate is behind the handling of small bitwise types in sparse; otherwise all operations on __be16 would trigger warnings due to promotions from __be16 to int. And aforementioned subtle example is common enough, so we had to deal with it. See commit d24967cb847b "[PATCH] handle fouled-bitwise" in sparse git...