aboutsummaryrefslogtreecommitdiff
path: root/benchmarking/java-ubenchs/benchmarks/deprecated/MultiplyAdd.java
blob: 7ccd587be28493f7f3c2c82de1d7a376b87c0d38 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *    Copyright 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import java.lang.reflect.Method;

public class MultiplyAdd {
    public final static int ITERATIONS = 100000000;
    public final static int VALUE = 500;

    public static void main(String[] args) {
        long start, end = 0;
        MultiplyAdd test = new MultiplyAdd();

        Method[] methods = test.getClass().getDeclaredMethods();
        // sort methods by name
        for (int i = 0; i < methods.length; i++) {
            for (int j = 0; j < methods.length - i - 1; j++) {
                if (methods[j].getName().compareTo(methods[j + 1].getName()) > 0) {
                    Method tmp = methods[j];
                    methods[j] = methods[j + 1];
                    methods[j + 1] = tmp;
                }
            }
        }

        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            if (m.getName().startsWith("time")) {
                start = System.currentTimeMillis();
                end = start - 1;
                try {
                    Object o = m.invoke(test, ITERATIONS);
                    end = System.currentTimeMillis();
                } catch (Exception e) {
                    System.err.println("Invoke method: " + m.toGenericString());
                }
                System.out.println(m.getDeclaringClass().getName() + "."
                        + m.getName().substring(4) + ": " + (end - start));
            }
        }
    }

    public int timeSimpleMaddw(int iters) {
        int result = 0;
        for (int i = 0; i < iters; i++) {
            result += i * i;
        }
        return result;
    }

    public long timeSimpleMaddx(int iters) {
        long result = 0;
        for (int i = 0; i < iters; i++) {
            long tmp = i;
            result += tmp * tmp;
        }
        return result;
    }

    public int timeSimpleMsubw(int iters) {
        int result = 0;
        for (int i = 0; i < iters; i++) {
            result -= i * i;
        }
        return result;
    }

    public long timeSimpleMsubx(int iters) {
        long result = 0;
        for (int i = 0; i < iters; i++) {
            long tmp = i;
            result += tmp * tmp;
        }
        return result;
    }

    public int timeMaddwBack2back(int iters) {
        int result = 0;
        int tmp1 = 0;
        int tmp2 = 0;
        for (int i = 0; i < iters; i++) {
            tmp1 += i * i;
            tmp2 += tmp1 * tmp1;
            result += tmp2 * tmp2;
        }
        return result;
    }

    public long timeMaddxBack2back(int iters) {
        long result = 0;
        long tmp1 = 0;
        long tmp2 = 0;
        for (int i = 0; i < iters; i++) {
            long a = i;
            tmp1 += a * a;
            tmp2 += tmp1 * tmp1;
            result += tmp2 * tmp2;
        }
        return result;
    }

    public int timeMaddwInterleave(int iters) {
        int result = 0;
        int tmp1 = 0;
        int tmp2 = 0;
        for (int i = 0; i < iters; i++) {
            tmp1 += i * i;
            tmp2 = tmp1 + tmp1;
            result += tmp2 * tmp2;
        }
        return result;
    }

    public long timeMaddxInterleave(int iters) {
        long result = 0;
        long tmp1 = 0;
        long tmp2 = 0;
        for (int i = 0; i < iters; i++) {
            long a = i;
            tmp1 += a * a;
            tmp2 += tmp1 + tmp1;
            result += tmp2 * tmp2;
        }
        return result;
    }

    public long timeMixed(int iters) {
        long result = 0;
        long tmp = 0;
        long lt = 12;
        int a = 0;
        for (int i = 0; i < iters; i++) {
            a = (i - 2) + i * i;
            a = 1 + a * a;
            tmp = a;
            result -= tmp * tmp;
            tmp++;
            result += tmp * tmp + (tmp >> 2) * tmp + (tmp >> 2) * lt;
        }
        return result;
    }
}