Skip to content

Commit 1925986

Browse files
committed
Genericize sqrt
1 parent 5bb4caa commit 1925986

15 files changed

Lines changed: 31 additions & 106 deletions

File tree

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,6 @@ fn codegen_float_intrinsic_call<'tcx>(
322322
sym::exp2f32 => ("exp2f", 1, fx.tcx.types.f32, types::F32),
323323
sym::exp2f64 => ("exp2", 1, fx.tcx.types.f64, types::F64),
324324
sym::exp2f128 => ("exp2f128", 1, fx.tcx.types.f128, types::F128),
325-
sym::sqrtf16 => ("sqrtf16", 1, fx.tcx.types.f16, types::F16),
326-
sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32, types::F32),
327-
sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64, types::F64),
328-
sym::sqrtf128 => ("sqrtf128", 1, fx.tcx.types.f128, types::F128),
329325
sym::powif16 => ("__powisf2", 2, fx.tcx.types.f16, types::F16), // compiler-builtins
330326
sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32, types::F32), // compiler-builtins
331327
sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64, types::F64), // compiler-builtins
@@ -409,7 +405,6 @@ fn codegen_float_intrinsic_call<'tcx>(
409405
sym::copysignf16 => codegen_f16_f128::copysign_f16(fx, args[0], args[1]),
410406
sym::copysignf128 => codegen_f16_f128::copysign_f128(fx, args[0], args[1]),
411407
sym::copysignf32 | sym::copysignf64 => fx.bcx.ins().fcopysign(args[0], args[1]),
412-
sym::sqrtf32 | sym::sqrtf64 => fx.bcx.ins().sqrt(args[0]),
413408

414409
// These intrinsics aren't supported natively by Cranelift.
415410
// Lower them to a libcall.
@@ -1121,6 +1116,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
11211116

11221117
// Float unop intrinsics
11231118
sym::fabs
1119+
| sym::sqrt
11241120
| sym::floor
11251121
| sym::ceil
11261122
| sym::trunc
@@ -1145,6 +1141,9 @@ fn codegen_regular_intrinsic_call<'tcx>(
11451141
// backend lowerings are implemented.
11461142
(sym::fabs, F16) => Ok(codegen_f16_f128::abs_f16(fx, x)),
11471143
(sym::fabs, F128) => Ok(codegen_f16_f128::abs_f128(fx, x)),
1144+
(sym::sqrt, F32 | F64) => Ok(fx.bcx.ins().sqrt(x)),
1145+
(sym::sqrt, F16) => Err("sqrtf16"),
1146+
(sym::sqrt, F128) => Err("sqrtf128"),
11481147
(sym::floor, F32 | F64) => Ok(fx.bcx.ins().floor(x)),
11491148
(sym::floor, F16) => Err("floorf16"),
11501149
(sym::floor, F128) => Err("floorf128"),

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ fn get_simple_intrinsic<'gcc, 'tcx>(
4545
name: Symbol,
4646
) -> Option<Function<'gcc>> {
4747
let gcc_name = match name {
48-
sym::sqrtf32 => "sqrtf",
49-
sym::sqrtf64 => "sqrt",
5048
sym::powif32 => "__builtin_powif",
5149
sym::powif64 => "__builtin_powi",
5250
sym::sinf32 => "sinf",
@@ -180,26 +178,6 @@ fn get_simple_function<'gcc, 'tcx>(
180178
))
181179
}
182180

183-
fn get_simple_function_f128<'gcc, 'tcx>(
184-
span: Span,
185-
cx: &CodegenCx<'gcc, 'tcx>,
186-
name: Symbol,
187-
) -> Function<'gcc> {
188-
let f128_type = cx.type_f128();
189-
let func_name = match name {
190-
sym::sqrtf128 => "sqrtf128",
191-
_ => span_bug!(span, "used get_simple_function_f128 for non-unary f128 intrinsic"),
192-
};
193-
cx.context.new_function(
194-
None,
195-
FunctionType::Extern,
196-
f128_type,
197-
&[cx.context.new_parameter(None, f128_type, "a")],
198-
func_name,
199-
false,
200-
)
201-
}
202-
203181
fn f16_builtin<'gcc, 'tcx>(
204182
cx: &CodegenCx<'gcc, 'tcx>,
205183
name: Symbol,
@@ -217,7 +195,6 @@ fn f16_builtin<'gcc, 'tcx>(
217195
let result = cx.context.new_call(None, func, &args);
218196
return cx.context.new_cast(None, result, cx.type_f16());
219197
}
220-
sym::sqrtf16 => "__builtin_sqrtf",
221198
_ => unreachable!(),
222199
};
223200

