Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
269667e1
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
269667e1
编写于
6月 22, 2017
作者:
R
Ralf Jung
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implement _nonzero intrinsics
上级
1a1d741d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
55 addition
and
6 deletion
+55
-6
src/error.rs
src/error.rs
+5
-0
src/terminator/intrinsic.rs
src/terminator/intrinsic.rs
+13
-6
tests/run-pass/intrinsics-integer.rs
tests/run-pass/intrinsics-integer.rs
+37
-0
未找到文件。
src/error.rs
浏览文件 @
269667e1
...
...
@@ -33,6 +33,7 @@ pub enum EvalError<'tcx> {
ExecuteMemory
,
ArrayIndexOutOfBounds
(
Span
,
u64
,
u64
),
Math
(
Span
,
ConstMathErr
),
Intrinsic
(
String
),
OverflowingMath
,
InvalidChar
(
u128
),
OutOfMemory
{
...
...
@@ -104,6 +105,8 @@ fn description(&self) -> &str {
"array index out of bounds"
,
EvalError
::
Math
(
..
)
=>
"mathematical operation failed"
,
EvalError
::
Intrinsic
(
..
)
=>
"intrinsic failed"
,
EvalError
::
OverflowingMath
=>
"attempted to do overflowing math"
,
EvalError
::
NoMirFor
(
..
)
=>
...
...
@@ -168,6 +171,8 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!
(
f
,
"index out of bounds: the len is {} but the index is {} at {:?}"
,
len
,
index
,
span
),
EvalError
::
Math
(
span
,
ref
err
)
=>
write!
(
f
,
"{:?} at {:?}"
,
err
,
span
),
EvalError
::
Intrinsic
(
ref
err
)
=>
write!
(
f
,
"{}"
,
err
),
EvalError
::
InvalidChar
(
c
)
=>
write!
(
f
,
"tried to interpret an invalid 32-bit value as a char: {}"
,
c
),
EvalError
::
OutOfMemory
{
allocation_size
,
memory_size
,
memory_usage
}
=>
...
...
src/terminator/intrinsic.rs
浏览文件 @
269667e1
...
...
@@ -154,12 +154,21 @@ pub(super) fn call_intrinsic(
"ctpop"
|
"cttz"
|
"cttz_nonzero"
|
"ctlz"
|
"ctlz_nonzero"
|
"bswap"
=>
{
let
ty
=
substs
.type_at
(
0
);
let
num
=
self
.value_to_primval
(
arg_vals
[
0
],
ty
)
?
;
let
num
=
self
.value_to_primval
(
arg_vals
[
0
],
ty
)
?
.to_bytes
()
?
;
let
kind
=
self
.ty_to_primval_kind
(
ty
)
?
;
let
num
=
numeric_intrinsic
(
intrinsic_name
,
num
,
kind
)
?
;
let
num
=
if
intrinsic_name
.ends_with
(
"_nonzero"
)
{
if
num
==
0
{
return
Err
(
EvalError
::
Intrinsic
(
format!
(
"{} called on 0"
,
intrinsic_name
)))
}
numeric_intrinsic
(
intrinsic_name
.trim_right_matches
(
"_nonzero"
),
num
,
kind
)
?
}
else
{
numeric_intrinsic
(
intrinsic_name
,
num
,
kind
)
?
};
self
.write_primval
(
dest
,
num
,
ty
)
?
;
}
...
...
@@ -538,13 +547,11 @@ fn field_ty(
fn
numeric_intrinsic
<
'tcx
>
(
name
:
&
str
,
val
:
PrimVal
,
bytes
:
u128
,
kind
:
PrimValKind
)
->
EvalResult
<
'tcx
,
PrimVal
>
{
macro_rules!
integer_intrinsic
{
(
$method:ident
)
=>
({
let
bytes
=
val
.to_bytes
()
?
;
use
value
::
PrimValKind
::
*
;
let
result_bytes
=
match
kind
{
I8
=>
(
bytes
as
i8
)
.
$method
()
as
u128
,
...
...
@@ -557,7 +564,7 @@ fn numeric_intrinsic<'tcx>(
U64
=>
(
bytes
as
u64
)
.
$method
()
as
u128
,
I128
=>
(
bytes
as
i128
)
.
$method
()
as
u128
,
U128
=>
bytes
.
$method
()
as
u128
,
_
=>
bug!
(
"invalid `{}` argument: {:?}"
,
name
,
val
),
_
=>
bug!
(
"invalid `{}` argument: {:?}"
,
name
,
bytes
),
};
PrimVal
::
Bytes
(
result_bytes
)
...
...
tests/run-pass/intrinsics-integer.rs
浏览文件 @
269667e1
...
...
@@ -14,7 +14,9 @@ mod rusti {
extern
"rust-intrinsic"
{
pub
fn
ctpop
<
T
>
(
x
:
T
)
->
T
;
pub
fn
ctlz
<
T
>
(
x
:
T
)
->
T
;
pub
fn
ctlz_nonzero
<
T
>
(
x
:
T
)
->
T
;
pub
fn
cttz
<
T
>
(
x
:
T
)
->
T
;
pub
fn
cttz_nonzero
<
T
>
(
x
:
T
)
->
T
;
pub
fn
bswap
<
T
>
(
x
:
T
)
->
T
;
}
}
...
...
@@ -68,6 +70,21 @@ pub fn main() {
assert_eq!
(
ctlz
(
100u32
),
25
);
assert_eq!
(
ctlz
(
100i32
),
25
);
assert_eq!
(
ctlz
(
100u64
),
57
);
assert_eq!
(
ctlz
(
100i64
),
57
);
assert_eq!
(
ctlz_nonzero
(
1u8
),
7
);
assert_eq!
(
ctlz_nonzero
(
1i8
),
7
);
assert_eq!
(
ctlz_nonzero
(
1u16
),
15
);
assert_eq!
(
ctlz_nonzero
(
1i16
),
15
);
assert_eq!
(
ctlz_nonzero
(
1u32
),
31
);
assert_eq!
(
ctlz_nonzero
(
1i32
),
31
);
assert_eq!
(
ctlz_nonzero
(
1u64
),
63
);
assert_eq!
(
ctlz_nonzero
(
1i64
),
63
);
assert_eq!
(
ctlz_nonzero
(
10u8
),
4
);
assert_eq!
(
ctlz_nonzero
(
10i8
),
4
);
assert_eq!
(
ctlz_nonzero
(
10u16
),
12
);
assert_eq!
(
ctlz_nonzero
(
10i16
),
12
);
assert_eq!
(
ctlz_nonzero
(
10u32
),
28
);
assert_eq!
(
ctlz_nonzero
(
10i32
),
28
);
assert_eq!
(
ctlz_nonzero
(
10u64
),
60
);
assert_eq!
(
ctlz_nonzero
(
10i64
),
60
);
assert_eq!
(
ctlz_nonzero
(
100u8
),
1
);
assert_eq!
(
ctlz_nonzero
(
100i8
),
1
);
assert_eq!
(
ctlz_nonzero
(
100u16
),
9
);
assert_eq!
(
ctlz_nonzero
(
100i16
),
9
);
assert_eq!
(
ctlz_nonzero
(
100u32
),
25
);
assert_eq!
(
ctlz_nonzero
(
100i32
),
25
);
assert_eq!
(
ctlz_nonzero
(
100u64
),
57
);
assert_eq!
(
ctlz_nonzero
(
100i64
),
57
);
assert_eq!
(
cttz
(
-
1i8
as
u8
),
0
);
assert_eq!
(
cttz
(
-
1i8
),
0
);
assert_eq!
(
cttz
(
-
1i16
as
u16
),
0
);
assert_eq!
(
cttz
(
-
1i16
),
0
);
assert_eq!
(
cttz
(
-
1i32
as
u32
),
0
);
assert_eq!
(
cttz
(
-
1i32
),
0
);
...
...
@@ -93,6 +110,26 @@ pub fn main() {
assert_eq!
(
cttz
(
100u32
),
2
);
assert_eq!
(
cttz
(
100i32
),
2
);
assert_eq!
(
cttz
(
100u64
),
2
);
assert_eq!
(
cttz
(
100i64
),
2
);
assert_eq!
(
cttz_nonzero
(
-
1i8
as
u8
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i8
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i16
as
u16
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i16
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i32
as
u32
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i32
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i64
as
u64
),
0
);
assert_eq!
(
cttz_nonzero
(
-
1i64
),
0
);
assert_eq!
(
cttz_nonzero
(
1u8
),
0
);
assert_eq!
(
cttz_nonzero
(
1i8
),
0
);
assert_eq!
(
cttz_nonzero
(
1u16
),
0
);
assert_eq!
(
cttz_nonzero
(
1i16
),
0
);
assert_eq!
(
cttz_nonzero
(
1u32
),
0
);
assert_eq!
(
cttz_nonzero
(
1i32
),
0
);
assert_eq!
(
cttz_nonzero
(
1u64
),
0
);
assert_eq!
(
cttz_nonzero
(
1i64
),
0
);
assert_eq!
(
cttz_nonzero
(
10u8
),
1
);
assert_eq!
(
cttz_nonzero
(
10i8
),
1
);
assert_eq!
(
cttz_nonzero
(
10u16
),
1
);
assert_eq!
(
cttz_nonzero
(
10i16
),
1
);
assert_eq!
(
cttz_nonzero
(
10u32
),
1
);
assert_eq!
(
cttz_nonzero
(
10i32
),
1
);
assert_eq!
(
cttz_nonzero
(
10u64
),
1
);
assert_eq!
(
cttz_nonzero
(
10i64
),
1
);
assert_eq!
(
cttz_nonzero
(
100u8
),
2
);
assert_eq!
(
cttz_nonzero
(
100i8
),
2
);
assert_eq!
(
cttz_nonzero
(
100u16
),
2
);
assert_eq!
(
cttz_nonzero
(
100i16
),
2
);
assert_eq!
(
cttz_nonzero
(
100u32
),
2
);
assert_eq!
(
cttz_nonzero
(
100i32
),
2
);
assert_eq!
(
cttz_nonzero
(
100u64
),
2
);
assert_eq!
(
cttz_nonzero
(
100i64
),
2
);
assert_eq!
(
bswap
(
0x0Au8
),
0x0A
);
// no-op
assert_eq!
(
bswap
(
0x0Ai8
),
0x0A
);
// no-op
assert_eq!
(
bswap
(
0x0A0Bu16
),
0x0B0A
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录