Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp783499rwe; Fri, 26 Aug 2022 14:36:32 -0700 (PDT) X-Google-Smtp-Source: AA6agR4q7orD0Q1BeIfnmp/iCTqorwPouOG10HPftWnVOU7hMj6ZtLb4DB6xpIb/qrXV0h8JTeKe X-Received: by 2002:a05:6a00:1145:b0:52b:78c:fa26 with SMTP id b5-20020a056a00114500b0052b078cfa26mr5730914pfm.27.1661549792061; Fri, 26 Aug 2022 14:36:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661549792; cv=none; d=google.com; s=arc-20160816; b=jPbX8uPFHszrRr258SngCB13HonKVolwU/wR8UlVR9RN3xMCn/UinKC5DJAvl8pgxD Z5y9ktO1Vq4N1/bqwt15/LwNCiGY+S3ZyHSJCipUqzR3UwRZemohN8eqK6tajyMe1Dpq Rsy3XUFuW0YRiindD256dvu+f10yxXgCvUx+acaGrIiCMK/eI3J1acttrYLQF0vyF1o3 bEMv9blTjN845LsTicNGgMMwDTJH7uwfU6qjw3EaaxaZ8/+c8Pv05I5Db7AiUC/kAL3O x3oCHUnke2el/GTfOTMSMQKml9zXs0iwyVz8A6UZTohEZwpNI6KS344CA3C8aNRUchLu TKzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=pRDA5k7yRN/3iQUSbdMhfkbAH0xEdHBx/uT17bZnTcc=; b=kyc0d7R5iAH4RInELw/A6oBZiyw7g+Cno0KgOndLSO6Ne66oH5FQNJRUn2ahwbBJVx 8n5YAz5YwW1Ozgxf6Ckulcyc9nkkHZ9cQOxA0FbaVQMwb6TNL3pmXFQ4V7T4rH/vTM+J fRMsTQZlDhYOo9JTQANMhFP1BT5A4EM95eQ2TCmsfkzkFvlOd4pLbaZUpFV6TP8fUVr2 Mae7r8Yxf6ZOjYsYqXHMAqWPOv/vY7XRXvJCV2H9MUrUySrSSGNUt5OkqQ13fItccUvd mP7qNFqh8dsAeY4PqBcbN1RbCOIH/qkh7KCImm6KEflisyXYeB2DUYvFMH38GwuhWQUI S9ZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oe12-20020a17090b394c00b001fabf75e140si2885990pjb.28.2022.08.26.14.36.21; Fri, 26 Aug 2022 14:36:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344328AbiHZVcW convert rfc822-to-8bit (ORCPT + 99 others); Fri, 26 Aug 2022 17:32:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241925AbiHZVcT (ORCPT ); Fri, 26 Aug 2022 17:32:19 -0400 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69F389BB5D for ; Fri, 26 Aug 2022 14:32:17 -0700 (PDT) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-334dc616f86so67154237b3.8 for ; Fri, 26 Aug 2022 14:32:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=i0/vsp+gUvOYuI/yixtkpRQ65mmMRu/3we2IfkmdpkY=; b=qujqa7Fo+CZIzAMqmRWJTjdzxS7/BhQO+/oDbVFgvDC7xR0ti1BffV+5eJ7IkV9pFf /Q+UYgG4Lc5Fly2kJv9/6jYGtbqaSi5YSs5PfmadlizztpC0jpLm71kUJYdot30TCiSC a9vUg74bWrFV5yrO2rsw8DADtLtbW5Ysd9QxHD+6mm0+Aduq+Sk5vGg6hW8EtdRRGtSW exZ8d329gFirRQjy6PSNopg+41l7bKufSjUkcyZp0tQvnMR+udY3SBlbPssrLGE7visn ZVUxRzO8j6oJYlB4FTY0RodNxft+BgN5WEAcmKqnMfUQ3ULe+25ZZG0ApEpdgM4no9hN n7vA== X-Gm-Message-State: ACgBeo0nuytvZ4yXteuTFUkxCA5z6mw0QgqA0SmsJvD2Z1y9XpwR+OiP vFkW46by6YUXkjOpk/kvqjDPMAtd+RFk7899h4Q= X-Received: by 2002:a25:becd:0:b0:690:a05c:8103 with SMTP id k13-20020a25becd000000b00690a05c8103mr1471661ybm.381.1661549536354; Fri, 26 Aug 2022 14:32:16 -0700 (PDT) MIME-Version: 1.0 References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> <20220812114438.1574-1-mailhol.vincent@wanadoo.fr> <20220812114438.1574-3-mailhol.vincent@wanadoo.fr> In-Reply-To: From: Vincent MAILHOL Date: Sat, 27 Aug 2022 06:32:05 +0900 Message-ID: Subject: Re: [PATCH v5 2/2] x86/asm/bitops: __ffs,ffz: use __builtin_ctzl to evaluate constant expressions To: Borislav Petkov Cc: Nick Desaulniers , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Peter Zijlstra , Dave Hansen , "H . Peter Anvin" , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, David Howells , Jan Beulich , Christophe Jaillet , Joe Perches , Josh Poimboeuf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, 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 Wed. 24 Aug. 2022 at 22:24, Borislav Petkov wrote: > On Wed, Aug 24, 2022 at 09:10:59PM +0900, Vincent MAILHOL wrote: > > Not exactly, this is TZCNT for x86_64 but for x86, it will be BSF… > > Not x86 - some old models which do not understand TZCNT, I'm being told. ACK. > And I'm being also told, "Intel and AMD disagree on what BSF does when > passed 0". So this is more mess. ACK. > > It means that __ffs() is not a x86_64 specific function. Each > > No, not that. The comment "Undefined if no bit exists". > > On my machine, __ffs(0) - the way it is implemented: > > rep; bsf %1,%0 > > is well-defined: > > "If the input operand is zero, CF is set to 1 and the size (in bits) of > the input operand is written to the destination register. Otherwise, CF > is cleared." It is well defined on *your* machine. On some other machines, it might be undefined: "If the content of the source operand is 0, the content of the destination operand is undefined." https://www.felixcloutier.com/x86/bsf > Leading to > > __ffs(0): 0x40 > > i.e., input operand of 64 bits. > > So on this particular x86 implementation, TZCNT(0) is well defined. It is here where I do not follow you. OK that on most of the recent machines, the compiler will emit a TZCNT and that this instruction is well defined for zero. But on some older machines, it will emit BSF, and on a subset of those machines, BSF(0) might be undefined. > So I'd like for that "undefined" thing to be expanded upon and > explained. Something along the lines of "the libc/compiler primitives' > *ffs(0) is undefined. Our inline asm helpers adhere to that behavior > even if the result they return for input operand of 0 is very well > defined." > > Now, if there are some machines which do not adhere to the current hw > behavior, then they should be ALTERNATIVEd. > > Better? > > > > Back to your patch: I think the text should be fixed to say that both > > > ffs() and __ffs()'s kernel implementation doesn't have undefined results > > > > NACK. __ffs(0) is an undefined behaviour (c.f. TZCNT instruction for > > NACK, SCHMACK. Read my mail again: "I think the text should be fixed". > The *text* - not __ffs(0) itself. The *text* should be fixed to explain > what undefined means. See above too. > > IOW, to start explaining this humongous mess I've scratched the surface > of. Agree that this is only the surface. But, my patch series is about constant folding, not about the text of *ffs(). Here, I just *move* the existing text, I did not modify anything. Can we agree that this is a separate topic? I do not think I am the good person to fix that mess (and in all honesty, I am not a domain expert in this domain and I am afraid I would just make you lose your time if I had to work on this). Yours sincerely, Vincent Mailhol