x86.c 2.0 KB

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