cpu.x86.features: Implement cpuid with help from joe. Add unit test.

db4
Doug Coleman 2012-09-18 18:40:29 -07:00
parent 8b49518349
commit 0d337e04ea
2 changed files with 38 additions and 6 deletions

View File

@ -1,7 +1,9 @@
USING: cpu.x86.features tools.test kernel sequences math math.order system ; USING: cpu.x86.features tools.test kernel sequences math math.order
strings system io.binary ;
IN: cpu.x86.features.tests IN: cpu.x86.features.tests
cpu x86? [ [ t ] [ sse-version 0 42 between? ] unit-test
[ t ] [ sse-version 0 42 between? ] unit-test [ t ] [ [ 10000 [ ] times ] count-instructions integer? ] unit-test
[ t ] [ [ 10000 [ ] times ] count-instructions integer? ] unit-test
] when { "GenuineIntel" }
[ 0 cpuid [ 4 >le ] map { 1 3 2 } swap nths concat >string ] unit-test

View File

@ -4,7 +4,8 @@ USING: accessors assocs sequences alien alien.c-types
combinators compiler compiler.codegen.labels compiler.units combinators compiler compiler.codegen.labels compiler.units
cpu.architecture cpu.x86.assembler cpu.x86.assembler.operands cpu.architecture cpu.x86.assembler cpu.x86.assembler.operands
init io kernel locals math math.order math.parser memoize init io kernel locals math math.order math.parser memoize
namespaces system ; namespaces system arrays specialized-arrays ;
SPECIALIZED-ARRAY: uint
IN: cpu.x86.features IN: cpu.x86.features
<PRIVATE <PRIVATE
@ -84,6 +85,35 @@ MEMO: sse-version ( -- n )
: sse4.1? ( -- ? ) sse-version 41 >= ; : sse4.1? ( -- ? ) sse-version 41 >= ;
: sse4.2? ( -- ? ) sse-version 42 >= ; : sse4.2? ( -- ? ) sse-version 42 >= ;
HOOK: (cpuid) cpu ( n regs -- )
M: x86.32 (cpuid) ( n regs -- )
void { uint void* } cdecl [
! Save ds-reg, rs-reg
EDI PUSH
EAX ESP 4 [+] MOV
CPUID
EDI ESP 8 [+] MOV
EDI [] EAX MOV
EDI 4 [+] EBX MOV
EDI 8 [+] ECX MOV
EDI 12 [+] EDX MOV
EDI POP
] alien-assembly ;
M: x86.64 (cpuid) ( n regs -- )
void { uint void* } cdecl [
RAX RDI MOV
CPUID
RSI [] EAX MOV
RSI 4 [+] EBX MOV
RSI 8 [+] ECX MOV
RSI 12 [+] EDX MOV
] alien-assembly ;
: cpuid ( n -- 4array )
4 <uint-array> [ (cpuid) ] keep >array ;
: popcnt? ( -- ? ) : popcnt? ( -- ? )
bool { } cdecl [ bool { } cdecl [
return-reg 1 MOV return-reg 1 MOV