Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize fastAtan2 #333

Merged
merged 1 commit into from
May 14, 2023
Merged

Optimize fastAtan2 #333

merged 1 commit into from
May 14, 2023

Conversation

0x000006
Copy link
Contributor

Basically replace x * y + z with fma(x, y, z).
Should be about 35-45% faster on an Intel processor.
Proof of concept:
JMH benchmark code:

package org.example;

import org.openjdk.jmh.annotations.*;

import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 4, time = 2500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
public class Main {
    public static void main(String[] args) throws IOException {
        org.openjdk.jmh.Main.main(args);
    }

    @State(Scope.Benchmark)
    public static class BenchState {
        public double value;
        @Setup(Level.Trial)
        public void setUp() {
            value = ThreadLocalRandom.current().nextDouble();
        }
    }

    @Benchmark
    public double builtinAtan2(BenchState state) {
        return java.lang.Math.atan2(state.value, state.value);
    }

    @Benchmark
    public double newAtan2(BenchState state) {
        return org.example.Math.fastAtan2new(state.value, state.value);
    }

    @Benchmark
    public double oldAtan2(BenchState state) {
        return org.example.Math.fastAtan2old(state.value, state.value);
    }
}

Results:

Benchmark          Mode  Cnt   Score   Error  Units
Main.builtinAtan2  avgt    4  75,509 ± 3,748  ns/op
Main.newAtan2      avgt    4   1,768 ± 0,179  ns/op
Main.oldAtan2      avgt    4   2,415 ± 0,038  ns/op

Additional information:

# JMH version: 1.36
# VM version: JDK 17.0.7, Java HotSpot(TM) 64-Bit Server VM, 17.0.7+8-LTS-224

@httpdigest httpdigest added this to the 1.10.6 release milestone May 14, 2023
@httpdigest httpdigest merged commit 9672295 into JOML-CI:main May 14, 2023
@httpdigest
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants