cpu-features.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  18. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #ifndef CPU_FEATURES_H
  29. #define CPU_FEATURES_H
  30. #include <sys/cdefs.h>
  31. #include <stdint.h>
  32. __BEGIN_DECLS
  33. typedef enum {
  34. ANDROID_CPU_FAMILY_UNKNOWN = 0,
  35. ANDROID_CPU_FAMILY_ARM,
  36. ANDROID_CPU_FAMILY_X86,
  37. ANDROID_CPU_FAMILY_MIPS,
  38. ANDROID_CPU_FAMILY_ARM64,
  39. ANDROID_CPU_FAMILY_X86_64,
  40. ANDROID_CPU_FAMILY_MIPS64,
  41. ANDROID_CPU_FAMILY_MAX /* do not remove */
  42. } AndroidCpuFamily;
  43. /* Return family of the device's CPU */
  44. extern AndroidCpuFamily android_getCpuFamily(void);
  45. /* The list of feature flags for ARM CPUs that can be recognized by the
  46. * library. Value details are:
  47. *
  48. * VFPv2:
  49. * CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs
  50. * support these instructions. VFPv2 is a subset of VFPv3 so this will
  51. * be set whenever VFPv3 is set too.
  52. *
  53. * ARMv7:
  54. * CPU supports the ARMv7-A basic instruction set.
  55. * This feature is mandated by the 'armeabi-v7a' ABI.
  56. *
  57. * VFPv3:
  58. * CPU supports the VFPv3-D16 instruction set, providing hardware FPU
  59. * support for single and double precision floating point registers.
  60. * Note that only 16 FPU registers are available by default, unless
  61. * the D32 bit is set too. This feature is also mandated by the
  62. * 'armeabi-v7a' ABI.
  63. *
  64. * VFP_D32:
  65. * CPU VFP optional extension that provides 32 FPU registers,
  66. * instead of 16. Note that ARM mandates this feature is the 'NEON'
  67. * feature is implemented by the CPU.
  68. *
  69. * NEON:
  70. * CPU FPU supports "ARM Advanced SIMD" instructions, also known as
  71. * NEON. Note that this mandates the VFP_D32 feature as well, per the
  72. * ARM Architecture specification.
  73. *
  74. * VFP_FP16:
  75. * Half-width floating precision VFP extension. If set, the CPU
  76. * supports instructions to perform floating-point operations on
  77. * 16-bit registers. This is part of the VFPv4 specification, but
  78. * not mandated by any Android ABI.
  79. *
  80. * VFP_FMA:
  81. * Fused multiply-accumulate VFP instructions extension. Also part of
  82. * the VFPv4 specification, but not mandated by any Android ABI.
  83. *
  84. * NEON_FMA:
  85. * Fused multiply-accumulate NEON instructions extension. Optional
  86. * extension from the VFPv4 specification, but not mandated by any
  87. * Android ABI.
  88. *
  89. * IDIV_ARM:
  90. * Integer division available in ARM mode. Only available
  91. * on recent CPUs (e.g. Cortex-A15).
  92. *
  93. * IDIV_THUMB2:
  94. * Integer division available in Thumb-2 mode. Only available
  95. * on recent CPUs (e.g. Cortex-A15).
  96. *
  97. * iWMMXt:
  98. * Optional extension that adds MMX registers and operations to an
  99. * ARM CPU. This is only available on a few XScale-based CPU designs
  100. * sold by Marvell. Pretty rare in practice.
  101. *
  102. * If you want to tell the compiler to generate code that targets one of
  103. * the feature set above, you should probably use one of the following
  104. * flags (for more details, see technical note at the end of this file):
  105. *
  106. * -mfpu=vfp
  107. * -mfpu=vfpv2
  108. * These are equivalent and tell GCC to use VFPv2 instructions for
  109. * floating-point operations. Use this if you want your code to
  110. * run on *some* ARMv6 devices, and any ARMv7-A device supported
  111. * by Android.
  112. *
  113. * Generated code requires VFPv2 feature.
  114. *
  115. * -mfpu=vfpv3-d16
  116. * Tell GCC to use VFPv3 instructions (using only 16 FPU registers).
  117. * This should be generic code that runs on any CPU that supports the
  118. * 'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this.
  119. *
  120. * Generated code requires VFPv3 feature.
  121. *
  122. * -mfpu=vfpv3
  123. * Tell GCC to use VFPv3 instructions with 32 FPU registers.
  124. * Generated code requires VFPv3|VFP_D32 features.
  125. *
  126. * -mfpu=neon
  127. * Tell GCC to use VFPv3 instructions with 32 FPU registers, and
  128. * also support NEON intrinsics (see <arm_neon.h>).
  129. * Generated code requires VFPv3|VFP_D32|NEON features.
  130. *
  131. * -mfpu=vfpv4-d16
  132. * Generated code requires VFPv3|VFP_FP16|VFP_FMA features.
  133. *
  134. * -mfpu=vfpv4
  135. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features.
  136. *
  137. * -mfpu=neon-vfpv4
  138. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA
  139. * features.
  140. *
  141. * -mcpu=cortex-a7
  142. * -mcpu=cortex-a15
  143. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|
  144. * NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2
  145. * This flag implies -mfpu=neon-vfpv4.
  146. *
  147. * -mcpu=iwmmxt
  148. * Allows the use of iWMMXt instrinsics with GCC.
  149. */
  150. enum {
  151. ANDROID_CPU_ARM_FEATURE_ARMv7 = (1 << 0),
  152. ANDROID_CPU_ARM_FEATURE_VFPv3 = (1 << 1),
  153. ANDROID_CPU_ARM_FEATURE_NEON = (1 << 2),
  154. ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3),
  155. ANDROID_CPU_ARM_FEATURE_VFPv2 = (1 << 4),
  156. ANDROID_CPU_ARM_FEATURE_VFP_D32 = (1 << 5),
  157. ANDROID_CPU_ARM_FEATURE_VFP_FP16 = (1 << 6),
  158. ANDROID_CPU_ARM_FEATURE_VFP_FMA = (1 << 7),
  159. ANDROID_CPU_ARM_FEATURE_NEON_FMA = (1 << 8),
  160. ANDROID_CPU_ARM_FEATURE_IDIV_ARM = (1 << 9),
  161. ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10),
  162. ANDROID_CPU_ARM_FEATURE_iWMMXt = (1 << 11),
  163. };
  164. enum {
  165. ANDROID_CPU_X86_FEATURE_SSSE3 = (1 << 0),
  166. ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1),
  167. ANDROID_CPU_X86_FEATURE_MOVBE = (1 << 2),
  168. };
  169. extern uint64_t android_getCpuFeatures(void);
  170. #define android_getCpuFeaturesExt android_getCpuFeatures
  171. /* Return the number of CPU cores detected on this device. */
  172. extern int android_getCpuCount(void);
  173. /* The following is used to force the CPU count and features
  174. * mask in sandboxed processes. Under 4.1 and higher, these processes
  175. * cannot access /proc, which is the only way to get information from
  176. * the kernel about the current hardware (at least on ARM).
  177. *
  178. * It _must_ be called only once, and before any android_getCpuXXX
  179. * function, any other case will fail.
  180. *
  181. * This function return 1 on success, and 0 on failure.
  182. */
  183. extern int android_setCpu(int cpu_count,
  184. uint64_t cpu_features);
  185. #ifdef __arm__
  186. /* Retrieve the ARM 32-bit CPUID value from the kernel.
  187. * Note that this cannot work on sandboxed processes under 4.1 and
  188. * higher, unless you called android_setCpuArm() before.
  189. */
  190. extern uint32_t android_getCpuIdArm(void);
  191. /* An ARM-specific variant of android_setCpu() that also allows you
  192. * to set the ARM CPUID field.
  193. */
  194. extern int android_setCpuArm(int cpu_count,
  195. uint64_t cpu_features,
  196. uint32_t cpu_id);
  197. #endif
  198. __END_DECLS
  199. #endif /* CPU_FEATURES_H */