1 /*
  2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.bench.valhalla.mandelbrot;
 24 
 25 import org.openjdk.jmh.annotations.Benchmark;
 26 import org.openjdk.jmh.annotations.Fork;
 27 
 28 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
 29 public class Value extends MandelbrotBase {
 30 
 31     private ValueComplex coordToComplex(int x, int y, int width, int height) {
 32         double cx = ((double) x) / (((double) width) / 2.0) - 1.0;
 33         double cy = ((double) y) / (((double) height) / 2.0) - 1.0;
 34         return new ValueComplex(cy * SCALE, cx * SCALE);
 35     }
 36 
 37     private static int count(ValueComplex c) {
 38         ValueComplex z = c;
 39         for (int i = 1; i < MAX_ITER; i++) {
 40             if (z.length() >= 2.0) return i;
 41             z = z.mul(z).add(c);
 42         }
 43         return MAX_ITER;
 44     }
 45 
 46     @Benchmark
 47     public int[][] mandelbrot_value() {
 48         for (int x = 0; x < size; x++) {
 49             for (int y = 0; y < size; y++) {
 50                 points[x][y] = count(coordToComplex(x, y, size, size));
 51             }
 52         }
 53         return points;
 54     }
 55 
 56     private Complex coordToComplex_interface(int x, int y, int width, int height) {
 57         double cx = ((double) x) / (((double) width) / 2.0) - 1.0;
 58         double cy = ((double) y) / (((double) height) / 2.0) - 1.0;
 59         return new ValueComplex(cy * SCALE, cx * SCALE);
 60     }
 61 
 62     private static int count_interface(Complex c) {
 63         Complex z = c;
 64         for (int i = 1; i < MAX_ITER; i++) {
 65             if (z.length() >= 2.0) return i;
 66             z = z.mul(z).add(c);
 67         }
 68         return MAX_ITER;
 69     }
 70 
 71     @Benchmark
 72     public int[][] mandelbrot_interface() {
 73         for (int x = 0; x < size; x++) {
 74             for (int y = 0; y < size; y++) {
 75                 points[x][y] = count_interface(coordToComplex_interface(x, y, size, size));
 76             }
 77         }
 78         return points;
 79     }
 80 
 81     public static interface Complex {
 82         public Complex add(Complex that);
 83         public Complex mul(Complex that);
 84         public double length();
 85         public double re();
 86         public double im();
 87     }
 88 
 89     public static value class ValueComplex implements Complex {
 90 
 91         public final double re;
 92         public final double im;
 93 
 94         public ValueComplex(double re, double im) {
 95             this.re =  re;
 96             this.im =  im;
 97         }
 98 
 99         @Override
100         public double re() { return re; }
101 
102         @Override
103         public double im() { return im; }
104 
105         public ValueComplex add(ValueComplex that) {
106             return new ValueComplex(this.re + that.re, this.im + that.im);
107         }
108 
109         @Override
110         public Complex add(Complex that) {
111             return new ValueComplex(this.re + that.re(), this.im + that.im());
112         }
113 
114         public ValueComplex mul(ValueComplex that) {
115             return new ValueComplex(this.re * that.re - this.im * that.im,
116                     this.re * that.im + this.im * that.re);
117         }
118 
119         @Override
120         public Complex mul(Complex that) {
121             return new ValueComplex(this.re * that.re() - this.im * that.im(),
122                     this.re * that.im() + this.im * that.re());
123         }
124 
125         public double length() {
126             return Math.sqrt(re * re + im * im);
127         }
128 
129     }
130 }