Skip to content

Commit c64a87c

Browse files
committed
Genericize sin and cos
1 parent 6efc2f3 commit c64a87c

10 files changed

Lines changed: 43 additions & 105 deletions

File tree

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,6 @@ fn codegen_float_intrinsic_call<'tcx>(
355355
sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32, types::F32),
356356
sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64, types::F64),
357357
sym::copysignf128 => ("copysignf128", 2, fx.tcx.types.f128, types::F128),
358-
sym::sinf16 => ("sinf16", 1, fx.tcx.types.f16, types::F16),
359-
sym::sinf32 => ("sinf", 1, fx.tcx.types.f32, types::F32),
360-
sym::sinf64 => ("sin", 1, fx.tcx.types.f64, types::F64),
361-
sym::sinf128 => ("sinf128", 1, fx.tcx.types.f128, types::F128),
362-
sym::cosf16 => ("cosf16", 1, fx.tcx.types.f16, types::F16),
363-
sym::cosf32 => ("cosf", 1, fx.tcx.types.f32, types::F32),
364-
sym::cosf64 => ("cos", 1, fx.tcx.types.f64, types::F64),
365-
sym::cosf128 => ("cosf128", 1, fx.tcx.types.f128, types::F128),
366358
_ => return false,
367359
};
368360

