File: client\x86.c
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20 #include <stdlib.h>
21 #include "client.h"
22
23 #if id386
24
25 static unsigned long bias;
26 static unsigned long *histogram;
27 static unsigned long start, range;
28 static unsigned long bias;
29
30 __declspec( naked ) void x86_TimerStart( void )
31 {
32 __asm _emit 0fh
33 __asm _emit 31h
34 __asm mov start, eax
35 __asm ret
36 }
37
38 __declspec( naked ) void x86_TimerStop( void )
39 {
40 __asm push edi
41 __asm mov edi, histogram
42 __asm _emit 0fh
43 __asm _emit 31h
44 __asm sub eax, start
45 __asm sub eax, bias
46 __asm js discard
47 __asm cmp eax, range
48 __asm jge discard
49 __asm lea edi, [edi + eax*4]
50 __asm inc dword ptr [edi]
51 discard:
52 __asm pop edi
53 __asm ret
54 }
55
56 #pragma warning( disable: 4035 )
57 static __declspec( naked ) unsigned long x86_TimerStopBias( void )
58 {
59 __asm push edi
60 __asm mov edi, histogram
61 __asm _emit 0fh
62 __asm _emit 31h
63 __asm sub eax, start
64 __asm pop edi
65 __asm ret
66 }
67 #pragma warning( default:4035 )
68
69 void x86_TimerInit( unsigned long smallest, unsigned length )
70 {
71 int i;
72 unsigned long biastable[100];
73
74 range = length;
75 bias = 10000;
76
77 for ( i = 0; i < 100; i++ )
78 {
79 x86_TimerStart();
80 biastable[i] = x86_TimerStopBias();
81
82 if ( bias > biastable[i] )
83 bias = biastable[i];
84 }
85
86 bias += smallest;
87 histogram = Z_Malloc( range * sizeof( unsigned long ) );
88 }
89
90 unsigned long *x86_TimerGetHistogram( void )
91 {
92 return histogram;
93 }
94 95 #endif
96