Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp217696rwl; Thu, 30 Mar 2023 14:54:52 -0700 (PDT) X-Google-Smtp-Source: AKy350bMTEO2rpwDeA7tTHlRJKUAn91hNx1O0HLfS8xJSylKBOj/mEjJD11SiQLhLpS/PYOlWbU2 X-Received: by 2002:a17:90a:d58b:b0:23b:4d09:c166 with SMTP id v11-20020a17090ad58b00b0023b4d09c166mr20639410pju.4.1680213291880; Thu, 30 Mar 2023 14:54:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680213291; cv=none; d=google.com; s=arc-20160816; b=GaetL3u0UUV9zg7QpMjc4gh/eV+ceEi+8VoDe7+TCo704Syi+dpoKVyiaDieJXz1lf lhxpwrfXAkB/ALvKGnzXmjmC5sIVeJLB/39dL+pny9pKYKG8Qv2xPTPNC70z/402kWsg CD5OrlzCjBHimNW5Ue7GvsMVnrYa9frUf76HAY9DmlH+VCQ6BmEgUGM8z4TxCJxJmYSc atjA5JdeFTb25FVGa7LUkhuqM3ba70H8Pc7oYGf02KyJanFPc2f16cOJCDYj5WWdFpQt CaHaxCnNkHr3gUPNaRAcY8y/xlanebonKOiXDW/iz57j3T7KvVxzziqcJrxMYxyOqcHu xEqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=VzwhjG5h+PUjUpbJO6Ew/dSBff68LTyGFPjWG8fa+Cg=; b=C5h920YZGHyqCK5yIPgLbslCsi8b2FutByK+RCglXCQmLurPBlOyAz1ofKDu0K4O+m P9Qh9Y+m5IzbBwSuSEbvkzLbB99iLviVKUrK5u0QuR5ukCADv/IL9lp1Zuu0ZKcu0OTw NOjWDGBJzM0bisd/LS8wn0/2ks1Nyf+SqyHZ5GJd5+C2lsKVpBLtpb+/LMicTdKbrIBI JNEbWXJy1mP26OcCRSf/vCC2oZV3Hw8vfSH0WkXrglwqtJzQ8M87HJHPlPioUOkV8Cv1 Htrful9CPnIi+BrNoKmhWJ6qGXzg140GbMFQz2r/g/fS5aVsWlZR7HzYIcpy2/rZLyQL XIHg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p5-20020a170902e74500b0019f23a15f43si511000plf.578.2023.03.30.14.54.39; Thu, 30 Mar 2023 14:54:51 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230163AbjC3VxM convert rfc822-to-8bit (ORCPT + 99 others); Thu, 30 Mar 2023 17:53:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229676AbjC3VxL (ORCPT ); Thu, 30 Mar 2023 17:53:11 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 999A61042E for ; Thu, 30 Mar 2023 14:53:08 -0700 (PDT) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-13-XNRIvLOrOXesN_kJbVyF0A-1; Thu, 30 Mar 2023 22:53:05 +0100 X-MC-Unique: XNRIvLOrOXesN_kJbVyF0A-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 30 Mar 2023 22:53:04 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Thu, 30 Mar 2023 22:53:04 +0100 From: David Laight To: 'Andrew Morton' , Jani Nikula CC: "linux-kernel@vger.kernel.org" , "intel-gfx@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" , =?iso-8859-1?Q?Christian_K=F6nig?= , David Gow Subject: RE: [PATCH 0/4] log2: make is_power_of_2() more generic Thread-Topic: [PATCH 0/4] log2: make is_power_of_2() more generic Thread-Index: AQHZY0DwWI19nDCca0eIs2l7T1UA9K8T1qTw Date: Thu, 30 Mar 2023 21:53:03 +0000 Message-ID: <549987e4967d45159573901d330c96a0@AcuMS.aculab.com> References: <20230330104243.2120761-1-jani.nikula@intel.com> <20230330125041.83b0f39fa3a4ec1a42dfd95f@linux-foundation.org> In-Reply-To: <20230330125041.83b0f39fa3a4ec1a42dfd95f@linux-foundation.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=0.0 required=5.0 tests=PDS_BAD_THREAD_QP_64, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 From: Andrew Morton > Sent: 30 March 2023 20:51 > > On Thu, 30 Mar 2023 13:42:39 +0300 Jani Nikula wrote: > > > is_power_of_2() only works for types <= sizeof(unsigned long) and it's > > also not a constant expression. There are a number of places in kernel > > where is_power_of_2() is called on u64, which fails on 32-bit > > builds. Try to remedy that. While at it, make it a constant expression > > when possible. > > Yes, the current `is_power_of_2(unsigned long n)' isn't very general. > > But wouldn't all these issues be addressed by simply doing > > #define is_power_of_2(n) (n != 0 && ((n & (n - 1)) == 0)) > > ? > > (With suitable tweaks to avoid evaluating `n' more than once) I think you need to use the 'horrid tricks' from min() to get a constant expression from constant inputs. For non-constants this looks ok (see https://godbolt.org/z/G73MTr9jn) David static inline int lisp2(unsigned long n) { return n && !(n & (n - 1)); } static inline int llisp2(unsigned long long lln) { #if 0 // I think this looks worse, esp. for gcc on x86 return lln && !(lln & (lln - 1)); #else unsigned long n = lln; if (lln >= 1ull << 32) { if (n) return 0; n = lln >> 32; } return lisp2(n); #endif } #define isp2(n) (sizeof ((n)+0) == sizeof (long) ? lisp2(n) : llisp2(n)) int is12(unsigned long long i) { return isp2(i); } - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)