cranelift_assembler_x64_meta/dsl/
features.rs1use core::fmt;
4use std::ops::BitOr;
5
6#[derive(PartialEq)]
24pub struct Features(Vec<Feature>);
25
26impl Features {
27 #[must_use]
28 pub fn is_empty(&self) -> bool {
29 self.0.is_empty()
30 }
31
32 pub fn iter(&self) -> impl Iterator<Item = &Feature> {
33 self.0.iter()
34 }
35
36 pub fn contains(&self, feature: Feature) -> bool {
37 self.0.contains(&feature)
38 }
39
40 pub(crate) fn is_sse(&self) -> bool {
41 self.0.iter().any(|f| {
42 matches!(
43 f,
44 Feature::sse | Feature::sse2 | Feature::ssse3 | Feature::sse41
45 )
46 })
47 }
48}
49
50impl fmt::Display for Features {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(
53 f,
54 "{}",
55 self.0
56 .iter()
57 .map(ToString::to_string)
58 .collect::<Vec<_>>()
59 .join(" | ")
60 )
61 }
62}
63
64#[derive(Clone, Copy, Debug, PartialEq)]
74#[allow(non_camel_case_types, reason = "makes DSL definitions easier to read")]
75pub enum Feature {
76 _64b,
77 compat,
78 sse,
79 sse2,
80 sse3,
81 ssse3,
82 sse41,
83 sse42,
84 bmi1,
85 bmi2,
86 lzcnt,
87 popcnt,
88 avx,
89 cmpxchg16b,
90}
91
92pub const ALL_FEATURES: &[Feature] = &[
100 Feature::_64b,
101 Feature::compat,
102 Feature::sse,
103 Feature::sse2,
104 Feature::sse3,
105 Feature::ssse3,
106 Feature::sse41,
107 Feature::sse42,
108 Feature::bmi1,
109 Feature::bmi2,
110 Feature::lzcnt,
111 Feature::popcnt,
112 Feature::avx,
113 Feature::cmpxchg16b,
114];
115
116impl fmt::Display for Feature {
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 fmt::Debug::fmt(self, f)
119 }
120}
121
122impl From<Feature> for Features {
123 fn from(flag: Feature) -> Self {
124 Features(vec![flag])
125 }
126}
127
128impl From<Option<Feature>> for Features {
129 fn from(flag: Option<Feature>) -> Self {
130 Features(flag.into_iter().collect())
131 }
132}
133
134impl BitOr for Feature {
135 type Output = Features;
136 fn bitor(self, rhs: Self) -> Self::Output {
137 assert_ne!(self, rhs, "duplicate feature: {self:?}");
138 Features(vec![self, rhs])
139 }
140}
141
142impl BitOr<Feature> for Features {
143 type Output = Features;
144 fn bitor(mut self, rhs: Feature) -> Self::Output {
145 assert!(!self.0.contains(&rhs), "duplicate feature: {rhs:?}");
146 self.0.push(rhs);
147 self
148 }
149}