Skip to content

Allow SIMD types in generics. Closes #10604 #11717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/librustc/middle/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if size == n * align? Doesn't this give n - 1? (Is this correct?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be (n * align - 1) / align. Either way, all that was changed was I moved that line from 435 to 428 and added the minus one from 439. That was so I could use it in the match arms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is n - 1, which is right — this patch also changes align_units from being the size of the entire object to the size of the padding.

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) => {
Expand Down
38 changes: 38 additions & 0 deletions src/test/run-pass/simd-generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.

// xfail-fast

#[feature(simd)];

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
}
}

pub 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);
}
18 changes: 18 additions & 0 deletions src/test/run-pass/simd-issue-10604.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

// xfail-fast

#[allow(experimental)];
#[feature(simd)];

pub fn main() {
let _o = None::<std::unstable::simd::i32x4>;
}