./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
41 / 41
100.0%
Branches:
24 / 24
100.0%
Functions:
3 / 3
100.0%

src/util/fft.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "util/fft.h"
4
5 #include "terraingen/util/bits.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <cstddef>
10 #include <stdexcept>
11 #include <utility>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// Reorders `values` by bit-reversed index, the input order of the iterative transform.
18 24742 void BitReverse(std::vector<std::complex<double>>& values) {
19 24742 const std::size_t n = values.size();
20
2/2
✓ Branch 0 taken 13845435 times.
✓ Branch 1 taken 24742 times.
13870177 for (std::size_t i = 1, j = 0; i < n; ++i) {
21 13845435 std::size_t bit = n >> 1;
22
2/2
✓ Branch 0 taken 13628113 times.
✓ Branch 1 taken 13845435 times.
27473548 for (; (j & bit) != 0; bit >>= 1) {
23 13628113 j ^= bit;
24 }
25 13845435 j ^= bit;
26
2/2
✓ Branch 0 taken 6623660 times.
✓ Branch 1 taken 7221775 times.
13845435 if (i < j) {
27 6623660 std::swap(values[i], values[j]);
28 }
29 }
30 24742 }
31
32 } // namespace
33
34 24742 void Fft(std::vector<std::complex<double>>& values, double sign) {
35 24742 BitReverse(values);
36 24742 const std::size_t n = values.size();
37
2/2
✓ Branch 0 taken 217322 times.
✓ Branch 1 taken 24742 times.
242064 for (std::size_t length = 2; length <= n; length <<= 1) {
38 217322 const double angle = sign * 2.0 * kPi / static_cast<double>(length);
39 217322 const std::complex<double> step(std::cos(angle), std::sin(angle));
40
2/2
✓ Branch 0 taken 13845435 times.
✓ Branch 1 taken 217322 times.
14062757 for (std::size_t start = 0; start < n; start += length) {
41 std::complex<double> w(1.0, 0.0);
42
2/2
✓ Branch 0 taken 65330832 times.
✓ Branch 1 taken 13845435 times.
79176267 for (std::size_t k = 0; k < length / 2; ++k) {
43 65330832 const std::complex<double> even = values[start + k];
44 65330832 const std::complex<double> odd = values[start + k + length / 2] * w;
45 65330832 values[start + k] = even + odd;
46 65330832 values[start + k + length / 2] = even - odd;
47 65330832 w *= step;
48 }
49 }
50 }
51 24742 }
52
53 33 void Fft2(std::vector<std::complex<double>>& values, std::size_t width, std::size_t height,
54 double sign) {
55
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 32 times.
33 if (values.size() != width * height) {
56
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A 2D FFT needs width x height values");
57 }
58 32 std::vector<std::complex<double>> line(width);
59
2/2
✓ Branch 0 taken 10532 times.
✓ Branch 1 taken 32 times.
10564 for (std::size_t row = 0; row < height; ++row) {
60 10532 std::copy_n(values.begin() + static_cast<std::ptrdiff_t>(row * width), width, line.begin());
61 10532 Fft(line, sign);
62 10532 std::copy(line.begin(), line.end(),
63 10532 values.begin() + static_cast<std::ptrdiff_t>(row * width));
64 }
65
1/1
✓ Branch 1 taken 32 times.
32 line.resize(height);
66
2/2
✓ Branch 0 taken 10568 times.
✓ Branch 1 taken 32 times.
10600 for (std::size_t col = 0; col < width; ++col) {
67
2/2
✓ Branch 0 taken 6448160 times.
✓ Branch 1 taken 10568 times.
6458728 for (std::size_t row = 0; row < height; ++row) {
68 6448160 line[row] = values[row * width + col];
69 }
70 10568 Fft(line, sign);
71
2/2
✓ Branch 1 taken 6448160 times.
✓ Branch 2 taken 10568 times.
6458728 for (std::size_t row = 0; row < height; ++row) {
72 6448160 values[row * width + col] = line[row];
73 }
74 }
75 32 }
76
77 } // namespace terraingen
78