Date: Tue, 11 Feb 2025 18:10:23 +0800 From: Zhenlei Huang <zlei@FreeBSD.org> To: Jessica Clarke <jrtc27@freebsd.org> Cc: Doug Moore <dougm@FreeBSD.org>, "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-branches@freebsd.org" <dev-commits-src-branches@FreeBSD.org> Subject: Re: git: 7bcc7a0b88cc - stable/14 - libkern: avoid local var in order_base_2() Message-ID: <5BF2F37C-16B4-4B39-B14B-131CE430BB4A@FreeBSD.org> In-Reply-To: <803D3DBC-A4F5-47FB-8034-5241461D4A71@freebsd.org> References: <202502101115.51ABFISj016298@gitrepo.freebsd.org> <858BD10B-A2C9-4D3A-9FF5-C5573597DF8B@FreeBSD.org> <803D3DBC-A4F5-47FB-8034-5241461D4A71@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] > On Feb 11, 2025, at 1:23 PM, Jessica Clarke <jrtc27@freebsd.org> wrote: > > On 11 Feb 2025, at 03:30, Zhenlei Huang <zlei@FreeBSD.org <mailto:zlei@FreeBSD.org>> wrote: >>> On Feb 10, 2025, at 7:15 PM, Doug Moore <dougm@FreeBSD.org> wrote: >>> >>> The branch stable/14 has been updated by dougm: >>> >>> URL: https://cgit.FreeBSD.org/src/commit/?id=7bcc7a0b88ccb5e1fe31de88ab9a17e46859318b >>> >>> commit 7bcc7a0b88ccb5e1fe31de88ab9a17e46859318b >>> Author: Doug Moore <dougm@FreeBSD.org> >>> AuthorDate: 2024-09-27 23:43:07 +0000 >>> Commit: Doug Moore <dougm@FreeBSD.org> >>> CommitDate: 2025-02-10 10:30:05 +0000 >>> >>> libkern: avoid local var in order_base_2() >>> >>> order_base_2(n) is implemented with a variable, which keeps it from >>> being used at file scope. Implement it instead as ilog2(2*n-1), which >>> produces a different result when 2*n overflows, which appears unlikely >>> in practice. >>> >>> Reviewed by: bz >>> Differential Revision: https://reviews.freebsd.org/D46826 >>> >>> (cherry picked from commit b7cbf741d55468ba34305a14ac3acc1c286af034) >>> --- >>> sys/sys/libkern.h | 5 +---- >>> 1 file changed, 1 insertion(+), 4 deletions(-) >>> >>> diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h >>> index a10289d72dc7..835e5ffaf0b7 100644 >>> --- a/sys/sys/libkern.h >>> +++ b/sys/sys/libkern.h >>> @@ -229,10 +229,7 @@ ilog2_long_long(long long n) >>> >>> #define ilog2(n) (__builtin_constant_p(n) ? ilog2_const(n) : ilog2_var(n)) >>> #define rounddown_pow_of_two(n) ((__typeof(n))1 << ilog2(n)) >>> -#define order_base_2(n) ({ \ >>> - __typeof(n) _n = (n); \ >> >> This local var `_n` is within the scope of the macro `order_base_2`, it is surrounded with >> {} and is harmless, so it will not pollute the file scoped variables. >> >> Am I reading the commit message wrong ? > > It’s not about pollution. GNU statement expressions are just not valid > at file scope, they must be used within a function (since otherwise the > implementation would have to have a full blown interpreter to constant > evaluate the whole thing, just like C++ and now C to some extent have > ended up with these days with constexpr). > > That is, you cannot write: > > int x = ({ ... }); > > at file scope. Ahh, I see. Thanks for the explaining! > > Jess [-- Attachment #2 --] <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Feb 11, 2025, at 1:23 PM, Jessica Clarke <<a href="mailto:jrtc27@freebsd.org" class="">jrtc27@freebsd.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta charset="UTF-8" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">On 11 Feb 2025, at 03:30, Zhenlei Huang <</span><a href="mailto:zlei@FreeBSD.org" style="font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">zlei@FreeBSD.org</a><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">> wrote:</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote type="cite" style="font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote type="cite" class="">On Feb 10, 2025, at 7:15 PM, Doug Moore <<a href="mailto:dougm@FreeBSD.org" class="">dougm@FreeBSD.org</a>> wrote:<br class=""><br class="">The branch stable/14 has been updated by dougm:<br class=""><br class="">URL: <a href="https://cgit.FreeBSD.org/src/commit/?id=7bcc7a0b88ccb5e1fe31de88ab9a17e46859318b" class="">https://cgit.FreeBSD.org/src/commit/?id=7bcc7a0b88ccb5e1fe31de88ab9a17e46859318b</a><br class=""><br class="">commit 7bcc7a0b88ccb5e1fe31de88ab9a17e46859318b<br class="">Author: Doug Moore <<a href="mailto:dougm@FreeBSD.org" class="">dougm@FreeBSD.org</a>><br class="">AuthorDate: 2024-09-27 23:43:07 +0000<br class="">Commit: Doug Moore <<a href="mailto:dougm@FreeBSD.org" class="">dougm@FreeBSD.org</a>><br class="">CommitDate: 2025-02-10 10:30:05 +0000<br class=""><br class=""> libkern: avoid local var in order_base_2()<br class=""><br class=""> order_base_2(n) is implemented with a variable, which keeps it from<br class=""> being used at file scope. Implement it instead as ilog2(2*n-1), which<br class=""> produces a different result when 2*n overflows, which appears unlikely<br class=""> in practice.<br class=""><br class=""> Reviewed by: bz<br class=""> Differential Revision: <a href="https://reviews.freebsd.org/D46826" class="">https://reviews.freebsd.org/D46826</a><br class=""><br class=""> (cherry picked from commit b7cbf741d55468ba34305a14ac3acc1c286af034)<br class="">---<br class="">sys/sys/libkern.h | 5 +----<br class="">1 file changed, 1 insertion(+), 4 deletions(-)<br class=""><br class="">diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h<br class="">index a10289d72dc7..835e5ffaf0b7 100644<br class="">--- a/sys/sys/libkern.h<br class="">+++ b/sys/sys/libkern.h<br class="">@@ -229,10 +229,7 @@ ilog2_long_long(long long n)<br class=""><br class="">#define ilog2(n) (__builtin_constant_p(n) ? ilog2_const(n) : ilog2_var(n))<br class="">#define rounddown_pow_of_two(n) ((__typeof(n))1 << ilog2(n))<br class="">-#define order_base_2(n) ({ \<br class="">- __typeof(n) _n = (n); \<br class=""></blockquote><br class="">This local var `_n` is within the scope of the macro `order_base_2`, it is surrounded with<br class="">{} and is harmless, so it will not pollute the file scoped variables.<br class=""><br class="">Am I reading the commit message wrong ?<br class=""></blockquote><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">It’s not about pollution. GNU statement expressions are just not valid</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">at file scope, they must be used within a function (since otherwise the</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">implementation would have to have a full blown interpreter to constant</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">evaluate the whole thing, just like C++ and now C to some extent have</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">ended up with these days with constexpr).</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">That is, you cannot write:</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class=""> int x = ({ ... });</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">at file scope.</span><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""></div></blockquote><div><br class=""></div><div>Ahh, I see. Thanks for the explaining!</div><br class=""><blockquote type="cite" class=""><div class=""><br style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Menlo-Regular; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Jess</span></div></blockquote></div><br class=""><div class=""> <div><br class=""></div> </div> <br class=""></body></html>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5BF2F37C-16B4-4B39-B14B-131CE430BB4A>
