Skip to content

Commit

Permalink
Some minor comment and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jul 16, 2024
1 parent b7a986a commit 4cbbd6b
Showing 1 changed file with 41 additions and 59 deletions.
100 changes: 41 additions & 59 deletions src/ddcpuid.d
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
/**
* x86 CPU Identification tool
*
* This was initially used internally, so it's pretty unfriendly.
*
* The best way to use this module would be:
* ---
* CPUINFO cpu; // CPUINFO.init or memset
* ddcpuid_leaves(cpu); // Get maximum CPUID leaves (mandatory step before info)
* ddcpuid_cpuinfo(cpu); // Fill CPUINFO structure (optional)
* ---
*
* Then checking the corresponding field:
* ---
* if (cpu.amx_xfd) {
* // Intel AMX with AMX_XFD is available
* }
* ---
*
* See the CPUINFO structure for available fields.
*
* To further understand these fields, it's encouraged to consult the technical manual.
*
* Authors: dd86k (dd@dax.moe)
* Copyright: © 2016-2023 dd86k
* License: MIT
*/
/// x86 CPU Identification tool
///
/// This was initially used internally, so it's pretty unfriendly.
///
/// The best way to use this module would be:
/// ---
/// CPUINFO cpu; // CPUINFO.init or memset
/// ddcpuid_leaves(cpu); // Get maximum CPUID leaves (mandatory step before info)
/// ddcpuid_cpuinfo(cpu); // Fill CPUINFO structure (optional)
/// ---
///
/// Then checking the corresponding field:
/// ---
/// if (cpu.amx_xfd) {
/// // Intel AMX with AMX_XFD is available
/// }
/// ---
///
/// See the CPUINFO structure for available fields.
///
/// To further understand these fields, it's encouraged to consult the technical manual.
///
/// Authors: dd86k (dd@dax.moe)
/// Copyright: © 2016-2023 dd86k
/// License: MIT
module ddcpuid;

// TODO: Reconsider bitflags, but with functions.
// Having functions like "bool SGX(CPUINFO)" and "CACHE* caches(CPUINFO)"
// would be more compact with bitflags internally than hundreds of bools.
// But for the purpose of this project, to be considered only.

// NOTE: GAS syntax crash course
// While ';' and '\n\t' are accepted, GNU typically recommends the
// latter for readability in output. (Don't know if this affects binaries)
Expand All @@ -48,11 +51,9 @@ module ddcpuid;
@system:
extern (C):

version (X86)
enum DDCPUID_PLATFORM = "i686"; /// Target platform
else version (X86_64)
enum DDCPUID_PLATFORM = "amd64"; /// Target platform
else static assert(0, "Unsupported platform");
version (X86) enum DDCPUID_PLATFORM = "i686"; /// Target platform
else version (X86_64) enum DDCPUID_PLATFORM = "amd64"; /// Target platform
else static assert(0, "Unsupported platform");

version (DigitalMars) {
version = DMD; // DMD compiler
Expand All @@ -61,7 +62,8 @@ version (DigitalMars) {
version = GDC; // GDC compiler
} else version (LDC) {
version = DMDLDC; // DMD or LDC compilers
} else static assert(0, "Unsupported compiler");
} else
static assert(0, "Unsupported compiler");

enum DDCPUID_VERSION = "0.21.1"; /// Library version
private enum CACHE_LEVELS = 6; /// For buffer
Expand Down Expand Up @@ -103,26 +105,10 @@ enum VirtVendor {

/// Registers structure used with the ddcpuid function.
struct REGISTERS {
union {
uint eax;
ushort ax;
struct { ubyte al, ah; }
}
union {
uint ebx;
ushort bx;
struct { ubyte bl, bh; }
}
union {
uint ecx;
ushort cx;
struct { ubyte cl, ch; }
}
union {
uint edx;
ushort dx;
struct { ubyte dl, dh; }
}
union { uint eax; ushort ax; struct { ubyte al, ah; } }
union { uint ebx; ushort bx; struct { ubyte bl, bh; } }
union { uint ecx; ushort cx; struct { ubyte cl, ch; } }
union { uint edx; ushort dx; struct { ubyte dl, dh; } }
}
///
@system unittest {
Expand Down Expand Up @@ -181,7 +167,6 @@ struct VendorString { align(1):
}
Vendor id; /// Validated vendor ID
}

@system unittest {
VendorString s;
s.string_ = "AuthenticAMD";
Expand All @@ -198,7 +183,6 @@ struct VirtVendorString { align(1):
}
VirtVendor id; /// Validated vendor ID
}

@system unittest {
VirtVendorString s;
s.string_ = "AuthenticAMD";
Expand Down Expand Up @@ -597,9 +581,8 @@ struct CPUINFO { align(1):
private bool __pad_10;
}

// EAX[4:0], 0-31, but there aren't that many
// So we limit it to 0-7
private enum CACHE_MASK = 7; // Max 31
// EAX[4:0], 0-31, but there aren't that many, so we limit up to 7
private enum CACHE_MASK = 7; // Usually max 31, but only seem 3 possible values
private immutable const(char)* CACHE_TYPE = "?DIU????";

private
Expand Down Expand Up @@ -987,7 +970,6 @@ void ddcpuid_strcpy48(ref char[48] dst, const(char) *src) {
}
}
private alias strcpy48 = ddcpuid_strcpy48;

@system unittest {
char[48] buffer = void;
strcpy48(buffer, "ea");
Expand Down

0 comments on commit 4cbbd6b

Please sign in to comment.