1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.data arrays assocs combinators
4 compiler.codegen.labels cpu.architecture cpu.x86.assembler
5 cpu.x86.assembler.operands init kernel math math.order
6 math.parser memoize namespaces sequences
7 specialized-arrays system math.bitwise combinators.smart ;
8 SPECIALIZED-ARRAY: uint
13 : return-reg ( -- reg ) int-regs return-regs at first ;
15 : (sse-version) ( -- n )
50 "sse-42" resolve-label
54 "sse-41" resolve-label
58 "ssse-3" resolve-label
78 MEMO: sse-version ( -- n )
79 (sse-version) "sse-version" get string>number [ min ] when* ;
81 : sse? ( -- ? ) sse-version 10 >= ;
82 : sse2? ( -- ? ) sse-version 20 >= ;
83 : sse3? ( -- ? ) sse-version 30 >= ;
84 : ssse3? ( -- ? ) sse-version 33 >= ;
85 : sse4.1? ( -- ? ) sse-version 41 >= ;
86 : sse4.2? ( -- ? ) sse-version 42 >= ;
88 HOOK: (cpuid) cpu ( rax rcx regs -- )
91 : cpuid-extended ( rax rcx -- 4array )
92 4 uint <c-array> [ (cpuid) ] keep >array ;
94 : cpuid ( rax -- 4array ) 0 cpuid-extended ;
96 : cpuid-processor-info ( -- eax ) 1 cpuid first ; inline
98 : parse-stepping ( eax -- n ) 3 0 bit-range ; inline
99 : parse-model ( eax -- n ) 7 4 bit-range ; inline
100 : parse-family ( eax -- n ) 11 8 bit-range ; inline
101 : parse-processor-type ( eax -- n ) 13 12 bit-range ; inline
102 : parse-extended-model ( eax -- n ) 19 16 bit-range ; inline
103 : parse-extended-family ( eax -- n ) 27 20 bit-range ; inline
105 : cpu-stepping ( -- n ) cpuid-processor-info parse-stepping ;
106 : cpu-model ( -- n ) cpuid-processor-info parse-model ;
107 : cpu-family ( -- n ) cpuid-processor-info parse-family ;
108 : cpu-processor-type ( -- n ) cpuid-processor-info parse-processor-type ;
109 : cpu-extended-model ( -- n ) cpuid-processor-info parse-extended-model ;
110 : cpu-extended-family ( -- n ) cpuid-processor-info parse-extended-family ;
112 : cpu-family-model-string ( -- string )
114 cpuid-processor-info {
115 [ parse-extended-family >hex ]
116 [ parse-family >hex ]
118 [ parse-extended-model >hex ]
121 ] "" append-outputs-as ;
132 : tscdeadline? ( -- ? ) 1 cpuid third 24 bit? ;
133 : aes? ( -- ? ) 1 cpuid third 25 bit? ;
134 : xsave? ( -- ? ) 1 cpuid third 26 bit? ;
135 : osxsave? ( -- ? ) 1 cpuid third 27 bit? ;
136 : avx? ( -- ? ) 1 cpuid third 28 bit? ;
137 : f16c? ( -- ? ) 1 cpuid third 29 bit? ;
138 : rdrand? ( -- ? ) 1 cpuid third 30 bit? ;
140 : msr? ( -- ? ) 1 cpuid fourth 5 bit? ;
141 : tm1? ( -- ? ) 1 cpuid fourth 29 bit? ;
142 : tm2? ( -- ? ) 1 cpuid third 8 bit? ;
160 ulonglong { } cdecl [
164 MEMO: enable-popcnt? ( -- ? )
165 popcnt? "disable-popcnt" get not and ;
168 { sse-version enable-popcnt? } [ reset-memoized ] each
171 : sse-string ( version -- string )
182 HOOK: instruction-count cpu ( -- n )
184 M: x86.32 instruction-count
189 M: x86.64 instruction-count
197 : count-instructions ( quot -- n )
198 instruction-count [ call instruction-count ] dip - ; inline