#[must_use]
pub fn source_is_f64(source: &str) -> bool {
source.contains("array<f64>")
|| source.contains(": f64,")
|| source.contains(": f64;")
|| source.contains(": f64\n")
|| source.contains("-> f64")
}
#[must_use]
pub fn strip_f64_enable(source: &str) -> String {
source
.lines()
.filter(|l| l.trim() != "enable f64;")
.collect::<Vec<_>>()
.join("\n")
}
#[must_use]
pub fn downcast_f64_to_f32(f64_source: &str) -> String {
let result = f64_source
.replace("enable f64;\n", "")
.replace("enable f64;", "")
.replace("_f64(", "\x00_F64_CALL\x00")
.replace("array<f64>", "array<f32>")
.replace("array<f64,", "array<f32,")
.replace(": f64", ": f32")
.replace("-> f64", "-> f32")
.replace("f64(", "f32(")
.replace("<f64>", "<f32>")
.replace("\x00_F64_CALL\x00", "_f64(");
clamp_f64_range_literals(&result)
}
#[must_use]
pub fn clamp_f64_range_literals(source: &str) -> String {
source
.replace("-1.7976931348623157e+308", "-3.4028235e+38")
.replace("1.7976931348623157e+308", "3.4028235e+38")
.replace("-1.0e308", "-3.4028235e+38")
.replace("1.0e308", "3.4028235e+38")
.replace("-1e308", "-3.4028235e+38")
.replace("1e308", "3.4028235e+38")
.replace("-1.0e300", "-3.4028235e+38")
.replace("1.0e300", "3.4028235e+38")
.replace("-1e300", "-3.4028235e+38")
.replace("1e300", "3.4028235e+38")
}
#[must_use]
pub fn downcast_f64_to_f32_with_transcendentals(f64_source: &str) -> String {
let base = downcast_f64_to_f32(f64_source);
base.replace("exp_f64(", "exp(")
.replace("log_f64(", "log(")
.replace("pow_f64(", "pow(")
.replace("sin_f64(", "sin(")
.replace("cos_f64(", "cos(")
.replace("tan_f64(", "tan(")
.replace("asin_f64(", "asin(")
.replace("acos_f64(", "acos(")
.replace("atan_f64(", "atan(")
.replace("atan2_f64(", "atan2(")
.replace("sinh_f64(", "sinh(")
.replace("cosh_f64(", "cosh(")
.replace("tanh_f64(", "tanh(")
.replace("sqrt_f64(", "sqrt(")
.replace("abs_f64(", "abs(")
.replace("erf_f64(", "erf(")
}
#[must_use]
pub fn downcast_f64_to_df64(f64_source: &str) -> String {
let result = f64_source
.replace("_f64(", "\x00_F64_CALL\x00")
.replace("array<f64>", "array<vec2<f32>>")
.replace("array<f64,", "array<vec2<f32>,")
.replace(": f64", ": Df64")
.replace("-> f64", "-> Df64")
.replace("<f64>", "<Df64>")
.replace("f64(", "df64_from_f32(")
.replace("\x00_F64_CALL\x00", "_f64(");
let with_transcendentals = result
.replace("exp_f64(", "exp_df64(")
.replace("log_f64(", "log_df64(")
.replace("pow_f64(", "pow_df64(")
.replace("sin_f64(", "sin_df64(")
.replace("cos_f64(", "cos_df64(")
.replace("tanh_f64(", "tanh_df64(")
.replace("sqrt_f64(", "sqrt_df64(")
.replace("abs_f64(", "df64_abs(");
clamp_f64_range_literals(&with_transcendentals)
}