@@ -1117,6 +1109,8 @@ fn codegen_regular_intrinsic_call<'tcx>(
11171109
// Float unop intrinsics
11181110
sym::fabs
11191111
| sym::sqrt
1112+
| sym::sin
1113+
| sym::cos
11201114
| sym::floor
11211115
| sym::ceil
11221116
| sym::trunc
@@ -1144,6 +1138,14 @@ fn codegen_regular_intrinsic_call<'tcx>(
11441138
(sym::sqrt, F32 | F64) => Ok(fx.bcx.ins().sqrt(x)),
11451139
(sym::sqrt, F16) => Err("sqrtf16"),
11461140
(sym::sqrt, F128) => Err("sqrtf128"),
1141+
(sym::sin, F16) => Err("sinf16"),
1142+
(sym::sin, F32) => Err("sinf"),
1143+
(sym::sin, F64) => Err("sin"),
1144+
(sym::sin, F128) => Err("sinf128"),
1145+
(sym::cos, F16) => Err("cosf16"),
1146+
(sym::cos, F32) => Err("cosf"),
1147+
(sym::cos, F64) => Err("cos"),
1148+
(sym::cos, F128) => Err("cosf128"),
11471149
(sym::floor, F32 | F64) => Ok(fx.bcx.ins().floor(x)),
11481150
(sym::floor, F16) => Err("floorf16"),
11491151
(sym::floor, F128) => Err("floorf128"),

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ fn get_simple_intrinsic<'gcc, 'tcx>(
4747
let gcc_name = match name {
4848
sym::powif32 => "__builtin_powif",
4949
sym::powif64 => "__builtin_powi",
50-
sym::sinf32 => "sinf",
51-
sym::sinf64 => "sin",
52-
sym::cosf32 => "cosf",
53-
sym::cosf64 => "cos",
5450
sym::powf32 => "powf",
5551
sym::powf64 => "pow",
5652
sym::expf32 => "expf",
@@ -421,6 +417,8 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
421417
// float unary intrinsics
422418
sym::fabs
423419
| sym::sqrt
420+
| sym::sin
421+
| sym::cos
424422
| sym::floor
425423
| sym::ceil
426424
| sym::trunc
@@ -449,6 +447,10 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
449447
(sym::sqrt, F32) => Builtin("sqrtf"),
450448
(sym::sqrt, F64) => Builtin("sqrt"),
451449
(sym::sqrt, F128) => Extern("sqrtf128"),
450+
(sym::sin, F32) => Builtin("sinf"),
451+
(sym::sin, F64) => Builtin("sin"),
452+
(sym::cos, F32) => Builtin("cosf"),
453+
(sym::cos, F64) => Builtin("cos"),
452454
(sym::floor, F16) => BuiltinF16Cast("__builtin_floorf"),
453455
(sym::floor, F32) => Builtin("floorf"),
454456
(sym::floor, F64) => Builtin("floor"),

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ fn call_simple_intrinsic<'ll, 'tcx>(
5353
sym::powif64 => ("llvm.powi", &[bx.type_f64(), bx.type_i32()]),
5454
sym::powif128 => ("llvm.powi", &[bx.type_f128(), bx.type_i32()]),
5555

56-
sym::sinf16 => ("llvm.sin", &[bx.type_f16()]),
57-
sym::sinf32 => ("llvm.sin", &[bx.type_f32()]),
58-
sym::sinf64 => ("llvm.sin", &[bx.type_f64()]),
59-
sym::sinf128 => ("llvm.sin", &[bx.type_f128()]),
60-
61-
sym::cosf16 => ("llvm.cos", &[bx.type_f16()]),
62-
sym::cosf32 => ("llvm.cos", &[bx.type_f32()]),
63-
sym::cosf64 => ("llvm.cos", &[bx.type_f64()]),
64-
sym::cosf128 => ("llvm.cos", &[bx.type_f128()]),
65-
6656
sym::powf16 => ("llvm.pow", &[bx.type_f16()]),
6757
sym::powf32 => ("llvm.pow", &[bx.type_f32()]),
6858
sym::powf64 => ("llvm.pow", &[bx.type_f64()]),
@@ -466,6 +456,8 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
466456

467457
sym::fabs
468458
| sym::sqrt
459+
| sym::sin
460+
| sym::cos
469461
| sym::floor
470462
| sym::ceil
471463
| sym::trunc
@@ -479,6 +471,8 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
479471
let llvm_name = match name {
480472
sym::fabs => "llvm.fabs",
481473
sym::sqrt => "llvm.sqrt",
474+
sym::sin => "llvm.sin",
475+
sym::cos => "llvm.cos",
482476
sym::floor => "llvm.floor",
483477
sym::ceil => "llvm.ceil",
484478
sym::trunc => "llvm.trunc",

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
9292
| sym::copysignf32
9393
| sym::copysignf64
9494
| sym::copysignf128
95-
| sym::cosf16
96-
| sym::cosf32
97-
| sym::cosf64
98-
| sym::cosf128
95+
| sym::cos
9996
| sym::ctlz
10097
| sym::ctpop
10198
| sym::cttz
@@ -182,10 +179,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
182179
| sym::saturating_add
183180
| sym::saturating_sub
184181
| sym::select_unpredictable
185-
| sym::sinf16
186-
| sym::sinf32
187-
| sym::sinf64
188-
| sym::sinf128
182+
| sym::sin
189183
| sym::size_of
190184
| sym::sqrt
191185
| sym::sub_with_overflow
@@ -385,15 +379,7 @@ pub(crate) fn check_intrinsic_type(
385379
sym::powif64 => (0, 0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
386380
sym::powif128 => (0, 0, vec![tcx.types.f128, tcx.types.i32], tcx.types.f128),
387381

388-
sym::sinf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
389-
sym::sinf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
390-
sym::sinf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
391-
sym::sinf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
392-
393-
sym::cosf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
394-
sym::cosf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
395-
sym::cosf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
396-
sym::cosf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
382+
sym::sin | sym::cos => (1, 0, vec![param(0)], param(0)),
397383

398384
sym::powf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
399385
sym::powf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,7 @@ symbols! {
712712
coroutine_state,
713713
coroutine_yield,
714714
coroutines,
715-
cosf16,
716-
cosf32,
717-
cosf64,
718-
cosf128,
715+
cos,
719716
count,
720717
coverage,
721718
coverage_attribute,
@@ -1891,11 +1888,8 @@ symbols! {
18911888
simd_trunc,
18921889
simd_with_exposed_provenance,
18931890
simd_xor,
1891+
sin,
18941892
since,
1895-
sinf16,
1896-
sinf32,
1897-
sinf64,
1898-
sinf128,
18991893
size,
19001894
size_of,
19011895
size_of_val,

library/core/src/intrinsics/mod.rs

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,63 +1057,23 @@ pub fn powif64(a: f64, x: i32) -> f64;
10571057
#[rustc_nounwind]
10581058
pub fn powif128(a: f128, x: i32) -> f128;
10591059

1060-
/// Returns the sine of an `f16`.
1060+
/// Returns the sine of a floating point value.
10611061
///
1062-
/// The stabilized version of this intrinsic is
1063-
/// [`f16::sin`](../../std/primitive.f16.html#method.sin)
1064-
#[rustc_intrinsic]
1065-
#[rustc_nounwind]
1066-
pub fn sinf16(x: f16) -> f16;
1067-
/// Returns the sine of an `f32`.
1068-
///
1069-
/// The stabilized version of this intrinsic is
1070-
/// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1071-
#[rustc_intrinsic]
1072-
#[rustc_nounwind]
1073-
pub fn sinf32(x: f32) -> f32;
1074-
/// Returns the sine of an `f64`.
1075-
///
1076-
/// The stabilized version of this intrinsic is
1077-
/// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1078-
#[rustc_intrinsic]
1079-
#[rustc_nounwind]
1080-
pub fn sinf64(x: f64) -> f64;
1081-
/// Returns the sine of an `f128`.
1082-
///
1083-
/// The stabilized version of this intrinsic is
1084-
/// [`f128::sin`](../../std/primitive.f128.html#method.sin)
1062+
/// The stabilized versions of this intrinsic are available on the float
1063+
/// primitives via the `sin` method.
1064+
/// For example, [`f32::sin`](../../std/primitive.f32.html#method.sin).
10851065
#[rustc_intrinsic]
10861066
#[rustc_nounwind]
1087-
pub fn sinf128(x: f128) -> f128;
1067+
pub fn sin<T: bounds::FloatPrimitive>(x: T) -> T;
10881068

1089-
/// Returns the cosine of an `f16`.
1069+
/// Returns the cosine of a floating point value.
10901070
///
1091-
/// The stabilized version of this intrinsic is
1092-
/// [`f16::cos`](../../std/primitive.f16.html#method.cos)
1093-
#[rustc_intrinsic]
1094-
#[rustc_nounwind]
1095-
pub fn cosf16(x: f16) -> f16;
1096-
/// Returns the cosine of an `f32`.
1097-
///
1098-
/// The stabilized version of this intrinsic is
1099-
/// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1100-
#[rustc_intrinsic]
1101-
#[rustc_nounwind]
1102-
pub fn cosf32(x: f32) -> f32;
1103-
/// Returns the cosine of an `f64`.
1104-
///
1105-
/// The stabilized version of this intrinsic is
1106-
/// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1107-
#[rustc_intrinsic]
1108-
#[rustc_nounwind]
1109-
pub fn cosf64(x: f64) -> f64;
1110-
/// Returns the cosine of an `f128`.
1111-
///
1112-
/// The stabilized version of this intrinsic is
1113-
/// [`f128::cos`](../../std/primitive.f128.html#method.cos)
1071+
/// The stabilized versions of this intrinsic are available on the float
1072+
/// primitives via the `cos` method.
1073+
/// For example, [`f32::cos`](../../std/primitive.f32.html#method.cos).
11141074
#[rustc_intrinsic]
11151075
#[rustc_nounwind]
1116-
pub fn cosf128(x: f128) -> f128;
1076+
pub fn cos<T: bounds::FloatPrimitive>(x: T) -> T;
11171077

11181078
/// Raises an `f16` to an `f16` power.
11191079
///

library/std/src/num/f128.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl f128 {
392392
#[unstable(feature = "f128", issue = "116909")]
393393
#[must_use = "method returns a new number and does not mutate the original value"]
394394
pub fn sin(self) -> f128 {
395-
intrinsics::sinf128(self)
395+
intrinsics::sin(self)
396396
}
397397

398398
/// Computes the cosine of a number (in radians).
@@ -421,7 +421,7 @@ impl f128 {
421421
#[unstable(feature = "f128", issue = "116909")]
422422
#[must_use = "method returns a new number and does not mutate the original value"]
423423
pub fn cos(self) -> f128 {
424-
intrinsics::cosf128(self)
424+
intrinsics::cos(self)
425425
}
426426

427427
/// Computes the tangent of a number (in radians).

library/std/src/num/f16.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl f16 {
357357
#[unstable(feature = "f16", issue = "116909")]
358358
#[must_use = "method returns a new number and does not mutate the original value"]
359359
pub fn sin(self) -> f16 {
360-
intrinsics::sinf16(self)
360+
intrinsics::sin(self)
361361
}
362362

363363
/// Computes the cosine of a number (in radians).
@@ -386,7 +386,7 @@ impl f16 {
386386
#[unstable(feature = "f16", issue = "116909")]
387387
#[must_use = "method returns a new number and does not mutate the original value"]
388388
pub fn cos(self) -> f16 {
389-
intrinsics::cosf16(self)
389+
intrinsics::cos(self)
390390
}
391391

392392
/// Computes the tangent of a number (in radians).

library/std/src/num/f32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl f32 {
697697
#[stable(feature = "rust1", since = "1.0.0")]
698698
#[inline]
699699
pub fn sin(self) -> f32 {
700-
intrinsics::sinf32(self)
700+
intrinsics::sin(self)
701701
}
702702

703703
/// Computes the cosine of a number (in radians).
@@ -721,7 +721,7 @@ impl f32 {
721721
#[stable(feature = "rust1", since = "1.0.0")]
722722
#[inline]
723723
pub fn cos(self) -> f32 {
724-
intrinsics::cosf32(self)
724+
intrinsics::cos(self)
725725
}
726726

727727
/// Computes the tangent of a number (in radians).

library/std/src/num/f64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl f64 {
697697
#[stable(feature = "rust1", since = "1.0.0")]
698698
#[inline]
699699
pub fn sin(self) -> f64 {
700-
intrinsics::sinf64(self)
700+
intrinsics::sin(self)
701701
}
702702

703703
/// Computes the cosine of a number (in radians).
@@ -721,7 +721,7 @@ impl f64 {
721721
#[stable(feature = "rust1", since = "1.0.0")]
722722
#[inline]
723723
pub fn cos(self) -> f64 {
724-
intrinsics::cosf64(self)
724+
intrinsics::cos(self)
725725
}
726726

727727
/// Computes the tangent of a number (in radians).

0 commit comments

Comments
 (0)