]> git.lizzy.rs Git - rust.git/commitdiff
Allow SIMD types in generics. Closes #10604
authorRichard Diamond <wichard@vitalitystudios.com>
Wed, 22 Jan 2014 05:12:41 +0000 (23:12 -0600)
committerRichard Diamond <wichard@vitalitystudios.com>
Wed, 22 Jan 2014 05:13:48 +0000 (23:13 -0600)
src/librustc/middle/trans/adt.rs
src/test/run-pass/simd-generics.rs [new file with mode: 0644]
src/test/run-pass/simd-issue-10604.rs [new file with mode: 0644]

index 7b194690b2f23efbbc85c2fc0aeba98512f79197..5a3d226fa3c0a14c6dfc62c4cfb40bd4975f7315 100644 (file)
@@ -46,6 +46,7 @@
 use std::container::Map;
 use std::libc::c_ulonglong;
 use std::option::{Option, Some, None};
+use std::num::{Bitwise};
 
 use lib::llvm::{ValueRef, True, IntEQ, IntNE};
 use middle::trans::_match;
@@ -424,19 +425,22 @@ fn generic_type_of(cx: &CrateContext, r: &Repr, name: Option<&str>, sizing: bool
             let align = most_aligned.align;
             let discr_ty = ll_inttype(cx, ity);
             let discr_size = machine::llsize_of_alloc(cx, discr_ty) as u64;
+            let align_units = (size + align - 1) / align - 1;
             let pad_ty = match align {
-                1 => Type::i8(),
-                2 => Type::i16(),
-                4 => Type::i32(),
-                8 if machine::llalign_of_min(cx, Type::i64()) == 8 => Type::i64(),
+                1 => Type::array(&Type::i8(), align_units),
+                2 => Type::array(&Type::i16(), align_units),
+                4 => Type::array(&Type::i32(), align_units),
+                8 if machine::llalign_of_min(cx, Type::i64()) == 8 =>
+                                 Type::array(&Type::i64(), align_units),
+                a if a.population_count() == 1 => Type::array(&Type::vector(&Type::i32(), a / 4),
+                                                              align_units),
                 _ => fail!("Unsupported enum alignment: {:?}", align)
             };
             assert_eq!(machine::llalign_of_min(cx, pad_ty) as u64, align);
-            let align_units = (size + align - 1) / align;
             assert_eq!(align % discr_size, 0);
             let fields = ~[discr_ty,
-              Type::array(&discr_ty, align / discr_size - 1),
-              Type::array(&pad_ty, align_units - 1)];
+                           Type::array(&discr_ty, align / discr_size - 1),
+                           pad_ty];
             match name {
                 None => Type::struct_(fields, false),
                 Some(name) => {
diff --git a/src/test/run-pass/simd-generics.rs b/src/test/run-pass/simd-generics.rs
new file mode 100644 (file)
index 0000000..c6d5f91
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::ops;
+
+#[simd] struct f32x4(f32, f32, f32, f32);
+
+fn add<T: ops::Add<T, T>>(lhs: T, rhs: T) -> T {
+    lhs + rhs
+}
+
+impl ops::Add<f32x4, f32x4> for f32x4 {
+    fn add(&self, rhs: &f32x4) -> f32x4 {
+        *self + *rhs
+    }
+}
+
+fn main() {
+    let lr = f32x4(1.0f32, 2.0f32, 3.0f32, 4.0f32);
+
+    // lame-o
+    let f32x4(x, y, z, w) = add(lr, lr);
+    assert_eq!(x, 2.0f32);
+    assert_eq!(y, 4.0f32);
+    assert_eq!(z, 6.0f32);
+    assert_eq!(w, 8.0f32);
+}
diff --git a/src/test/run-pass/simd-issue-10604.rs b/src/test/run-pass/simd-issue-10604.rs
new file mode 100644 (file)
index 0000000..49aa87f
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    let _o = None::<std::unstable::simd::i32x4>;
+}