Commit 3d3a933f authored by David Flynn's avatar David Flynn
Browse files

refactor: relocate and rename sqrtFixedpoint() to isqrt()

Since the implementation has no interaction with the FixedPoint class,
it makes little sense to name it so.
parent 90b16a38
......@@ -117,6 +117,7 @@ file(GLOB PROJECT_CPP_FILES
"geometry_trisoup_encoder.cpp"
"io_hls.cpp"
"io_tlv.cpp"
"misc.cpp"
"osspecific.cpp"
"partitioning.cpp"
"pcc_chrono.cpp"
......
......@@ -59,58 +59,6 @@ FixedPoint::operator/=(const FixedPoint& that)
}
}
static const uint32_t kSqrtLut[256] = {
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16};
uint32_t
sqrtFixedpoint(uint64_t x)
{
uint32_t a;
// Initial guess
if (x >= ((int64_t)0x1 << 32)) {
if (x >= ((int64_t)0x1 << 48)) {
if (x >= ((int64_t)0x1 << 56))
a = (kSqrtLut[x >> 56] << 28) - 1;
else
a = kSqrtLut[x >> 48] << 24;
} else {
if (x >= ((int64_t)0x1 << 40))
a = kSqrtLut[x >> 40] << 20;
else
a = kSqrtLut[x >> 32] << 16;
}
} else {
if (x >= ((int64_t)0x1 << 16)) {
if (x >= ((int64_t)0x1 << 24))
a = kSqrtLut[x >> 24] << 12;
else
a = kSqrtLut[x >> 16] << 8;
} else {
if (x >= ((int64_t)0x1 << 8))
a = kSqrtLut[x >> 8] << 4;
else
return kSqrtLut[x];
}
}
a = (a + x / a) >> 1;
return (a + x / a + 1) >> 1;
}
//============================================================================
} // namespace pcc
......@@ -121,8 +121,4 @@ FixedPoint::operator*=(const FixedPoint& that)
//============================================================================
uint32_t sqrtFixedpoint(uint64_t x);
//============================================================================
} // namespace pcc
......@@ -171,6 +171,11 @@ ceillog2(uint32_t x)
return ilog2(x - 1) + 1;
}
//---------------------------------------------------------------------------
// Compute an approximation of \left\floor \sqrt{x} \right\floor
uint32_t isqrt(uint64_t x);
//---------------------------------------------------------------------------
// Decrement the @axis-th dimension of 3D morton code @x.
//
......
......@@ -38,6 +38,8 @@
#include <cstddef>
#include <utility>
#include "PCCMisc.h"
namespace pcc {
//============================================================================
......@@ -57,7 +59,7 @@ rahtFixedPointRotation(
b.val = (weightRight << b.kFracBits) / (weightLeft + weightRight);
adjustedQuantStepSize.val = sqrtFixedpoint(
adjustedQuantStepSize.val = isqrt(
((quantStepSizeLuma.val * quantStepSizeLuma.val)
* (weightLeft + weightRight))
/ (weightLeft * weightRight));
......@@ -92,7 +94,7 @@ rahtFixedPointInverseRotation(
b.val = (weightRight << b.kFracBits) / (weightLeft + weightRight);
adjustedQuantStepSize.val = sqrtFixedpoint(
adjustedQuantStepSize.val = isqrt(
((quantStepSizeLuma.val * quantStepSizeLuma.val)
* (weightLeft + weightRight))
/ (weightLeft * weightRight));
......@@ -204,8 +206,8 @@ regionAdaptiveHierarchicalTransform(
delete[] attributesTransformed;
// Quantization of DC coefficients
quantStepSizeLuma.val = sqrtFixedpoint(
(quantStepSizeLuma.val * quantStepSizeLuma.val) / weight[0]);
quantStepSizeLuma.val =
isqrt((quantStepSizeLuma.val * quantStepSizeLuma.val) / weight[0]);
for (size_t k = 0; k < attribCount; k++)
attributes[k] /= quantStepSizeLuma;
......@@ -277,7 +279,7 @@ regionAdaptiveHierarchicalInverseTransform(
{
FixedPoint quantStepSizeLuma2;
quantStepSizeLuma2.val =
sqrtFixedpoint((quantStepSizeLuma.val * quantStepSizeLuma.val) / N);
isqrt((quantStepSizeLuma.val * quantStepSizeLuma.val) / N);
for (size_t k = 0; k < attribCount; k++)
attributesTransformed[k] *= quantStepSizeLuma2;
}
......
......@@ -115,4 +115,60 @@ expandNum(const std::string& src, int num)
//============================================================================
static const int8_t kSqrtLut[256] = {
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16};
//============================================================================
uint32_t
isqrt(uint64_t x)
{
uint32_t a;
// Initial guess
if (x >= ((int64_t)0x1 << 32)) {
if (x >= ((int64_t)0x1 << 48)) {
if (x >= ((int64_t)0x1 << 56))
a = (kSqrtLut[x >> 56] << 28) - 1;
else
a = kSqrtLut[x >> 48] << 24;
} else {
if (x >= ((int64_t)0x1 << 40))
a = kSqrtLut[x >> 40] << 20;
else
a = kSqrtLut[x >> 32] << 16;
}
} else {
if (x >= ((int64_t)0x1 << 16)) {
if (x >= ((int64_t)0x1 << 24))
a = kSqrtLut[x >> 24] << 12;
else
a = kSqrtLut[x >> 16] << 8;
} else {
if (x >= ((int64_t)0x1 << 8))
a = kSqrtLut[x >> 8] << 4;
else
return kSqrtLut[x];
}
}
a = (a + x / a) >> 1;
return (a + x / a + 1) >> 1;
}
//============================================================================
} // namespace pcc
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment