Skip to content

Commit 69f475d

Browse files
committed
Fix popcnt CPU support detection
Signed-off-by: Artem Siryk <[email protected]>
1 parent de1f112 commit 69f475d

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/discover/discover.ml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ let () =
1515
~name:"discover"
1616
~args:[ "-o", Set_string output, "FILENAME output file" ]
1717
(fun c ->
18-
let has_popcnt = c_test c ~c_flags:[ "-mpopcnt" ] program in
19-
Flags.write_sexp !output (if has_popcnt then [ "-mpopcnt" ] else []))
18+
let arch = ocaml_config_var c "architecture" in
19+
let cc = ocaml_config_var_exn c "native_c_compiler" in
20+
let is_gcc =
21+
(Process.run c cc [ "--version" ] ~env:[]).stdout
22+
|> String.lowercase_ascii
23+
|> String.starts_with ~prefix:"gcc"
24+
in
25+
let try_flag c flag = if c_test c ~c_flags:[ flag ] program then flag else "" in
26+
let flags =
27+
match arch with
28+
| Some s when String.length s >= 3 ->
29+
(match String.sub s 0 3 with
30+
| "ppc" | "pow" ->
31+
let popcntd = try_flag c "-mpopcntd" in
32+
if popcntd <> ""
33+
then popcntd
34+
(* needed to prevent crashing, only gcc has this option *)
35+
else if is_gcc
36+
then try_flag c "-mpopcntb"
37+
else ""
38+
| "spa" -> try_flag c "-mpopc"
39+
| "x86" | "amd" -> try_flag c "-mpopcnt"
40+
| _ -> "")
41+
(* armv8/arm64/aarch64 support popcnt via vcnt by default and don't require any flags *)
42+
| _ -> ""
43+
in
44+
let flags = if flags <> "" then [ flags ] else [] in
45+
Flags.write_sexp !output flags)
2046
;;
47+

0 commit comments

Comments
 (0)