1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use syntax::{ast, attr};
12 use rustc::session::Session;
13 use syntax::parse::token::InternedString;
14 use syntax::parse::token::intern_and_get_ident as intern;
16 /// Add `target_feature = "..."` cfgs for a variety of platform
17 /// specific features (SSE, NEON etc.).
19 /// This uses a scheme similar to that employed by clang: reimplement
20 /// the target feature knowledge. *Theoretically* we could query LLVM
21 /// since that has perfect knowledge about what things are enabled in
22 /// code-generation, however, it is extremely non-obvious how to do
23 /// this successfully. Each platform defines a subclass of a
24 /// SubtargetInfo, which knows all this information, but the ways to
25 /// query them do not seem to be public.
26 pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
27 let tf = InternedString::new("target_feature");
28 macro_rules! fillout {
29 ($($func: ident, $name: expr;)*) => {{
31 cfg.push(attr::mk_name_value_item_str(tf.clone(), intern($name)))
50 fn features_contain(sess: &Session, s: &str) -> bool {
51 sess.target.target.options.features.contains(s) || sess.opts.cg.target_feature.contains(s)
54 pub fn has_sse(sess: &Session) -> bool {
55 features_contain(sess, "+sse") || has_sse2(sess)
57 pub fn has_sse2(sess: &Session) -> bool {
58 // x86-64 requires at least SSE2 support
59 sess.target.target.arch == "x86_64" || features_contain(sess, "+sse2") || has_sse3(sess)
61 pub fn has_sse3(sess: &Session) -> bool {
62 features_contain(sess, "+sse3") || has_ssse3(sess)
64 pub fn has_ssse3(sess: &Session) -> bool {
65 features_contain(sess, "+ssse3") || has_sse41(sess)
67 pub fn has_sse41(sess: &Session) -> bool {
68 features_contain(sess, "+sse4.1") || has_sse42(sess)
70 pub fn has_sse42(sess: &Session) -> bool {
71 features_contain(sess, "+sse4.2") || has_avx(sess)
73 pub fn has_avx(sess: &Session) -> bool {
74 features_contain(sess, "+avx") || has_avx2(sess)
76 pub fn has_avx2(sess: &Session) -> bool {
77 features_contain(sess, "+avx2")
80 pub fn has_neon(sess: &Session) -> bool {
81 // AArch64 requires NEON support
82 sess.target.target.arch == "aarch64" || features_contain(sess, "+neon")
84 pub fn has_vfp(sess: &Session) -> bool {
85 // AArch64 requires VFP support
86 sess.target.target.arch == "aarch64" || features_contain(sess, "+vfp")