From 617e57fc75ed56e5880e64ba53b883de9dd82974 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 31 May 2009 15:02:14 -0500 Subject: [PATCH 1/2] cpu.x86.features: add RDTSC support. This is a new vocabulary with words: sse2? instruction-counter count-instructions --- basis/cpu/x86/32/32.factor | 5 +--- basis/cpu/x86/features/authors.txt | 1 + basis/cpu/x86/features/features-tests.factor | 7 ++++++ basis/cpu/x86/features/features.factor | 25 ++++++++++++++++++++ vm/cpu-x86.32.S | 4 ++++ vm/cpu-x86.64.S | 7 ++++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 basis/cpu/x86/features/authors.txt create mode 100644 basis/cpu/x86/features/features-tests.factor create mode 100644 basis/cpu/x86/features/features.factor diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index 0a0ac4a53e..95b65912d1 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -305,10 +305,7 @@ os windows? [ 4 "double" c-type (>>align) ] unless -FUNCTION: bool check_sse2 ( ) ; - -: sse2? ( -- ? ) - check_sse2 ; +USING: cpu.x86.features cpu.x86.features.private ; "-no-sse2" (command-line) member? [ [ { check_sse2 } compile ] with-optimizer diff --git a/basis/cpu/x86/features/authors.txt b/basis/cpu/x86/features/authors.txt new file mode 100644 index 0000000000..d4f5d6b3ae --- /dev/null +++ b/basis/cpu/x86/features/authors.txt @@ -0,0 +1 @@ +Slava Pestov \ No newline at end of file diff --git a/basis/cpu/x86/features/features-tests.factor b/basis/cpu/x86/features/features-tests.factor new file mode 100644 index 0000000000..69847cacfa --- /dev/null +++ b/basis/cpu/x86/features/features-tests.factor @@ -0,0 +1,7 @@ +IN: cpu.x86.features.tests +USING: cpu.x86.features tools.test kernel sequences math system ; + +cpu x86? [ + [ t ] [ sse2? { t f } member? ] unit-test + [ t ] [ [ 10000 [ ] times ] count-instructions integer? ] unit-test +] when \ No newline at end of file diff --git a/basis/cpu/x86/features/features.factor b/basis/cpu/x86/features/features.factor new file mode 100644 index 0000000000..bc4818d6af --- /dev/null +++ b/basis/cpu/x86/features/features.factor @@ -0,0 +1,25 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: system kernel math alien.syntax ; +IN: cpu.x86.features + + + +HOOK: sse2? cpu ( -- ? ) + +M: x86.32 sse2? check_sse2 ; + +M: x86.64 sse2? t ; + +HOOK: instruction-count cpu ( -- n ) + +M: x86 instruction-count read_timestamp_counter ; + +: count-instructions ( quot -- n ) + instruction-count [ call ] dip instruction-count swap - ; inline diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index afda9d31cd..1a9fd6165e 100755 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -55,6 +55,10 @@ DEF(bool,check_sse2,(void)): mov %edx,%eax ret +DEF(long long,read_timestamp_counter,(void)): + rdtsc + ret + DEF(void,primitive_inline_cache_miss,(void)): mov (%esp),%ebx DEF(void,primitive_inline_cache_miss_tail,(void)): diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index 8cf7423239..5cc3c98f33 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -72,6 +72,13 @@ DEF(void,set_callstack,(F_STACK_FRAME *to, F_STACK_FRAME *from, CELL length, voi call *ARG3 /* call memcpy */ ret /* return _with new stack_ */ +DEF(long long,read_timestamp_counter,(void)): + mov $0,%rax + rdtsc + shl $32,%rdx + or %rdx,%rax + ret + DEF(void,primitive_inline_cache_miss,(void)): mov (%rsp),%rbx DEF(void,primitive_inline_cache_miss_tail,(void)): From faaccceac65d71dabc5d6fc3da44b9efaa948bf2 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 31 May 2009 15:16:40 -0500 Subject: [PATCH 2/2] Fix rdtsc on Windows --- vm/cpu-x86.32.S | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 1a9fd6165e..a879712190 100755 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -73,4 +73,5 @@ DEF(void,primitive_inline_cache_miss_tail,(void)): #ifdef WINDOWS .section .drectve .ascii " -export:check_sse2" + .ascii " -export:read_timestamp_counter" #endif