File: bounds_check_elim.rs

package info (click to toggle)
rust-ndarray 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: sh: 30; makefile: 2
file content (118 lines) | stat: -rw-r--r-- 1,905 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![crate_type = "lib"]
#![allow(
    clippy::many_single_char_names, clippy::deref_addrof, clippy::unreadable_literal, clippy::many_single_char_names
)]

// Test cases for bounds check elimination

use ndarray::prelude::*;

/*
pub fn testslice(a: &[f64]) -> f64 {
    let mut sum = 0.;
    for i in 0..a.len() {
        sum += a[i];
    }
    sum
}

pub fn testvec(a: &Vec<f64>) -> f64 {
    let mut sum = 0.;
    for i in 0..a.len() {
        sum += a[i];
    }
    sum
}

pub fn testvec_as_slice(a: &Vec<f64>) -> f64 {
    let a = a.as_slice();
    let mut sum = 0.;
    for i in 0..a.len() {
        sum += a[i];
    }
    sum
}
*/

#[no_mangle]
pub fn test1d_single(a: &Array1<f64>, i: usize) -> f64
{
    if i < a.len() {
        a[i]
    } else {
        0.
    }
}

#[no_mangle]
pub fn test1d_single_mut(a: &mut Array1<f64>, i: usize) -> f64
{
    if i < a.len() {
        *&mut a[i]
    } else {
        0.
    }
}

#[no_mangle]
pub fn test1d_len_of(a: &Array1<f64>) -> f64
{
    let a = &*a;
    let mut sum = 0.;
    for i in 0..a.len_of(Axis(0)) {
        sum += a[i];
    }
    sum
}

#[no_mangle]
pub fn test1d_range(a: &Array1<f64>) -> f64
{
    let mut sum = 0.;
    for i in 0..a.len() {
        sum += a[i];
    }
    sum
}

#[no_mangle]
pub fn test1d_while(a: &Array1<f64>) -> f64
{
    let mut sum = 0.;
    let mut i = 0;
    while i < a.len() {
        sum += a[i];
        i += 1;
    }
    sum
}

#[no_mangle]
pub fn test2d_ranges(a: &Array2<f64>) -> f64
{
    let mut sum = 0.;
    for i in 0..a.nrows() {
        for j in 0..a.ncols() {
            sum += a[[i, j]];
        }
    }
    sum
}

#[no_mangle]
pub fn test2d_whiles(a: &Array2<f64>) -> f64
{
    let mut sum = 0.;
    let mut i = 0;
    while i < a.nrows() {
        let mut j = 0;
        while j < a.ncols() {
            sum += a[[i, j]];
            j += 1;
        }
        i += 1;
    }
    sum
}

fn main() {}