Repository navigation
Expand file tree
/
Copy pathfixed_study.cc
More file actions
167 lines (159 loc) · 6.01 KB
/
Copy pathfixed_study.cc
File metadata and controls
167 lines (159 loc) · 6.01 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "ipl/fixed_study.h"
#include <limits>
#include "ipl/fixed.h"
namespace ipl {
namespace {
using Word = std::int64_t;
using Range = std::pair<Word, Word>;
Range SignedRange(int width) {
if (width < 2 || width > 64)
throw std::invalid_argument("Invalid signed width");
if (width == 64) {
return {std::numeric_limits<Word>::min(), std::numeric_limits<Word>::max()};
}
const Word high = Word{1} << (width - 1);
return {-high, high - 1};
}
bool Contains(Range outer, Range inner) {
return outer.first <= inner.first && inner.second <= outer.second;
}
} // namespace
FixedStudy::FixedStudy(const Image& input, const std::vector<double>& kernel,
int rows, int cols, StudyFormat format)
: format_(format),
input_rows_(input.rows),
input_cols_(input.cols),
input_channels_(input.channels),
rows_(rows),
cols_(cols) {
ValidateKernel(rows, cols, kernel.size());
if (format.input_bits < 8 || format.input_bits > 12) {
throw std::invalid_argument("Input width must be in [8, 12]");
}
SignedRange(format.product_bits);
SignedRange(format.accumulator_bits);
peak_ = (1 << format.input_bits) - 1;
for (double tap : kernel) {
const Word code = Quantize(tap, format.coefficient_bits, format.fraction);
weights_.push_back(code);
const Word extreme = peak_ * code;
product_range_.first = std::min(product_range_.first, extreme);
product_range_.second = std::max(product_range_.second, extreme);
prefix_range_.first += std::min(Word{0}, extreme);
prefix_range_.second += std::max(Word{0}, extreme);
}
pixels_.reserve(input.pixels.size());
for (double sample : input.pixels) {
if (!std::isfinite(sample)) throw std::invalid_argument("Nonfinite pixel");
pixels_.push_back(static_cast<int>(std::round(Unit(sample) * peak_)));
}
}
template <bool Interior, bool Count, bool Proven>
StudyStats FixedStudy::Process(const Image& in, Image& out,
Border border) const {
const auto product_limits = SignedRange(format_.product_bits);
const auto accumulator_limits = SignedRange(format_.accumulator_bits);
StudyStats stats;
const int ry = rows_ / 2;
const int rx = cols_ / 2;
const Word divisor = Word{1} << format_.fraction;
auto pixel = [&](int y, int x, int c, auto interior) {
constexpr bool mapped = !decltype(interior)::value;
Word sum = 0;
bool affected = false;
for (int ky = 0; ky < rows_; ++ky) {
int sy = y + ky - ry;
if constexpr (mapped) {
sy = BorderIndex(sy, in.rows, border);
if (sy < 0) continue;
}
const int* row = pixels_.data() + in.Index(sy, 0, c);
const Word* weights = weights_.data() + ky * cols_;
for (int kx = 0; kx < cols_; ++kx) {
int sx = x + kx - rx;
if constexpr (mapped) {
sx = BorderIndex(sx, in.cols, border);
if (sx < 0) continue;
}
const Word raw = row[sx * in.channels] * weights[kx];
Word product = raw;
if constexpr (!Proven)
product =
std::clamp(raw, product_limits.first, product_limits.second);
const Word candidate = sum + product;
sum = candidate;
if constexpr (!Proven) {
if (!format_.end_only)
sum = std::clamp(sum, accumulator_limits.first,
accumulator_limits.second);
}
if constexpr (Count) {
++stats.taps;
stats.products += raw != product;
stats.accumulators += sum != candidate;
affected |= raw != product || sum != candidate;
stats.product_min = std::min(stats.product_min, raw);
stats.product_max = std::max(stats.product_max, raw);
stats.prefix_min = std::min(stats.prefix_min, candidate);
stats.prefix_max = std::max(stats.prefix_max, candidate);
}
}
}
if constexpr (!Proven) {
if (format_.end_only) {
const auto clipped = std::clamp(sum, accumulator_limits.first,
accumulator_limits.second);
if constexpr (Count) {
stats.accumulators += clipped != sum;
affected |= clipped != sum;
}
sum = clipped;
}
}
const Word scaled =
format_.truncate ? sum / divisor : RoundScale(sum, format_.fraction);
const Word code = std::clamp(scaled, Word{0}, Word{peak_});
if constexpr (Count) {
stats.outputs += scaled != code;
stats.affected += affected || scaled != code;
}
out.At(y, x, c) = static_cast<double>(code) / peak_;
};
for (int y = 0; y < in.rows; ++y) {
for (int x = 0; x < in.cols; ++x) {
for (int c = 0; c < in.channels; ++c) {
if constexpr (Interior) {
if (y >= ry && y < in.rows - ry && x >= rx && x < in.cols - rx) {
pixel(y, x, c, std::true_type{});
continue;
}
}
pixel(y, x, c, std::false_type{});
}
}
}
return stats;
}
StudyStats FixedStudy::Run(const Image& in, Image& out, Border border,
StudyVariant variant) const {
if (in.rows != input_rows_ || in.cols != input_cols_ ||
in.channels != input_channels_ || out.rows != input_rows_ ||
out.cols != input_cols_ || out.channels != input_channels_ ||
out.pixels.size() != pixels_.size()) {
throw std::invalid_argument("Incompatible fixed-study image shape");
}
if (variant == StudyVariant::kGolden)
return Process<false, true, false>(in, out, border);
if (variant == StudyVariant::kInterior)
return Process<true, true, false>(in, out, border);
const bool safe =
Contains(SignedRange(format_.product_bits), product_range_) &&
Contains(SignedRange(format_.accumulator_bits), prefix_range_);
if (variant == StudyVariant::kProof) {
return safe ? Process<true, true, true>(in, out, border)
: Process<true, true, false>(in, out, border);
}
return safe ? Process<true, false, true>(in, out, border)
: Process<true, false, false>(in, out, border);
}
} // namespace ipl