Skip to content

synchronization cache of roots #72

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ch.randelshofer.fastdoubleparser;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;

import static ch.randelshofer.fastdoubleparser.FastDoubleMath.fastScalb;
import static ch.randelshofer.fastdoubleparser.FastDoubleSwar.fma;
Expand Down Expand Up @@ -48,7 +49,7 @@ class FftMultiplier {
/**
* for FFTs of length up to 2^19
*/
private static final int ROOTS_CACHE2_SIZE = 20;
private static final int ROOTS2_CACHE_SIZE = 20;
/**
* The threshold value for using 3-way Toom-Cook multiplication.
*/
Expand All @@ -58,13 +59,13 @@ class FftMultiplier {
* elements representing all (2^(k+2))-th roots between 0 and pi/2.
* Used for FFT multiplication.
*/
private volatile static ComplexVector[] ROOTS2_CACHE = new ComplexVector[ROOTS_CACHE2_SIZE];
private final static AtomicReferenceArray<ComplexVector> ROOTS2_CACHE = new AtomicReferenceArray<>(ROOTS2_CACHE_SIZE);
/**
* Sets of complex roots of unity. The set at index k contains 3*2^k
* elements representing all (3*2^(k+2))-th roots between 0 and pi/2.
* Used for FFT multiplication.
*/
private volatile static ComplexVector[] ROOTS3_CACHE = new ComplexVector[ROOTS3_CACHE_SIZE];
private final static AtomicReferenceArray<ComplexVector> ROOTS3_CACHE = new AtomicReferenceArray<>(ROOTS3_CACHE_SIZE);

/**
* Returns the maximum number of bits that one double precision number can fit without
Expand Down Expand Up @@ -351,11 +352,11 @@ static BigInteger fromFftVector(ComplexVector fftVec, int signum, int bitsPerFft
private static ComplexVector[] getRootsOfUnity2(int logN) {
ComplexVector[] roots = new ComplexVector[logN + 1];
for (int i = logN; i >= 0; i -= 2) {
if (i < ROOTS_CACHE2_SIZE) {
if (ROOTS2_CACHE[i] == null) {
ROOTS2_CACHE[i] = calculateRootsOfUnity(1 << i);
if (i < ROOTS2_CACHE_SIZE) {
if (ROOTS2_CACHE.get(i) == null) {
ROOTS2_CACHE.set(i, calculateRootsOfUnity(1 << i));
}
roots[i] = ROOTS2_CACHE[i];
roots[i] = ROOTS2_CACHE.get(i);
} else {
roots[i] = calculateRootsOfUnity(1 << i);
}
Expand All @@ -371,10 +372,10 @@ private static ComplexVector[] getRootsOfUnity2(int logN) {
*/
private static ComplexVector getRootsOfUnity3(int logN) {
if (logN < ROOTS3_CACHE_SIZE) {
if (ROOTS3_CACHE[logN] == null) {
ROOTS3_CACHE[logN] = calculateRootsOfUnity(3 << logN);
if (ROOTS3_CACHE.get(logN) == null) {
ROOTS3_CACHE.set(logN, calculateRootsOfUnity(3 << logN));
}
return ROOTS3_CACHE[logN];
return ROOTS3_CACHE.get(logN);
} else {
return calculateRootsOfUnity(3 << logN);
}
Expand Down