mmx.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  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. **********************************************************************}
  11. { This unit contains some helpful stuff to deal with the mmx extensions }
  12. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit mmx;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. interface
  16. type
  17. tmmxshortint = array[0..7] of shortint;
  18. tmmxbyte = array[0..7] of byte;
  19. tmmxword = array[0..3] of word;
  20. tmmxinteger = array[0..3] of integer;
  21. tmmxlongint = array[0..1] of longint;
  22. tmmxcardinal = array[0..1] of cardinal;
  23. { for the AMD 3D }
  24. tmmxsingle = array[0..1] of single;
  25. pmmxshortint = ^tmmxshortint;
  26. pmmxbyte = ^tmmxbyte;
  27. pmmxword = ^tmmxword;
  28. pmmxinteger = ^tmmxinteger;
  29. {$ifdef HASFIXED}
  30. pmmxfixed = ^tmmxfixed;
  31. {$endif HASFIXED}
  32. pmmxlongint = ^tmmxlongint;
  33. pmmxcardinal = ^tmmxcardinal;
  34. { for the AMD 3D }
  35. pmmxsingle = ^tmmxsingle;
  36. const
  37. is_mmx_cpu : boolean = false;
  38. is_sse_cpu : boolean = false;
  39. is_sse2_cpu : boolean = false;
  40. is_amd_3d_cpu : boolean = false;
  41. is_amd_3d_dsp_cpu : boolean = false;
  42. is_amd_3d_mmx_cpu : boolean = false;
  43. { sets all floating point registers to empty
  44. (use this after mmx usage)
  45. }
  46. procedure emms;
  47. procedure femms;
  48. implementation
  49. {$IFDEF FPC_DOTTEDUNITS}
  50. uses
  51. System.CPU;
  52. {$ELSE FPC_DOTTEDUNITS}
  53. uses
  54. cpu;
  55. {$ENDIF FPC_DOTTEDUNITS}
  56. {$ASMMODE ATT}
  57. { return base type of processor: 0 - is Unknown, 10 - is AMD (AuthenticAMD), }
  58. { 20 - is Intel (GenuineIntel) }
  59. { 30 - is Hygon (HygonGenuine) }
  60. function getdevel:byte;
  61. var
  62. _ebx,_ecx,_edx : longint;
  63. begin
  64. getdevel:=0;
  65. if cpuid_support then
  66. with CPUID(0) do
  67. if ((ebx=$68747541) and (ecx=$444D4163) and (edx=$69746E65)) then getdevel:=10 else
  68. if ((ebx=$756E6547) and (ecx=$6C65746E) and (edx=$49656E69)) then getdevel:=20 else
  69. if ((ebx=$6f677948) and (ecx=$656e6975) and (edx=$6e65476e)) then getdevel:=30;
  70. end;
  71. { returns true, if the processor supports the mmx instructions }
  72. function mmx_support : boolean;
  73. begin
  74. { a cpu with without cpuid instruction supports never mmx }
  75. mmx_support:=MMXSupport;
  76. end;
  77. function amd_3d_support : boolean;
  78. begin
  79. { getdevel returns 0 if no cpuid_support; are there third party cpus supporting amd 3d instructions? }
  80. amd_3d_support:=(getdevel in [10,30]) and (CPUID($80000001).edx and (1 shl 31)<>0);
  81. end;
  82. function amd_3d_dsp_support : boolean;
  83. begin
  84. { getdevel returns 0 if no cpuid_support; are there third party cpus supporting amd dsp instructions? }
  85. amd_3d_dsp_support:=(getdevel in [10,30]) and (CPUID($80000001).edx and (1 shl 30)<>0);
  86. end;
  87. function amd_3d_mmx_support : boolean;
  88. begin
  89. { getdevel returns 0 if no cpuid_support; are there third party cpus supporting amd mmx instructions? }
  90. amd_3d_mmx_support:=(getdevel in [10,30]) and (CPUID($80000001).edx and (1 shl 22)<>0);
  91. end;
  92. procedure emms;assembler;
  93. asm
  94. { emms instruction not supported by older GNU as version,
  95. like 2.6 used by EMX target }
  96. // emms
  97. .byte 0x0f, 0x77
  98. end;
  99. procedure femms;assembler;
  100. asm
  101. { femms instruction not supported with older as versions }
  102. .byte 0x0f, 0x0e
  103. end;
  104. var
  105. oldexitproc : pointer;
  106. procedure mmxexitproc;
  107. begin
  108. exitproc:=oldexitproc;
  109. if is_amd_3d_cpu then femms else emms;
  110. end;
  111. begin
  112. if mmx_support then
  113. begin
  114. is_mmx_cpu:=true;
  115. if amd_3d_support then
  116. begin
  117. is_amd_3d_cpu:=true;
  118. is_amd_3d_dsp_cpu:=amd_3d_dsp_support;
  119. is_amd_3d_mmx_cpu:=amd_3d_mmx_support;
  120. end;
  121. is_sse_cpu:=has_sse_support;
  122. is_sse2_cpu:=has_sse2_support;
  123. { the exit code sets the fpu stack to empty }
  124. oldexitproc:=exitproc;
  125. exitproc:=@mmxexitproc;
  126. end;
  127. end.