oxigdal_embedded/target/
mod.rs1pub mod arm;
6pub mod common;
7pub mod esp32;
8pub mod riscv;
9
10pub trait TargetArch {
12 fn name(&self) -> &'static str;
14
15 fn pointer_size(&self) -> usize;
17
18 fn native_alignment(&self) -> usize;
20
21 fn supports_unaligned_access(&self) -> bool;
23
24 fn memory_barrier(&self);
26
27 fn cycle_count(&self) -> Option<u64>;
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum MemoryOrder {
34 Relaxed,
36 Acquire,
38 Release,
40 AcquireRelease,
42 SeqCst,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum CacheOp {
49 Clean,
51 Invalidate,
53 CleanInvalidate,
55}
56
57#[derive(Debug, Clone, Copy)]
59pub struct TargetCapabilities {
60 pub has_fpu: bool,
62 pub has_simd: bool,
64 pub has_aes: bool,
66 pub has_crc: bool,
68 pub cache_line_size: usize,
70 pub num_cores: usize,
72}
73
74impl Default for TargetCapabilities {
75 fn default() -> Self {
76 Self {
77 has_fpu: false,
78 has_simd: false,
79 has_aes: false,
80 has_crc: false,
81 cache_line_size: 64,
82 num_cores: 1,
83 }
84 }
85}
86
87pub fn get_capabilities() -> TargetCapabilities {
89 #[cfg(feature = "arm")]
90 {
91 arm::get_capabilities()
92 }
93
94 #[cfg(all(feature = "riscv", not(feature = "arm")))]
95 {
96 riscv::get_capabilities()
97 }
98
99 #[cfg(all(feature = "esp32", not(any(feature = "arm", feature = "riscv"))))]
100 {
101 esp32::get_capabilities()
102 }
103
104 #[cfg(not(any(feature = "arm", feature = "riscv", feature = "esp32")))]
105 {
106 TargetCapabilities::default()
107 }
108}
109
110#[inline]
112pub fn memory_barrier() {
113 #[cfg(feature = "arm")]
114 arm::memory_barrier();
115
116 #[cfg(all(feature = "riscv", not(feature = "arm")))]
117 riscv::memory_barrier();
118
119 #[cfg(all(feature = "esp32", not(any(feature = "arm", feature = "riscv"))))]
120 esp32::memory_barrier();
121
122 #[cfg(not(any(feature = "arm", feature = "riscv", feature = "esp32")))]
123 core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
124}
125
126#[inline]
128pub fn cycle_count() -> Option<u64> {
129 #[cfg(feature = "arm")]
130 {
131 arm::cycle_count()
132 }
133
134 #[cfg(all(feature = "riscv", not(feature = "arm")))]
135 {
136 riscv::cycle_count()
137 }
138
139 #[cfg(all(feature = "esp32", not(any(feature = "arm", feature = "riscv"))))]
140 {
141 esp32::cycle_count()
142 }
143
144 #[cfg(not(any(feature = "arm", feature = "riscv", feature = "esp32")))]
145 {
146 None
147 }
148}
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 #[test]
155 fn test_capabilities() {
156 let caps = get_capabilities();
157 assert!(caps.cache_line_size > 0);
158 assert!(caps.num_cores > 0);
159 }
160
161 #[test]
162 fn test_memory_barrier() {
163 memory_barrier();
165 }
166}