@@ -262,30 +239,8 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
262239
&args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
263240
)
264241
}
265-
sym::ceilf16
266-
| sym::copysignf16
267-
| sym::floorf16
268-
| sym::fmaf16
269-
| sym::powf16
270-
| sym::powif16
271-
| sym::roundf16
272-
| sym::round_ties_even_f16
273-
| sym::sqrtf16
274-
| sym::truncf16 => f16_builtin(self, name, args),
275-
sym::ceilf128
276-
| sym::floorf128
277-
| sym::truncf128
278-
| sym::roundf128
279-
| sym::round_ties_even_f128
280-
| sym::sqrtf128
281-
if self.cx.supports_f128_type =>
282-
{
283-
let func = get_simple_function_f128(span, self, name);
284-
self.cx.context.new_call(
285-
self.location,
286-
func,
287-
&args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
288-
)
242+
sym::copysignf16 | sym::fmaf16 | sym::powf16 | sym::powif16 => {
243+
f16_builtin(self, name, args)
289244
}
290245
sym::copysignf128 if self.cx.supports_f128_type => {
291246
let f128_type = self.cx.type_f128();
@@ -465,6 +420,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
465420
}
466421
// float unary intrinsics
467422
sym::fabs
423+
| sym::sqrt
468424
| sym::floor
469425
| sym::ceil
470426
| sym::trunc
@@ -489,6 +445,10 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
489445
(sym::fabs, F32) => Builtin("fabsf"),
490446
(sym::fabs, F64) => Builtin("fabs"),
491447
(sym::fabs, F128) => Extern("fabsf128"),
448+
(sym::sqrt, F16) => BuiltinF16Cast("__builtin_sqrtf"),
449+
(sym::sqrt, F32) => Builtin("sqrtf"),
450+
(sym::sqrt, F64) => Builtin("sqrt"),
451+
(sym::sqrt, F128) => Extern("sqrtf128"),
492452
(sym::floor, F16) => BuiltinF16Cast("__builtin_floorf"),
493453
(sym::floor, F32) => Builtin("floorf"),
494454
(sym::floor, F64) => Builtin("floor"),

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ fn call_simple_intrinsic<'ll, 'tcx>(
4848
args: &[OperandRef<'tcx, &'ll Value>],
4949
) -> Option<&'ll Value> {
5050
let (base_name, type_params): (&'static str, &[&'ll Type]) = match name {
51-
sym::sqrtf16 => ("llvm.sqrt", &[bx.type_f16()]),
52-
sym::sqrtf32 => ("llvm.sqrt", &[bx.type_f32()]),
53-
sym::sqrtf64 => ("llvm.sqrt", &[bx.type_f64()]),
54-
sym::sqrtf128 => ("llvm.sqrt", &[bx.type_f128()]),
55-
5651
sym::powif16 => ("llvm.powi", &[bx.type_f16(), bx.type_i32()]),
5752
sym::powif32 => ("llvm.powi", &[bx.type_f32(), bx.type_i32()]),
5853
sym::powif64 => ("llvm.powi", &[bx.type_f64(), bx.type_i32()]),
@@ -470,6 +465,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
470465
}
471466

