summaryrefslogtreecommitdiff
path: root/src/main/java/org/linaro/benchmarks/math/MathCordic.java
blob: 313e52c4898f04e4589bde1cad460f99bb1c0190 (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
/*
 * Ported to java from:
 *  https://github.com/WebKit/webkit/blob/master/PerformanceTests/SunSpider/tests/sunspider-1.0.2/math-cordic.js
 *
 * Copyright (C) Rich Moore.  All rights reserved.
 * Copyright (C) 2015 Linaro Limited. For the port to Java
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Description:     A simple and efficient algorithm used to calculate
 *                  trigonometric cos and sin functions using additions,
 *                  subtractions and bitshifts.
 * Main Focus:      Bitshifts, Floating-Point operations.
 *
 */

package org.linaro.benchmarks.math;

import java.lang.System;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)

public class MathCordic {
  private static double[] ANGLES;
  private static final double AG_CONST = 0.6072529350;
  private static final double CORDIC_EXPECTED = 10362.570468755888;
  private static final double RAD_CONST = 0.017453;
  private static final double TARGET_CONST = 28.027;
  private static final double X_CONST = 65536.0;

  private static double fixedMul(double x) {
    return x * X_CONST;
  }

  private static double fixedDiv(double x) {
    return x / X_CONST;
  }

  private static double deg2rad(double x) {
    return x * RAD_CONST;
  }

  static {
    ANGLES = new double[] {
        fixedMul(45.0), fixedMul(26.565), fixedMul(14.0362), fixedMul(7.12502),
        fixedMul(3.57633), fixedMul(1.78991), fixedMul(0.895174), fixedMul(0.447614),
        fixedMul(0.223811), fixedMul(0.111906), fixedMul(0.055953), fixedMul(0.027977) };
  }

  private double cordicSinCos(double target) {
    double x;
    double y;
    double targetAngle;
    double currentAngle;

    x = fixedMul(AG_CONST);     /* AG_CONST * cos(0) */
    y = 0.0;                    /* AG_CONST * sin(0) */
    currentAngle = 0.0;
    targetAngle = fixedMul(target);

    for (int i = 0; i < ANGLES.length; i++) {
      double newX;
      if (targetAngle > currentAngle) {
        newX = x - ((long)y >> i);
        y = ((long)x >> i) + y;
        x = newX;
        currentAngle += ANGLES[i];
      } else {
        newX = x + ((long)y >> i);
        y = -((long)x >> i) + y;
        x = newX;
        currentAngle -= ANGLES[i];
      }
    }

    return fixedDiv(x) * fixedDiv(y);
  }

  @Benchmark
  public double jmhTimeMathCordic() {
      // When profiling, hot region is in jmhTimeMathCordic_avgt_jmhStub not jmhTimeMathCordic.
      // This may be caused by inlining jmhTimeMathCordic in jmhTimeMathCordic_avgt_jmhStub.
      // The following "loops" is added to work around this problem.
      int loops = 400;
      double s = 0;
      for (int i = 0; i < loops; i++) {
          s += cordicSinCos(TARGET_CONST);
      }
      return s;
  }
}