| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | module Hardware | 
					
						
							|  |  |  |   class CPU | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |     class << self | 
					
						
							| 
									
										
										
										
											2019-05-06 14:34:35 +09:00
										 |  |  |       def optimization_flags | 
					
						
							| 
									
										
										
										
											2020-04-30 16:50:13 +10:00
										 |  |  |         @optimization_flags ||= begin | 
					
						
							|  |  |  |           flags = generic_optimization_flags.dup | 
					
						
							|  |  |  |           flags[:native] = arch_flag(Homebrew::EnvConfig.arch) | 
					
						
							|  |  |  |           flags | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2019-05-06 14:34:35 +09:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def family | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         return :arm if arm? | 
					
						
							| 
									
										
										
										
											2017-11-29 11:44:59 -08:00
										 |  |  |         return :ppc if ppc? | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         return :dunno unless intel? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         # See https://software.intel.com/en-us/articles/intel-architecture-and-processor-identification-with-cpuid-model-and-family-numbers | 
					
						
							| 
									
										
										
										
											2024-11-09 11:45:23 +08:00
										 |  |  |         # and https://github.com/llvm/llvm-project/blob/main/llvm/lib/TargetParser/Host.cpp | 
					
						
							| 
									
										
										
										
											2018-10-01 09:44:59 -07:00
										 |  |  |         # and https://en.wikipedia.org/wiki/List_of_Intel_CPU_microarchitectures#Roadmap | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         vendor_id = cpuinfo[/^vendor_id\s*: (.*)/, 1] | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         cpu_family = cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i | 
					
						
							|  |  |  |         cpu_model = cpuinfo[/^model\s*: ([0-9]+)/, 1].to_i | 
					
						
							| 
									
										
										
										
											2018-10-01 09:44:59 -07:00
										 |  |  |         unknown = :"unknown_0x#{cpu_family.to_s(16)}_0x#{cpu_model.to_s(16)}" | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         case vendor_id | 
					
						
							|  |  |  |         when "GenuineIntel" | 
					
						
							| 
									
										
										
										
											2020-11-30 07:25:02 -06:00
										 |  |  |           intel_family(cpu_family, cpu_model) | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         when "AuthenticAMD" | 
					
						
							| 
									
										
										
										
											2024-11-09 21:54:05 -05:00
										 |  |  |           amd_family(cpu_family, cpu_model) | 
					
						
							| 
									
										
										
										
											2020-11-30 07:25:02 -06:00
										 |  |  |         end || unknown | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 07:25:02 -06:00
										 |  |  |       def intel_family(family, cpu_model) | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         case family | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         when 0x06
 | 
					
						
							|  |  |  |           case cpu_model | 
					
						
							|  |  |  |           when 0x3a, 0x3e
 | 
					
						
							|  |  |  |             :ivybridge | 
					
						
							|  |  |  |           when 0x2a, 0x2d
 | 
					
						
							|  |  |  |             :sandybridge | 
					
						
							|  |  |  |           when 0x25, 0x2c, 0x2f
 | 
					
						
							|  |  |  |             :westmere | 
					
						
							| 
									
										
										
										
											2024-11-09 11:45:23 +08:00
										 |  |  |           when 0x1a, 0x1e, 0x1f, 0x2e
 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |             :nehalem | 
					
						
							|  |  |  |           when 0x17, 0x1d
 | 
					
						
							|  |  |  |             :penryn | 
					
						
							|  |  |  |           when 0x0f, 0x16
 | 
					
						
							|  |  |  |             :merom | 
					
						
							|  |  |  |           when 0x0d
 | 
					
						
							|  |  |  |             :dothan | 
					
						
							| 
									
										
										
										
											2020-02-11 12:14:14 +11:00
										 |  |  |           when 0x1c, 0x26, 0x27, 0x35, 0x36
 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |             :atom | 
					
						
							| 
									
										
										
										
											2020-02-11 12:14:14 +11:00
										 |  |  |           when 0x3c, 0x3f, 0x45, 0x46
 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |             :haswell | 
					
						
							|  |  |  |           when 0x3d, 0x47, 0x4f, 0x56
 | 
					
						
							|  |  |  |             :broadwell | 
					
						
							| 
									
										
										
										
											2024-11-09 11:45:23 +08:00
										 |  |  |           when 0x4e, 0x5e, 0x8e, 0x9e, 0xa5, 0xa6
 | 
					
						
							| 
									
										
										
										
											2017-03-10 16:32:41 -06:00
										 |  |  |             :skylake | 
					
						
							| 
									
										
										
										
											2020-02-11 12:14:14 +11:00
										 |  |  |           when 0x66
 | 
					
						
							|  |  |  |             :cannonlake | 
					
						
							|  |  |  |           when 0x6a, 0x6c, 0x7d, 0x7e
 | 
					
						
							|  |  |  |             :icelake | 
					
						
							| 
									
										
										
										
											2024-11-09 11:45:23 +08:00
										 |  |  |           when 0xa7
 | 
					
						
							|  |  |  |             :rocketlake | 
					
						
							|  |  |  |           when 0x8c, 0x8d
 | 
					
						
							|  |  |  |             :tigerlake | 
					
						
							|  |  |  |           when 0x97, 0x9a, 0xbe, 0xb7, 0xba, 0xbf, 0xaa, 0xac
 | 
					
						
							|  |  |  |             :alderlake | 
					
						
							|  |  |  |           when 0xc5, 0xb5, 0xc6, 0xbd
 | 
					
						
							|  |  |  |             :arrowlake | 
					
						
							|  |  |  |           when 0xcc
 | 
					
						
							|  |  |  |             :pantherlake | 
					
						
							|  |  |  |           when 0xad, 0xae
 | 
					
						
							|  |  |  |             :graniterapids | 
					
						
							|  |  |  |           when 0xcf, 0x8f
 | 
					
						
							|  |  |  |             :sapphirerapids | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |           end | 
					
						
							|  |  |  |         when 0x0f
 | 
					
						
							|  |  |  |           case cpu_model | 
					
						
							|  |  |  |           when 0x06
 | 
					
						
							|  |  |  |             :presler | 
					
						
							|  |  |  |           when 0x03, 0x04
 | 
					
						
							|  |  |  |             :prescott | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 21:54:05 -05:00
										 |  |  |       def amd_family(family, cpu_model) | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         case family | 
					
						
							|  |  |  |         when 0x06
 | 
					
						
							|  |  |  |           :amd_k7 | 
					
						
							|  |  |  |         when 0x0f
 | 
					
						
							|  |  |  |           :amd_k8 | 
					
						
							|  |  |  |         when 0x10
 | 
					
						
							|  |  |  |           :amd_k10 | 
					
						
							|  |  |  |         when 0x11
 | 
					
						
							|  |  |  |           :amd_k8_k10_hybrid | 
					
						
							|  |  |  |         when 0x12
 | 
					
						
							|  |  |  |           :amd_k12 | 
					
						
							|  |  |  |         when 0x14
 | 
					
						
							|  |  |  |           :bobcat | 
					
						
							|  |  |  |         when 0x15
 | 
					
						
							|  |  |  |           :bulldozer | 
					
						
							|  |  |  |         when 0x16
 | 
					
						
							|  |  |  |           :jaguar | 
					
						
							|  |  |  |         when 0x17
 | 
					
						
							| 
									
										
										
										
											2024-11-09 21:54:05 -05:00
										 |  |  |           case cpu_model | 
					
						
							|  |  |  |           when 0x10..0x2f
 | 
					
						
							|  |  |  |             :zen | 
					
						
							|  |  |  |           when 0x30..0x3f, 0x47, 0x60..0x7f, 0x84..0x87, 0x90..0xaf
 | 
					
						
							|  |  |  |             :zen2 | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-11-30 05:38:13 -06:00
										 |  |  |         when 0x19
 | 
					
						
							| 
									
										
										
										
											2024-11-09 21:54:05 -05:00
										 |  |  |           case cpu_model | 
					
						
							|  |  |  |           when ..0x0f, 0x20..0x5f
 | 
					
						
							|  |  |  |             :zen3 | 
					
						
							|  |  |  |           when 0x10..0x1f, 0x60..0x7f, 0xa0..0xaf
 | 
					
						
							|  |  |  |             :zen4 | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         when 0x1a
 | 
					
						
							|  |  |  |           :zen5 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:26 -06:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 17:00:24 -06:00
										 |  |  |       # Supported CPU instructions | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def flags | 
					
						
							| 
									
										
										
										
											2020-11-27 17:00:24 -06:00
										 |  |  |         @flags ||= cpuinfo[/^(?:flags|Features)\s*: (.*)/, 1]&.split | 
					
						
							| 
									
										
										
										
											2017-11-29 11:44:59 -08:00
										 |  |  |         @flags ||= [] | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       # Compatibility with Mac method, which returns lowercase symbols | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # instead of strings. | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def features | 
					
						
							| 
									
										
										
										
											2020-05-12 08:32:27 +01:00
										 |  |  |         @features ||= flags[1..].map(&:intern) | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 11:19:24 +02:00
										 |  |  |       %w[aes altivec avx avx2 lm ssse3 sse4_2].each do |flag| | 
					
						
							| 
									
										
										
										
											2023-12-18 09:34:01 -08:00
										 |  |  |         define_method(:"#{flag}?") do | 
					
						
							| 
									
										
										
										
											2023-04-02 16:38:30 -07:00
										 |  |  |           T.bind(self, T.class_of(Hardware::CPU)) | 
					
						
							|  |  |  |           flags.include? flag | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-05 12:36:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def sse3? | 
					
						
							|  |  |  |         flags.include?("pni") || flags.include?("sse3") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 11:19:24 +02:00
										 |  |  |       def sse4? | 
					
						
							|  |  |  |         flags.include? "sse4_1" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-08-09 03:23:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def cpuinfo | 
					
						
							|  |  |  |         @cpuinfo ||= File.read("/proc/cpuinfo") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |