2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2020-11-08 10:21:28 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
2020-11-17 10:44:21 +00:00
|
|
|
#![feature(repr_simd, platform_intrinsics)]
|
2014-02-04 07:04:19 +00:00
|
|
|
|
2014-01-22 05:12:41 +00:00
|
|
|
use std::ops;
|
|
|
|
|
2015-08-12 23:09:02 +00:00
|
|
|
#[repr(simd)]
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2015-01-24 21:36:30 +00:00
|
|
|
struct f32x4(f32, f32, f32, f32);
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-06 01:01:33 +00:00
|
|
|
|
2019-07-13 15:16:57 +00:00
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
2021-08-03 03:33:09 +00:00
|
|
|
struct A<const N: usize>([f32; N]);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct B<T>([T; 4]);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct C<T, const N: usize>([T; N]);
|
2019-07-13 15:16:57 +00:00
|
|
|
|
|
|
|
|
2015-08-12 23:09:02 +00:00
|
|
|
extern "platform-intrinsic" {
|
|
|
|
fn simd_add<T>(x: T, y: T) -> T;
|
|
|
|
}
|
|
|
|
|
2014-12-31 20:45:13 +00:00
|
|
|
fn add<T: ops::Add<Output=T>>(lhs: T, rhs: T) -> T {
|
2014-01-22 05:12:41 +00:00
|
|
|
lhs + rhs
|
|
|
|
}
|
|
|
|
|
2014-12-31 20:45:13 +00:00
|
|
|
impl ops::Add for f32x4 {
|
|
|
|
type Output = f32x4;
|
|
|
|
|
2014-12-01 22:33:22 +00:00
|
|
|
fn add(self, rhs: f32x4) -> f32x4 {
|
2020-11-08 10:21:28 +00:00
|
|
|
unsafe { simd_add(self, rhs) }
|
2014-01-22 05:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:33:09 +00:00
|
|
|
impl ops::Add for A<4> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self {
|
|
|
|
unsafe { simd_add(self, rhs) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add for B<f32> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self {
|
|
|
|
unsafe { simd_add(self, rhs) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add for C<f32, 4> {
|
2019-07-13 15:16:57 +00:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self {
|
2020-11-08 10:21:28 +00:00
|
|
|
unsafe { simd_add(self, rhs) }
|
2019-07-13 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-08 10:21:28 +00:00
|
|
|
pub fn main() {
|
2021-08-03 03:33:09 +00:00
|
|
|
let x = [1.0f32, 2.0f32, 3.0f32, 4.0f32];
|
|
|
|
let y = [2.0f32, 4.0f32, 6.0f32, 8.0f32];
|
2014-01-22 05:12:41 +00:00
|
|
|
|
|
|
|
// lame-o
|
2021-08-03 03:33:09 +00:00
|
|
|
let a = f32x4(1.0f32, 2.0f32, 3.0f32, 4.0f32);
|
|
|
|
let f32x4(a0, a1, a2, a3) = add(a, a);
|
|
|
|
assert_eq!(a0, 2.0f32);
|
|
|
|
assert_eq!(a1, 4.0f32);
|
|
|
|
assert_eq!(a2, 6.0f32);
|
|
|
|
assert_eq!(a3, 8.0f32);
|
|
|
|
|
|
|
|
let a = A(x);
|
|
|
|
assert_eq!(add(a, a).0, y);
|
|
|
|
|
|
|
|
let b = B(x);
|
|
|
|
assert_eq!(add(b, b).0, y);
|
|
|
|
|
|
|
|
let c = C(x);
|
|
|
|
assert_eq!(add(c, c).0, y);
|
2020-11-08 10:21:28 +00:00
|
|
|
}
|