472467
sym::fabs
468+
| sym::sqrt
473469
| sym::floor
474470
| sym::ceil
475471
| sym::trunc
@@ -482,6 +478,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
482478
let llty = self.type_float_from_ty(*f);
483479
let llvm_name = match name {
484480
sym::fabs => "llvm.fabs",
481+
sym::sqrt => "llvm.sqrt",
485482
sym::floor => "llvm.floor",
486483
sym::ceil => "llvm.ceil",
487484
sym::trunc => "llvm.trunc",

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
187187
| sym::sinf64
188188
| sym::sinf128
189189
| sym::size_of
190-
| sym::sqrtf16
191-
| sym::sqrtf32
192-
| sym::sqrtf64
193-
| sym::sqrtf128
190+
| sym::sqrt
194191
| sym::sub_with_overflow
195192
| sym::three_way_compare
196193
| sym::trunc
@@ -381,10 +378,7 @@ pub(crate) fn check_intrinsic_type(
381378
tcx.types.unit,
382379
),
383380

384-
sym::sqrtf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
385-
sym::sqrtf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
386-
sym::sqrtf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
387-
sym::sqrtf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
381+
sym::sqrt => (1, 0, vec![param(0)], param(0)),
388382

389383
sym::powif16 => (0, 0, vec![tcx.types.f16, tcx.types.i32], tcx.types.f16),
390384
sym::powif32 => (0, 0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,10 +1917,7 @@ symbols! {
19171917
speed,
19181918
spirv,
19191919
spotlight,
1920-
sqrtf16,
1921-
sqrtf32,
1922-
sqrtf64,
1923-
sqrtf128,
1920+
sqrt,
19241921
sreg,
19251922
sreg_low16,
19261923
sse,

library/core/src/intrinsics/mod.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,34 +1019,13 @@ pub unsafe fn unaligned_volatile_load<T>(src: *const T) -> T;
10191019
#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_store"]
10201020
pub unsafe fn unaligned_volatile_store<T>(dst: *mut T, val: T);
10211021

1022-
/// Returns the square root of an `f16`
1022+
/// Returns the square root of a floating point value.
10231023
///
1024-
/// The stabilized version of this intrinsic is
1025-
/// [`f16::sqrt`](../../std/primitive.f16.html#method.sqrt)
1026-
#[rustc_intrinsic]
1027-
#[rustc_nounwind]
1028-
pub fn sqrtf16(x: f16) -> f16;
1029-
/// Returns the square root of an `f32`
1030-
///
1031-
/// The stabilized version of this intrinsic is
1032-
/// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1033-
#[rustc_intrinsic]
1034-
#[rustc_nounwind]
1035-
pub fn sqrtf32(x: f32) -> f32;
1036-
/// Returns the square root of an `f64`
1037-
///
1038-
/// The stabilized version of this intrinsic is
1039-
/// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1040-
#[rustc_intrinsic]
1041-
#[rustc_nounwind]
1042-
pub fn sqrtf64(x: f64) -> f64;
1043-
/// Returns the square root of an `f128`
1044-
///
1045-
/// The stabilized version of this intrinsic is
1046-
/// [`f128::sqrt`](../../std/primitive.f128.html#method.sqrt)
1024+
/// The stabilized versions of this intrinsic are available on the float
1025+
/// primitives via the `sqrt` method. For example, [`f32::sqrt`].
10471026
#[rustc_intrinsic]
10481027
#[rustc_nounwind]
1049-
pub fn sqrtf128(x: f128) -> f128;
1028+
pub fn sqrt<T: bounds::FloatPrimitive>(x: T) -> T;
10501029

10511030
/// Raises an `f16` to an integer power.
10521031
///

library/core/src/num/f128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,6 @@ impl f128 {
19271927
#[unstable(feature = "f128", issue = "116909")]
19281928
#[must_use = "method returns a new number and does not mutate the original value"]
19291929
pub fn sqrt(self) -> f128 {
1930-
intrinsics::sqrtf128(self)
1930+
intrinsics::sqrt(self)
19311931
}
19321932
}

library/core/src/num/f16.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ impl f16 {
19111911
#[unstable(feature = "f16", issue = "116909")]
19121912
#[must_use = "method returns a new number and does not mutate the original value"]
19131913
pub fn sqrt(self) -> f16 {
1914-
intrinsics::sqrtf16(self)
1914+
intrinsics::sqrt(self)
19151915
}
19161916

19171917
/// Returns the cube root of a number.

library/core/src/num/f32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ pub mod math {
20392039
#[unstable(feature = "core_float_math", issue = "137578")]
20402040
#[must_use = "method returns a new number and does not mutate the original value"]
20412041
pub fn sqrt(x: f32) -> f32 {
2042-
intrinsics::sqrtf32(x)
2042+
intrinsics::sqrt(x)
20432043
}
20442044

20452045
/// Experimental version of `abs_sub` in `core`. See [`f32::abs_sub`] for details.

library/core/src/num/f64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ pub mod math {
20372037
#[unstable(feature = "core_float_math", issue = "137578")]
20382038
#[must_use = "method returns a new number and does not mutate the original value"]
20392039
pub fn sqrt(x: f64) -> f64 {
2040-
intrinsics::sqrtf64(x)
2040+
intrinsics::sqrt(x)
20412041
}
20422042

20432043
/// Experimental version of `abs_sub` in `core`. See [`f64::abs_sub`] for details.

0 commit comments

Comments
 (0)