diff options
author | Ruslan Kovalev <ruslan.a.kovalev@gmail.com> | 2022-02-10 16:46:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:45 +0300 |
commit | 9123176b341b6f2658cff5132482b8237c1416c8 (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/cpp/packedtypes | |
parent | 59e19371de37995fcb36beb16cd6ec030af960bc (diff) | |
download | ydb-9123176b341b6f2658cff5132482b8237c1416c8.tar.gz |
Restoring authorship annotation for Ruslan Kovalev <ruslan.a.kovalev@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/packedtypes')
-rw-r--r-- | library/cpp/packedtypes/fixed_point.h | 34 | ||||
-rw-r--r-- | library/cpp/packedtypes/longs.h | 2 | ||||
-rw-r--r-- | library/cpp/packedtypes/longs_ut.cpp | 6 | ||||
-rw-r--r-- | library/cpp/packedtypes/packed.h | 2 | ||||
-rw-r--r-- | library/cpp/packedtypes/packedfloat.h | 84 | ||||
-rw-r--r-- | library/cpp/packedtypes/packedfloat_ut.cpp | 222 |
6 files changed, 175 insertions, 175 deletions
diff --git a/library/cpp/packedtypes/fixed_point.h b/library/cpp/packedtypes/fixed_point.h index bb7ca0a643..b36b80876e 100644 --- a/library/cpp/packedtypes/fixed_point.h +++ b/library/cpp/packedtypes/fixed_point.h @@ -1,71 +1,71 @@ -#pragma once - -namespace NFixedPoint { +#pragma once + +namespace NFixedPoint { template <ui64 FracMult> class TFixedPoint { typedef TFixedPoint<FracMult> TSelf; ui64 Rep; - + public: TFixedPoint() : Rep() { } - + template <typename T> explicit TFixedPoint(T t) : Rep(ui64(t * FracMult)) { } - + explicit TFixedPoint(ui64 i, ui64 f) : Rep(i * FracMult + f % FracMult) { } - + template <typename T> TSelf& operator=(T t) { Rep = t * FracMult; return *this; } - + operator double() const { return Int() + double(Frac()) / FracMult; } - + operator ui64() const { return Int(); } - + template <typename T> TSelf& operator/=(T t) { Rep = ui64(Rep / t); return *this; } - + template <typename T> TSelf operator/(T t) const { TSelf r = *this; return r /= t; } - + ui64 Frac() const { return Rep % FracMult; } - + ui64 Int() const { return Rep / FracMult; } - + static ui64 Mult() { return FracMult; } - + static TSelf Make(ui64 rep) { TFixedPoint<FracMult> fp; fp.Rep = rep; return fp; } }; - -} + +} diff --git a/library/cpp/packedtypes/longs.h b/library/cpp/packedtypes/longs.h index 221d2561d0..084098d705 100644 --- a/library/cpp/packedtypes/longs.h +++ b/library/cpp/packedtypes/longs.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include <util/system/defaults.h> // _BIDSCLASS _EXPCLASS #include <util/system/yassert.h> diff --git a/library/cpp/packedtypes/longs_ut.cpp b/library/cpp/packedtypes/longs_ut.cpp index 88e19a960d..8b06c934d2 100644 --- a/library/cpp/packedtypes/longs_ut.cpp +++ b/library/cpp/packedtypes/longs_ut.cpp @@ -55,9 +55,9 @@ Y_UNIT_TEST_SUITE(TLongsTest) { template <typename TSignedInt> void TestCornerCasesImpl(int maxPow) { for (int i = 0; i <= maxPow; ++i) { - TestOneValue<TSignedInt>((TSignedInt)(1ull << i)); - TestOneValue<TSignedInt>((TSignedInt)((1ull << i) - 1)); - TestOneValue<TSignedInt>((TSignedInt)((1ull << i) + 1)); + TestOneValue<TSignedInt>((TSignedInt)(1ull << i)); + TestOneValue<TSignedInt>((TSignedInt)((1ull << i) - 1)); + TestOneValue<TSignedInt>((TSignedInt)((1ull << i) + 1)); } } diff --git a/library/cpp/packedtypes/packed.h b/library/cpp/packedtypes/packed.h index 9abc1c2adb..88cff26ae2 100644 --- a/library/cpp/packedtypes/packed.h +++ b/library/cpp/packedtypes/packed.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include <library/cpp/streams/zc_memory_input/zc_memory_input.h> diff --git a/library/cpp/packedtypes/packedfloat.h b/library/cpp/packedtypes/packedfloat.h index 9cb17a8dc1..f178912ed3 100644 --- a/library/cpp/packedtypes/packedfloat.h +++ b/library/cpp/packedtypes/packedfloat.h @@ -1,65 +1,65 @@ -#pragma once - +#pragma once + #include <util/generic/cast.h> #include <util/generic/ylimits.h> #include <util/system/hi_lo.h> -#include <cmath> -#include <cfloat> +#include <cmath> +#include <cfloat> #include <limits> -#include <algorithm> +#include <algorithm> #include <cassert> - -namespace NPackedFloat { + +namespace NPackedFloat { /* - Exponent Mantissa zero Mantissa non-zero Equation - 0x00 zero denormal (-1)^sign * 2^-126 * 0.mantissa - 0x01–0xfe normalized value (-1)^sign * 2^(exponent - 127) * 1.mantissa - 0xff infinity NaN - * */ - + Exponent Mantissa zero Mantissa non-zero Equation + 0x00 zero denormal (-1)^sign * 2^-126 * 0.mantissa + 0x01–0xfe normalized value (-1)^sign * 2^(exponent - 127) * 1.mantissa + 0xff infinity NaN + * */ + //fast 16 bit floats by melkov template <ui8 SIGNED> struct float16 { private: typedef float16<SIGNED> self; - + public: ui16 val; - + explicit float16(ui16 v = 0) : val(v) { } - + self& operator=(float t) { assert(SIGNED == 1 || SIGNED == 0 && t >= 0.); val = BitCast<ui32>(t) >> (15 + SIGNED); return *this; } - + operator float() const { return BitCast<float>((ui32)val << (15 + SIGNED)); } - + static self New(float v) { self f; return f = v; } - + static self denorm_min() { return self(0x0001); } - + static self min() { return self(SIGNED ? 0x0080 : 0x0100); } - + static self max() { return self(SIGNED ? 0x7f7f : 0xfeff); } - }; - + }; + //fast 8 bit floats template <ui8 SIGNED, ui8 DENORM = 0> struct float8 { @@ -70,23 +70,23 @@ namespace NPackedFloat { FMaxExp = SIGNED ? 0x83 : 0x87, MaxExp = SIGNED ? 0x70 : 0xf0, }; - + public: ui8 val; - + explicit float8(ui8 v = 0) : val(v) { } - + self& operator=(float t) { assert(SIGNED == 1 || SIGNED == 0 && t >= 0.); ui16 hi16 = Hi16(t); - + ui8 sign = SIGNED ? Hi8(hi16) & 0x80 : 0; - + hi16 <<= 1; - + ui8 fexp = Hi8(hi16); ui8 exp; ui8 frac = (Lo8(hi16) & 0xf0) >> 4; @@ -103,15 +103,15 @@ namespace NPackedFloat { val = sign | exp | frac; return *this; - } - + } + operator float() const { ui32 v = 0; - + v |= SIGNED ? (val & 0x80) << 24 : 0; ui8 frac = val & 0x0f; ui8 exp = val & MaxExp; - + if (exp) { v |= ((exp >> 4) + FMinExp) << 23 | frac << 19; } else if (DENORM && val & 0x0f) { @@ -119,33 +119,33 @@ namespace NPackedFloat { frac <<= 1; ++exp; } - + v |= (FMinExp - exp + 1) << 23 | (frac & 0x0f) << 19; } else v |= 0; - + return BitCast<float>(v); } - + static self New(float v) { self f; return f = v; } - + static self denorm_min() { return self(0x01); } - + static self min() { return self(0x10); } - + static self max() { return self(SIGNED ? 0x7f : 0xff); } }; -} - +} + using f64 = double; using f32 = float; static_assert(sizeof(f32) == 4, "expect sizeof(f32) == 4"); @@ -156,7 +156,7 @@ using f8 = NPackedFloat::float8<1>; using uf8 = NPackedFloat::float8<0>; using f8d = NPackedFloat::float8<1, 1>; using uf8d = NPackedFloat::float8<0, 1>; - + // [0,1) value in 1/255s. using frac8 = ui8; diff --git a/library/cpp/packedtypes/packedfloat_ut.cpp b/library/cpp/packedtypes/packedfloat_ut.cpp index fb33883c1c..f61e49014c 100644 --- a/library/cpp/packedtypes/packedfloat_ut.cpp +++ b/library/cpp/packedtypes/packedfloat_ut.cpp @@ -1,10 +1,10 @@ #include "packedfloat.h" #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/ylimits.h> -class TPackedFloatTest: public TTestBase { +class TPackedFloatTest: public TTestBase { UNIT_TEST_SUITE(TPackedFloatTest); UNIT_TEST(F16Test); UNIT_TEST(Uf16Test); @@ -14,117 +14,117 @@ class TPackedFloatTest: public TTestBase { UNIT_TEST(Uf8Test); UNIT_TEST_SUITE_END(); -private: +private: template <typename F> - void TestF(float f, float rf) { - UNIT_ASSERT(F::New(f) == rf); - } - - void TestF16(float f, float rf) { - TestF<f16>(f, rf); - } - - void F16Test() { - TestF16(f16(), 0.); - TestF16(0, 0); - TestF16(1.5, 1.5); - TestF16(-1.5, -1.5); - TestF16(f16::max(), f16::max()); - TestF16(f16::min(), f16::min()); - TestF16(2.0f * f16::max(), std::numeric_limits<float>::infinity()); - TestF16(0.5 * f16::min(), 0.5 * f16::min()); - TestF16(0.5 * f16::denorm_min(), 0); + void TestF(float f, float rf) { + UNIT_ASSERT(F::New(f) == rf); + } + + void TestF16(float f, float rf) { + TestF<f16>(f, rf); + } + + void F16Test() { + TestF16(f16(), 0.); + TestF16(0, 0); + TestF16(1.5, 1.5); + TestF16(-1.5, -1.5); + TestF16(f16::max(), f16::max()); + TestF16(f16::min(), f16::min()); + TestF16(2.0f * f16::max(), std::numeric_limits<float>::infinity()); + TestF16(0.5 * f16::min(), 0.5 * f16::min()); + TestF16(0.5 * f16::denorm_min(), 0); TestF16(-0.5 * f16::denorm_min(), 0); - TestF16(FLT_MIN, FLT_MIN); - TestF16(FLT_MAX, f16::max()); - TestF16(f16::min(), FLT_MIN); - TestF16(f16::denorm_min(), (FLT_MIN / (1 << 23)) * (1 << 16)); - } - - void TestUf16(float f, float rf) { - TestF<uf16>(f, rf); - } - - void Uf16Test() { - UNIT_ASSERT(uf16() == 0.); - - TestUf16(0, 0); - TestUf16(1.5, 1.5); - TestUf16(uf16::max(), uf16::max()); - TestUf16(uf16::min(), uf16::min()); - TestUf16(2.0f * uf16::max(), std::numeric_limits<float>::infinity()); - TestUf16(0.5 * uf16::min(), 0.5 * uf16::min()); - TestUf16(0.5 * uf16::denorm_min(), 0); - TestUf16(FLT_MIN, FLT_MIN); - TestUf16(FLT_MAX, uf16::max()); - TestUf16(uf16::min(), FLT_MIN); - TestUf16(uf16::denorm_min(), (FLT_MIN / (1 << 23)) * (1 << 15)); - } - - void TestF8d(float f, float rf) { - TestF<f8d>(f, rf); - } - - void F8dTest() { - UNIT_ASSERT(f8d() == 0.); - - TestF8d(0, 0); - TestF8d(1.5, 1.5); - TestF8d(-1.5, -1.5); - TestF8d(f8d::max(), f8d::max()); - TestF8d(f8d::min(), f8d::min()); - TestF8d(f8d::denorm_min(), f8d::denorm_min()); - TestF8d(2.0 * f8d::max(), f8d::max()); - TestF8d(0.5 * f8d::min(), 0.5 * f8d::min()); - TestF8d(0.5 * f8d::denorm_min(), 0); - } - - void TestF8(float f, float rf) { - TestF<f8>(f, rf); - } - - void F8Test() { - UNIT_ASSERT(f8() == 0.); - - TestF8(0, 0); - TestF8(1.5, 1.5); - TestF8(-1.5, -1.5); - TestF8(f8::max(), f8::max()); - TestF8(f8::min(), f8::min()); - TestF8(2.0 * f8::max(), f8::max()); - TestF8(0.5 * f8::min(), 0); - } - - void TestUf8d(float f, float rf) { - TestF<uf8d>(f, rf); - } - - void Uf8dTest() { - UNIT_ASSERT(uf8d() == 0.); - - TestUf8d(0, 0); - TestUf8d(1.5, 1.5); - TestUf8d(uf8d::max(), uf8d::max()); - TestUf8d(uf8d::min(), uf8d::min()); - TestUf8d(uf8d::denorm_min(), uf8d::denorm_min()); - TestUf8d(2.0 * uf8d::max(), uf8d::max()); - TestUf8d(0.5 * uf8d::min(), 0.5 * uf8d::min()); - TestUf8d(0.5 * uf8d::denorm_min(), 0); - } - - void TestUf8(float f, float rf) { - TestF<uf8>(f, rf); - } - - void Uf8Test() { - UNIT_ASSERT(uf8() == 0.); - - TestUf8(0, 0); - TestUf8(1.5, 1.5); - TestUf8(uf8::max(), uf8::max()); - TestUf8(uf8::min(), uf8::min()); - TestUf8(2.0 * uf8::max(), uf8::max()); - TestUf8(0.5 * uf8::min(), 0); + TestF16(FLT_MIN, FLT_MIN); + TestF16(FLT_MAX, f16::max()); + TestF16(f16::min(), FLT_MIN); + TestF16(f16::denorm_min(), (FLT_MIN / (1 << 23)) * (1 << 16)); + } + + void TestUf16(float f, float rf) { + TestF<uf16>(f, rf); + } + + void Uf16Test() { + UNIT_ASSERT(uf16() == 0.); + + TestUf16(0, 0); + TestUf16(1.5, 1.5); + TestUf16(uf16::max(), uf16::max()); + TestUf16(uf16::min(), uf16::min()); + TestUf16(2.0f * uf16::max(), std::numeric_limits<float>::infinity()); + TestUf16(0.5 * uf16::min(), 0.5 * uf16::min()); + TestUf16(0.5 * uf16::denorm_min(), 0); + TestUf16(FLT_MIN, FLT_MIN); + TestUf16(FLT_MAX, uf16::max()); + TestUf16(uf16::min(), FLT_MIN); + TestUf16(uf16::denorm_min(), (FLT_MIN / (1 << 23)) * (1 << 15)); + } + + void TestF8d(float f, float rf) { + TestF<f8d>(f, rf); + } + + void F8dTest() { + UNIT_ASSERT(f8d() == 0.); + + TestF8d(0, 0); + TestF8d(1.5, 1.5); + TestF8d(-1.5, -1.5); + TestF8d(f8d::max(), f8d::max()); + TestF8d(f8d::min(), f8d::min()); + TestF8d(f8d::denorm_min(), f8d::denorm_min()); + TestF8d(2.0 * f8d::max(), f8d::max()); + TestF8d(0.5 * f8d::min(), 0.5 * f8d::min()); + TestF8d(0.5 * f8d::denorm_min(), 0); + } + + void TestF8(float f, float rf) { + TestF<f8>(f, rf); + } + + void F8Test() { + UNIT_ASSERT(f8() == 0.); + + TestF8(0, 0); + TestF8(1.5, 1.5); + TestF8(-1.5, -1.5); + TestF8(f8::max(), f8::max()); + TestF8(f8::min(), f8::min()); + TestF8(2.0 * f8::max(), f8::max()); + TestF8(0.5 * f8::min(), 0); + } + + void TestUf8d(float f, float rf) { + TestF<uf8d>(f, rf); + } + + void Uf8dTest() { + UNIT_ASSERT(uf8d() == 0.); + + TestUf8d(0, 0); + TestUf8d(1.5, 1.5); + TestUf8d(uf8d::max(), uf8d::max()); + TestUf8d(uf8d::min(), uf8d::min()); + TestUf8d(uf8d::denorm_min(), uf8d::denorm_min()); + TestUf8d(2.0 * uf8d::max(), uf8d::max()); + TestUf8d(0.5 * uf8d::min(), 0.5 * uf8d::min()); + TestUf8d(0.5 * uf8d::denorm_min(), 0); + } + + void TestUf8(float f, float rf) { + TestF<uf8>(f, rf); + } + + void Uf8Test() { + UNIT_ASSERT(uf8() == 0.); + + TestUf8(0, 0); + TestUf8(1.5, 1.5); + TestUf8(uf8::max(), uf8::max()); + TestUf8(uf8::min(), uf8::min()); + TestUf8(2.0 * uf8::max(), uf8::max()); + TestUf8(0.5 * uf8::min(), 0); } }; UNIT_TEST_SUITE_REGISTRATION(TPackedFloatTest); |