From: "H. Peter Anvin" <hpa@zytor.com>
To: mingo@kernel.org, linux-kernel@vger.kernel.org
Cc: Juergen Gross <jgross@suse.com>,
Stefano Stabellini <sstabellini@kernel.org>,
"Ahmed S . Darwish" <darwi@linutronix.de>,
Andrew Cooper <andrew.cooper3@citrix.com>,
John Ogness <john.ogness@linutronix.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Borislav Petkov <bp@alien8.de>,
Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH 4/5] x86/cpuid: Standardize on u32 in <asm/cpuid/api.h>
Date: Tue, 18 Mar 2025 12:08:56 -0700 [thread overview]
Message-ID: <a05422be-8043-409e-817d-3dec9571dd15@zytor.com> (raw)
In-Reply-To: <5D7935C3-8CAE-4821-8E31-A43169B8CB83@zytor.com>
[-- Attachment #1: Type: text/plain, Size: 516 bytes --]
On 3/18/25 11:48, H. Peter Anvin wrote:
>
> One more thing is that we ought to be able to make cpuid a const
> function, allowing the compiler to elide multiple calls. (Slight warning
> for feature-enabling MSRs changing CPUID), but that would require
> changing the API to returning a structure, since a pure or const
> structure can't return values by reference.
>
So I experimented (test included) and found that gcc 14.2 does not seem
to merge the cpuid calls in this code, whereas clang 19.1 does.
-hpa
[-- Attachment #2: cpuid.c --]
[-- Type: text/x-csrc, Size: 1262 bytes --]
#include <inttypes.h>
#include <stdio.h>
typedef uint32_t u32;
struct cpuid {
u32 ebx;
u32 edx;
u32 ecx;
u32 eax;
};
static inline __attribute__((const)) struct cpuid
cpuid(u32 leaf, u32 subleaf)
{
struct cpuid rv;
asm("cpuid"
: "=a" (rv.eax), "=c" (rv.ecx), "=d" (rv.edx), "=b" (rv.ebx)
: "a" (leaf), "c" (subleaf));
return rv;
}
static inline __attribute__((const)) u32
cpuid_eax(u32 leaf, u32 subleaf)
{
struct cpuid rv = cpuid(leaf, subleaf);
return rv.eax;
}
static inline __attribute__((const)) u32
cpuid_ecx(u32 leaf, u32 subleaf)
{
struct cpuid rv = cpuid(leaf, subleaf);
return rv.ecx;
}
static inline __attribute__((const)) u32
cpuid_edx(u32 leaf, u32 subleaf)
{
struct cpuid rv = cpuid(leaf, subleaf);
return rv.edx;
}
static inline __attribute__((const)) u32
cpuid_ebx(u32 leaf, u32 subleaf)
{
struct cpuid rv = cpuid(leaf, subleaf);
return rv.ebx;
}
u32 eax(u32 leaf, u32 subleaf)
{
return cpuid_eax(leaf, subleaf);
}
struct cpuid _cpuid(u32 leaf, u32 subleaf)
{
return cpuid(leaf, subleaf);
}
int test_cpuid(void)
{
int found = 0;
found += !!(cpuid_edx(1, 0) & (1 << 26)); /* SSE2 */
found += !!(cpuid_ecx(1, 0) & (1 << 0)); /* SSE3 */
found += !!(cpuid(1, 0).ecx & (1 << 9)); /* SSSE3 */
return found;
}
next prev parent reply other threads:[~2025-03-18 19:09 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-17 22:30 [PATCH 0/5] x86/cpu: Introduce <asm/cpuid/types.h> and <asm/cpuid/api.h> and clean them up mingo
2025-03-17 22:30 ` [PATCH 1/5] x86/cpuid: Refactor <asm/cpuid.h> mingo
2025-03-17 22:30 ` [PATCH 2/5] x86/cpuid: Clean up <asm/cpuid/types.h> mingo
2025-03-17 22:30 ` [PATCH 3/5] x86/cpuid: Clean up <asm/cpuid/api.h> mingo
2025-03-17 22:30 ` [PATCH 4/5] x86/cpuid: Standardize on u32 in <asm/cpuid/api.h> mingo
2025-03-18 18:48 ` H. Peter Anvin
2025-03-18 19:08 ` H. Peter Anvin [this message]
2025-03-18 19:09 ` Andrew Cooper
2025-03-18 19:25 ` H. Peter Anvin
2025-03-18 19:44 ` Andrew Cooper
2025-03-19 8:16 ` Ahmed S. Darwish
2025-03-17 22:30 ` [PATCH 5/5] x86/cpuid: Use u32 in instead of uint32_t " mingo
2025-03-17 22:49 ` [PATCH 0/5] x86/cpu: Introduce <asm/cpuid/types.h> and <asm/cpuid/api.h> and clean them up Linus Torvalds
2025-03-17 23:00 ` Ingo Molnar
2025-03-18 11:39 ` Ahmed S. Darwish
2025-03-18 11:55 ` Ingo Molnar
-- strict thread matches above, loose matches on Subject: below --
2025-03-17 22:18 Ingo Molnar
2025-03-17 22:18 ` [PATCH 4/5] x86/cpuid: Standardize on u32 in <asm/cpuid/api.h> Ingo Molnar
2025-03-18 5:59 ` Xin Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a05422be-8043-409e-817d-3dec9571dd15@zytor.com \
--to=hpa@zytor.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=darwi@linutronix.de \
--cc=jgross@suse.com \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=sstabellini@kernel.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®