Skip to content

Commit d56c976

Browse files
committed
auto merge of #7705 : sanxiyn/rust/simd-op, r=cmr
Fix #3499. This is the other half of #5841 that was left out when I revised it to #6214.
2 parents 0939477 + 2bc06b4 commit d56c976

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/librustc/middle/trans/expr.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1380,11 +1380,16 @@ fn trans_eager_binop(bcx: block,
13801380
let rhs = rhs_datum.to_appropriate_llval(bcx);
13811381
let rhs_t = rhs_datum.ty;
13821382

1383-
let intype = {
1383+
let mut intype = {
13841384
if ty::type_is_bot(lhs_t) { rhs_t }
13851385
else { lhs_t }
13861386
};
1387+
let tcx = bcx.tcx();
1388+
if ty::type_is_simd(tcx, intype) {
1389+
intype = ty::simd_type(tcx, intype);
1390+
}
13871391
let is_float = ty::type_is_fp(intype);
1392+
let signed = ty::type_is_signed(intype);
13881393

13891394
let rhs = base::cast_shift_expr_rhs(bcx, op, lhs, rhs);
13901395

@@ -1409,7 +1414,7 @@ fn trans_eager_binop(bcx: block,
14091414
// Only zero-check integers; fp /0 is NaN
14101415
bcx = base::fail_if_zero(bcx, binop_expr.span,
14111416
op, rhs, rhs_t);
1412-
if ty::type_is_signed(intype) {
1417+
if signed {
14131418
SDiv(bcx, lhs, rhs)
14141419
} else {
14151420
UDiv(bcx, lhs, rhs)
@@ -1423,7 +1428,7 @@ fn trans_eager_binop(bcx: block,
14231428
// Only zero-check integers; fp %0 is NaN
14241429
bcx = base::fail_if_zero(bcx, binop_expr.span,
14251430
op, rhs, rhs_t);
1426-
if ty::type_is_signed(intype) {
1431+
if signed {
14271432
SRem(bcx, lhs, rhs)
14281433
} else {
14291434
URem(bcx, lhs, rhs)
@@ -1435,7 +1440,7 @@ fn trans_eager_binop(bcx: block,
14351440
ast::bitxor => Xor(bcx, lhs, rhs),
14361441
ast::shl => Shl(bcx, lhs, rhs),
14371442
ast::shr => {
1438-
if ty::type_is_signed(intype) {
1443+
if signed {
14391444
AShr(bcx, lhs, rhs)
14401445
} else { LShr(bcx, lhs, rhs) }
14411446
}

src/librustc/middle/ty.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,8 @@ fn type_is_newtype_immediate(cx: ctxt, ty: t) -> bool {
16621662
pub fn type_is_immediate(cx: ctxt, ty: t) -> bool {
16631663
return type_is_scalar(ty) || type_is_boxed(ty) ||
16641664
type_is_unique(ty) || type_is_region_ptr(ty) ||
1665-
type_is_newtype_immediate(cx, ty);
1665+
type_is_newtype_immediate(cx, ty) ||
1666+
type_is_simd(cx, ty);
16661667
}
16671668

16681669
pub fn type_needs_drop(cx: ctxt, ty: t) -> bool {
@@ -4075,7 +4076,7 @@ pub fn struct_fields(cx: ctxt, did: ast::def_id, substs: &substs)
40754076
}
40764077
}
40774078

4078-
pub fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
4079+
pub fn is_binopable(cx: ctxt, ty: t, op: ast::binop) -> bool {
40794080
static tycat_other: int = 0;
40804081
static tycat_bool: int = 1;
40814082
static tycat_int: int = 2;
@@ -4115,7 +4116,10 @@ pub fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
41154116
}
41164117
}
41174118

4118-
fn tycat(ty: t) -> int {
4119+
fn tycat(cx: ctxt, ty: t) -> int {
4120+
if type_is_simd(cx, ty) {
4121+
return tycat(cx, simd_type(cx, ty))
4122+
}
41194123
match get(ty).sty {
41204124
ty_bool => tycat_bool,
41214125
ty_int(_) | ty_uint(_) | ty_infer(IntVar(_)) => tycat_int,
@@ -4140,7 +4144,7 @@ pub fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
41404144
/*bot*/ ~[f, f, f, f, f, f, f, f],
41414145
/*struct*/ ~[t, t, t, t, f, f, t, t]];
41424146

4143-
return tbl[tycat(ty)][opcat(op)];
4147+
return tbl[tycat(cx, ty)][opcat(op)];
41444148
}
41454149

41464150
pub fn ty_params_to_tys(tcx: ty::ctxt, generics: &ast::Generics) -> ~[t] {

src/test/run-pass/simd-binop.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::unstable::simd::{i32x4, f32x4};
12+
13+
fn test_int(e: i32) -> i32 {
14+
let v = i32x4(e, 0i32, 0i32, 0i32);
15+
let i32x4(e2, _, _, _) = v * v + v - v;
16+
e2
17+
}
18+
19+
fn test_float(e: f32) -> f32 {
20+
let v = f32x4(e, 0f32, 0f32, 0f32);
21+
let f32x4(e2, _, _, _) = v * v + v - v;
22+
e2
23+
}
24+
25+
fn main() {
26+
assert_eq!(test_int(3i32), 9i32);
27+
assert_eq!(test_float(3f32), 9f32);
28+
}

0 commit comments

Comments
 (0)