tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ppc.cpp (1673B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /* compile-time and runtime tests for whether to use Power ISA-specific
      6 * extensions */
      7 
      8 #include "ppc.h"
      9 
     10 #if defined(XP_LINUX)
     11 // Use the getauxval() function if available.
     12 // ARCH_3_00 wasn't defined until glibc 2.23, so include just in case.
     13 #  include <sys/auxv.h>
     14 #  ifndef PPC_FEATURE2_ARCH_3_00
     15 #    define PPC_FEATURE2_ARCH_3_00 0x00800000
     16 #  endif
     17 #endif
     18 
     19 const unsigned PPC_FLAG_VMX = 1;
     20 const unsigned PPC_FLAG_VSX = 2;
     21 const unsigned PPC_FLAG_VSX3 = 4;
     22 
     23 static signed get_ppc_cpu_flags(void) {
     24  // This could be expensive, so cache the result.
     25  static signed cpu_flags = -1;
     26 
     27  if (cpu_flags > -1) {  // already checked
     28    return cpu_flags;
     29  }
     30  cpu_flags = 0;
     31 
     32 #if defined(XP_LINUX)
     33  // Try getauxval().
     34  unsigned long int cap = getauxval(AT_HWCAP);
     35  unsigned long int cap2 = getauxval(AT_HWCAP2);
     36 
     37  if (cap & PPC_FEATURE_HAS_ALTIVEC) {
     38    cpu_flags |= PPC_FLAG_VMX;
     39  }
     40  if (cap & PPC_FEATURE_HAS_VSX) {
     41    cpu_flags |= PPC_FLAG_VSX;
     42  }
     43  if (cap2 & PPC_FEATURE2_ARCH_3_00) {
     44    cpu_flags |= PPC_FLAG_VSX3;
     45  }
     46 #else
     47  // Non-Linux detection here. Currently, on systems other than Linux,
     48  // no CPU SIMD features will be detected.
     49 #endif
     50 
     51  return cpu_flags;
     52 }
     53 
     54 namespace mozilla {
     55 namespace ppc_private {
     56 bool vmx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VMX);
     57 bool vsx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX);
     58 bool vsx3_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX3);
     59 }  // namespace ppc_private
     60 }  // namespace mozilla