miri/shims/math.rs
1use rustc_middle::ty::Ty;
2use rustc_span::Symbol;
3use rustc_target::callconv::FnAbi;
4
5use self::math::{ToHost, ToSoft};
6use crate::*;
7
8impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
9pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10 fn emulate_foreign_item_inner(
11 &mut self,
12 link_name: Symbol,
13 abi: &FnAbi<'tcx, Ty<'tcx>>,
14 args: &[OpTy<'tcx>],
15 dest: &MPlaceTy<'tcx>,
16 ) -> InterpResult<'tcx, EmulateItemResult> {
17 let ecx = self.eval_context_mut();
18
19 // math functions (note that there are also intrinsics for some other functions)
20 match link_name.as_str() {
21 // math functions (note that there are also intrinsics for some other functions)
22 #[rustfmt::skip]
23 | "cbrtf"
24 | "coshf"
25 | "sinhf"
26 | "tanf"
27 | "tanhf"
28 | "acosf"
29 | "asinf"
30 | "atanf"
31 | "acoshf"
32 | "asinhf"
33 | "log1pf"
34 | "expm1f"
35 | "tgammaf"
36 | "erff"
37 | "erfcf"
38 => {
39 let [f] = &ecx.check_shim_sig(
40 shim_sig!(extern "C" fn(f32) -> f32),
41 (link_name, abi, args),
42 )?;
43 let f = ecx.read_scalar(f)?.to_f32()?;
44
45 let res = math::fixed_float_value(ecx, link_name.as_str(), &[f]).unwrap_or_else(|| {
46 // Using host floats (but it's fine, these operations do not have
47 // guaranteed precision).
48 let f_host = f.to_host();
49 let res = match link_name.as_str() {
50 "cbrtf" => f_host.cbrt(),
51 "coshf" => f_host.cosh(),
52 "sinhf" => f_host.sinh(),
53 "tanf" => f_host.tan(),
54 "tanhf" => f_host.tanh(),
55 "acosf" => f_host.acos(),
56 "asinf" => f_host.asin(),
57 "atanf" => f_host.atan(),
58 "acoshf" => f_host.acosh(),
59 "asinhf" => f_host.asinh(),
60 "log1pf" => f_host.ln_1p(),
61 "expm1f" => f_host.exp_m1(),
62 "tgammaf" => f_host.gamma(),
63 "erff" => f_host.erf(),
64 "erfcf" => f_host.erfc(),
65 _ => bug!(),
66 };
67 let res = res.to_soft();
68 // Apply a relative error of 4ULP to introduce some non-determinism
69 // simulating imprecise implementations and optimizations.
70 let res = math::apply_random_float_error_ulp(ecx, res, 4);
71
72 // Clamp the result to the guaranteed range of this function according to the C standard,
73 // if any.
74 math::clamp_float_value(link_name.as_str(), res)
75 });
76 let res = ecx.adjust_nan(res, &[f]);
77 ecx.write_scalar(res, dest)?;
78 }
79 #[rustfmt::skip]
80 | "_hypotf"
81 | "hypotf"
82 | "atan2f"
83 | "fdimf"
84 => {
85 let [f1, f2] = &ecx.check_shim_sig(
86 shim_sig!(extern "C" fn(f32, f32) -> f32),
87 (link_name, abi, args),
88 )?;
89 let f1 = ecx.read_scalar(f1)?.to_f32()?;
90 let f2 = ecx.read_scalar(f2)?.to_f32()?;
91
92 let res = math::fixed_float_value(ecx, link_name.as_str(), &[f1, f2])
93 .unwrap_or_else(|| {
94 let res = match link_name.as_str() {
95 // underscore case for windows, here and below
96 // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
97 // Using host floats (but it's fine, these operations do not have guaranteed precision).
98 "_hypotf" | "hypotf" => f1.to_host().hypot(f2.to_host()).to_soft(),
99 "atan2f" => f1.to_host().atan2(f2.to_host()).to_soft(),
100 #[allow(deprecated)]
101 "fdimf" => f1.to_host().abs_sub(f2.to_host()).to_soft(),
102 _ => bug!(),
103 };
104 // Apply a relative error of 4ULP to introduce some non-determinism
105 // simulating imprecise implementations and optimizations.
106 let res = math::apply_random_float_error_ulp(ecx, res, 4);
107
108 // Clamp the result to the guaranteed range of this function according to the C standard,
109 // if any.
110 math::clamp_float_value(link_name.as_str(), res)
111 });
112 let res = ecx.adjust_nan(res, &[f1, f2]);
113 ecx.write_scalar(res, dest)?;
114 }
115 #[rustfmt::skip]
116 | "cbrt"
117 | "cosh"
118 | "sinh"
119 | "tan"
120 | "tanh"
121 | "acos"
122 | "asin"
123 | "atan"
124 | "acosh"
125 | "asinh"
126 | "log1p"
127 | "expm1"
128 | "tgamma"
129 | "erf"
130 | "erfc"
131 => {
132 let [f] = &ecx.check_shim_sig(
133 shim_sig!(extern "C" fn(f64) -> f64),
134 (link_name, abi, args),
135 )?;
136 let f = ecx.read_scalar(f)?.to_f64()?;
137
138 let res = math::fixed_float_value(ecx, link_name.as_str(), &[f]).unwrap_or_else(|| {
139 // Using host floats (but it's fine, these operations do not have
140 // guaranteed precision).
141 let f_host = f.to_host();
142 let res = match link_name.as_str() {
143 "cbrt" => f_host.cbrt(),
144 "cosh" => f_host.cosh(),
145 "sinh" => f_host.sinh(),
146 "tan" => f_host.tan(),
147 "tanh" => f_host.tanh(),
148 "acos" => f_host.acos(),
149 "asin" => f_host.asin(),
150 "atan" => f_host.atan(),
151 "acosh" => f_host.acosh(),
152 "asinh" => f_host.asinh(),
153 "log1p" => f_host.ln_1p(),
154 "expm1" => f_host.exp_m1(),
155 "tgamma" => f_host.gamma(),
156 "erf" => f_host.erf(),
157 "erfc" => f_host.erfc(),
158 _ => bug!(),
159 };
160 let res = res.to_soft();
161 // Apply a relative error of 4ULP to introduce some non-determinism
162 // simulating imprecise implementations and optimizations.
163 let res = math::apply_random_float_error_ulp(ecx, res, 4);
164
165 // Clamp the result to the guaranteed range of this function according to the C standard,
166 // if any.
167 math::clamp_float_value(link_name.as_str(), res)
168 });
169 let res = ecx.adjust_nan(res, &[f]);
170 ecx.write_scalar(res, dest)?;
171 }
172 #[rustfmt::skip]
173 | "_hypot"
174 | "hypot"
175 | "atan2"
176 | "fdim"
177 => {
178 let [f1, f2] = &ecx.check_shim_sig(
179 shim_sig!(extern "C" fn(f64, f64) -> f64),
180 (link_name, abi, args),
181 )?;
182 let f1 = ecx.read_scalar(f1)?.to_f64()?;
183 let f2 = ecx.read_scalar(f2)?.to_f64()?;
184
185 let res = math::fixed_float_value(ecx, link_name.as_str(), &[f1, f2]).unwrap_or_else(|| {
186 let res = match link_name.as_str() {
187 // underscore case for windows, here and below
188 // (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
189 // Using host floats (but it's fine, these operations do not have guaranteed precision).
190 "_hypot" | "hypot" => f1.to_host().hypot(f2.to_host()).to_soft(),
191 "atan2" => f1.to_host().atan2(f2.to_host()).to_soft(),
192 #[allow(deprecated)]
193 "fdim" => f1.to_host().abs_sub(f2.to_host()).to_soft(),
194 _ => bug!(),
195 };
196 // Apply a relative error of 4ULP to introduce some non-determinism
197 // simulating imprecise implementations and optimizations.
198 let res = math::apply_random_float_error_ulp(ecx, res, 4);
199
200 // Clamp the result to the guaranteed range of this function according to the C standard,
201 // if any.
202 math::clamp_float_value(link_name.as_str(), res)
203 });
204 let res = ecx.adjust_nan(res, &[f1, f2]);
205 ecx.write_scalar(res, dest)?;
206 }
207 "lgammaf_r" => {
208 let [x, signp] = &ecx.check_shim_sig(
209 shim_sig!(extern "C" fn(f32, *i32) -> f32),
210 (link_name, abi, args),
211 )?;
212 let x = ecx.read_scalar(x)?.to_f32()?;
213 let signp = ecx.deref_pointer(signp)?;
214
215 // Using host floats (but it's fine, these operations do not have guaranteed precision).
216 let (res, sign) = x.to_host().ln_gamma();
217 ecx.write_int(sign, &signp)?;
218
219 let res = res.to_soft();
220 // Apply a relative error of 4ULP to introduce some non-determinism
221 // simulating imprecise implementations and optimizations.
222 let res = math::apply_random_float_error_ulp(ecx, res, 4);
223 // Clamp the result to the guaranteed range of this function according to the C standard,
224 // if any.
225 let res = math::clamp_float_value(link_name.as_str(), res);
226 let res = ecx.adjust_nan(res, &[x]);
227 ecx.write_scalar(res, dest)?;
228 }
229 "lgamma_r" => {
230 let [x, signp] = &ecx.check_shim_sig(
231 shim_sig!(extern "C" fn(f64, *i32) -> f64),
232 (link_name, abi, args),
233 )?;
234 let x = ecx.read_scalar(x)?.to_f64()?;
235 let signp = ecx.deref_pointer(signp)?;
236
237 // Using host floats (but it's fine, these operations do not have guaranteed precision).
238 let (res, sign) = x.to_host().ln_gamma();
239 ecx.write_int(sign, &signp)?;
240
241 let res = res.to_soft();
242 // Apply a relative error of 4ULP to introduce some non-determinism
243 // simulating imprecise implementations and optimizations.
244 let res = math::apply_random_float_error_ulp(ecx, res, 4);
245 // Clamp the result to the guaranteed range of this function according to the C standard,
246 // if any.
247 let res = math::clamp_float_value(link_name.as_str(), res);
248 let res = ecx.adjust_nan(res, &[x]);
249 ecx.write_scalar(res, dest)?;
250 }
251
252 _ => return interp_ok(EmulateItemResult::NotSupported),
253 }
254
255 interp_ok(EmulateItemResult::NeedsReturn)
256 }
257}