cpu_unittest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "butil/cpu.h"
  5. #include "butil/build_config.h"
  6. #include <gtest/gtest.h>
  7. // Tests whether we can run extended instructions represented by the CPU
  8. // information. This test actually executes some extended instructions (such as
  9. // MMX, SSE, etc.) supported by the CPU and sees we can run them without
  10. // "undefined instruction" exceptions. That is, this test succeeds when this
  11. // test finishes without a crash.
  12. TEST(CPU, RunExtendedInstructions) {
  13. #if defined(ARCH_CPU_X86_FAMILY)
  14. // Retrieve the CPU information.
  15. butil::CPU cpu;
  16. // TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64.
  17. #if defined(OS_WIN) && !defined(_M_X64)
  18. ASSERT_TRUE(cpu.has_mmx());
  19. // Execute an MMX instruction.
  20. __asm emms;
  21. if (cpu.has_sse()) {
  22. // Execute an SSE instruction.
  23. __asm xorps xmm0, xmm0;
  24. }
  25. if (cpu.has_sse2()) {
  26. // Execute an SSE 2 instruction.
  27. __asm psrldq xmm0, 0;
  28. }
  29. if (cpu.has_sse3()) {
  30. // Execute an SSE 3 instruction.
  31. __asm addsubpd xmm0, xmm0;
  32. }
  33. if (cpu.has_ssse3()) {
  34. // Execute a Supplimental SSE 3 instruction.
  35. __asm psignb xmm0, xmm0;
  36. }
  37. if (cpu.has_sse41()) {
  38. // Execute an SSE 4.1 instruction.
  39. __asm pmuldq xmm0, xmm0;
  40. }
  41. if (cpu.has_sse42()) {
  42. // Execute an SSE 4.2 instruction.
  43. __asm crc32 eax, eax;
  44. }
  45. #elif defined(OS_POSIX) && defined(__x86_64__)
  46. ASSERT_TRUE(cpu.has_mmx());
  47. // Execute an MMX instruction.
  48. __asm__ __volatile__("emms\n" : : : "mm0");
  49. if (cpu.has_sse()) {
  50. // Execute an SSE instruction.
  51. __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
  52. }
  53. if (cpu.has_sse2()) {
  54. // Execute an SSE 2 instruction.
  55. __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
  56. }
  57. if (cpu.has_sse3()) {
  58. // Execute an SSE 3 instruction.
  59. __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0");
  60. }
  61. // NOTE(gejun): Not work in gcc 3.4
  62. #if !defined(COMPILER_GCC) || __GNUC__ >= 4
  63. if (cpu.has_ssse3()) {
  64. // Execute a Supplimental SSE 3 instruction.
  65. __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0");
  66. }
  67. if (cpu.has_sse41()) {
  68. // Execute an SSE 4.1 instruction.
  69. __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0");
  70. }
  71. if (cpu.has_sse42()) {
  72. // Execute an SSE 4.2 instruction.
  73. __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax");
  74. }
  75. #endif
  76. #endif
  77. #endif
  78. }