|
| 1 | +use crate::types::{IntrinsicType, TypeKind}; |
| 2 | + |
| 3 | +use super::argument::ArgumentList; |
| 4 | +use serde::de::Unexpected; |
| 5 | +use serde::{de, Deserialize, Deserializer}; |
| 6 | + |
| 7 | +/// An intrinsic |
| 8 | +#[derive(Deserialize, Debug, PartialEq, Clone)] |
| 9 | +pub struct Intrinsic { |
| 10 | + /// If the intrinsic should be tested. |
| 11 | + #[serde(deserialize_with = "bool_from_string")] |
| 12 | + pub enabled: bool, |
| 13 | + |
| 14 | + /// The function name of this intrinsic. |
| 15 | + pub name: String, |
| 16 | + |
| 17 | + /// Any arguments for this intrinsinc. |
| 18 | + #[serde(rename = "args")] |
| 19 | + pub arguments: ArgumentList, |
| 20 | + |
| 21 | + /// The return type of this intrinsic. |
| 22 | + #[serde(rename = "return")] |
| 23 | + pub results: IntrinsicType, |
| 24 | +} |
| 25 | + |
| 26 | +impl Intrinsic { |
| 27 | + /// Generates a std::cout for the intrinsics results that will match the |
| 28 | + /// rust debug output format for the return type. |
| 29 | + pub fn print_result_c(&self, index: usize) -> String { |
| 30 | + let lanes = if self.results.num_lanes() > 1 { |
| 31 | + (0..self.results.num_lanes()) |
| 32 | + .map(|idx| -> std::string::String { |
| 33 | + format!( |
| 34 | + "{cast}{lane_fn}(__return_value, {lane})", |
| 35 | + cast = self.results.c_promotion(), |
| 36 | + lane_fn = self.results.get_lane_function(), |
| 37 | + lane = idx |
| 38 | + ) |
| 39 | + }) |
| 40 | + .collect::<Vec<_>>() |
| 41 | + .join(r#" << ", " << "#) |
| 42 | + } else { |
| 43 | + format!( |
| 44 | + "{promote}cast<{cast}>(__return_value)", |
| 45 | + cast = match self.results.kind() { |
| 46 | + TypeKind::Float if self.results.inner_size() == 32 => "float".to_string(), |
| 47 | + TypeKind::Float if self.results.inner_size() == 64 => "double".to_string(), |
| 48 | + TypeKind::Int => format!("int{}_t", self.results.inner_size()), |
| 49 | + TypeKind::UInt => format!("uint{}_t", self.results.inner_size()), |
| 50 | + TypeKind::Poly => format!("poly{}_t", self.results.inner_size()), |
| 51 | + ty => todo!("print_result_c - Unknown type: {:#?}", ty), |
| 52 | + }, |
| 53 | + promote = self.results.c_promotion(), |
| 54 | + ) |
| 55 | + }; |
| 56 | + |
| 57 | + format!( |
| 58 | + r#"std::cout << "Result {idx}: {ty}" << std::fixed << std::setprecision(150) << {lanes} << "{close}" << std::endl;"#, |
| 59 | + ty = if self.results.is_simd() { |
| 60 | + format!("{}(", self.results.c_type()) |
| 61 | + } else { |
| 62 | + String::from("") |
| 63 | + }, |
| 64 | + close = if self.results.is_simd() { ")" } else { "" }, |
| 65 | + lanes = lanes, |
| 66 | + idx = index, |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn generate_pass_rust(&self, index: usize) -> String { |
| 71 | + format!( |
| 72 | + r#" |
| 73 | + unsafe {{ |
| 74 | + {initialized_args} |
| 75 | + let res = {intrinsic_call}({args}); |
| 76 | + println!("Result {idx}: {{:.150?}}", res); |
| 77 | + }}"#, |
| 78 | + initialized_args = self.arguments.init_random_values_rust(index), |
| 79 | + intrinsic_call = self.name, |
| 80 | + args = self.arguments.as_call_param_rust(), |
| 81 | + idx = index, |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn generate_pass_c(&self, index: usize) -> String { |
| 86 | + format!( |
| 87 | + r#" {{ |
| 88 | + {initialized_args} |
| 89 | + auto __return_value = {intrinsic_call}({args}); |
| 90 | + {print_result} |
| 91 | + }}"#, |
| 92 | + initialized_args = self.arguments.init_random_values_c(index), |
| 93 | + intrinsic_call = self.name, |
| 94 | + args = self.arguments.as_call_param_c(), |
| 95 | + print_result = self.print_result_c(index) |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn bool_from_string<'de, D>(deserializer: D) -> Result<bool, D::Error> |
| 101 | +where |
| 102 | + D: Deserializer<'de>, |
| 103 | +{ |
| 104 | + match String::deserialize(deserializer)?.to_uppercase().as_ref() { |
| 105 | + "TRUE" => Ok(true), |
| 106 | + "FALSE" => Ok(false), |
| 107 | + other => Err(de::Error::invalid_value( |
| 108 | + Unexpected::Str(other), |
| 109 | + &"TRUE or FALSE", |
| 110 | + )), |
| 111 | + } |
| 112 | +} |
0 commit comments