From 0df291f1e9d21341fbdda2ab87c307a89bf088a1 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 17 Nov 2026 08:49:52 +0300 Subject: [PATCH] fix: encode fractional, non-finite and negative zero floats --- .../Internal/Visitor/IsValueEmpty.php | 23 +++ src/Reflection/Internal/numeric.php | 6 +- tests/FieldPresenceTest.php | 159 ++++++++++++++++++ 3 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 tests/FieldPresenceTest.php diff --git a/src/Reflection/Internal/Visitor/IsValueEmpty.php b/src/Reflection/Internal/Visitor/IsValueEmpty.php index 5a464d8..b9b6d7d 100644 --- a/src/Reflection/Internal/Visitor/IsValueEmpty.php +++ b/src/Reflection/Internal/Visitor/IsValueEmpty.php @@ -6,7 +6,9 @@ use Thesis\Protobuf\Reflection\BoolT; use Thesis\Protobuf\Reflection\BytesT; +use Thesis\Protobuf\Reflection\DoubleT; use Thesis\Protobuf\Reflection\EnumT; +use Thesis\Protobuf\Reflection\FloatT; use Thesis\Protobuf\Reflection\ListT; use Thesis\Protobuf\Reflection\MapT; use Thesis\Protobuf\Reflection\ObjectT; @@ -30,6 +32,18 @@ public function bool(BoolT $type): bool return $this->value === false; } + #[\Override] + public function float(FloatT $type): bool + { + return self::isPositiveZero($this->value); + } + + #[\Override] + public function double(DoubleT $type): bool + { + return self::isPositiveZero($this->value); + } + #[\Override] public function string(StringT $type): bool { @@ -75,4 +89,13 @@ protected function default(Type $type): bool { return zeroed($this->value); } + + /** + * Only +0.0 is the default value: -0.0 differs from it in the sign bit and must be encoded. + * An int 0 (a property typed int|float) is the default value too. + */ + private static function isPositiveZero(mixed $value): bool + { + return $value === 0 || ($value === 0.0 && fdiv(1, $value) === INF); + } } diff --git a/src/Reflection/Internal/numeric.php b/src/Reflection/Internal/numeric.php index 1ee6e8f..79d841c 100644 --- a/src/Reflection/Internal/numeric.php +++ b/src/Reflection/Internal/numeric.php @@ -11,12 +11,12 @@ */ function zeroed(mixed $value): bool { - if (is_numeric($value)) { - return (int) $value === 0; + if (\is_int($value)) { + return $value === 0; } if ($value instanceof Number) { - return $value->value === '0'; + return $value->compare(0) === 0; } return false; diff --git a/tests/FieldPresenceTest.php b/tests/FieldPresenceTest.php new file mode 100644 index 0000000..8c5c3c0 --- /dev/null +++ b/tests/FieldPresenceTest.php @@ -0,0 +1,159 @@ +encode(new FieldPresenceTestFloats(d: $value, f: $value)); + self::assertSame("09{$double}15{$float}", bin2hex($bytes)); + + $decoded = $decoder->decode($bytes, FieldPresenceTestFloats::class); + + if (is_nan($value)) { + self::assertNan($decoded->d); + self::assertNan($decoded->f); + } else { + // A float field holds the value rounded to 32 bits. + $float = unpack('g', pack('g', $value))[1] ?? null; + + self::assertSame($value, $decoded->d); + self::assertSame($float, $decoded->f); + self::assertSame(fdiv(1, $value), fdiv(1, $decoded->d), 'the sign of zero survives in a double'); + self::assertSame(fdiv(1, $float), fdiv(1, $decoded->f), 'the sign of zero survives in a float'); + } + } + + /** + * @return iterable + */ + public static function provideNonZeroFloatsAreEncodedCases(): iterable + { + yield '0.5' => [0.5, '000000000000e03f', '0000003f']; + yield '-0.25' => [-0.25, '000000000000d0bf', '000080be']; + yield '1e-10' => [1e-10, 'bbbdd7d9df7cdb3d', 'ffe6db2e']; + yield '-0.0' => [-0.0, '0000000000000080', '00000080']; + yield 'NAN' => [NAN, '000000000000f87f', '0000c07f']; + yield 'INF' => [INF, '000000000000f07f', '0000807f']; + yield '-INF' => [-INF, '000000000000f0ff', '000080ff']; + } + + public function testZeroValuesAreNotEncoded(): void + { + $encoder = Encoder\Builder::buildDefault(); + + self::assertSame('', $encoder->encode(new FieldPresenceTestFloats(d: 0.0, f: 0.0))); + self::assertSame('', $encoder->encode(new FieldPresenceTestIntOrFloat(d: 0, f: 0))); + self::assertSame('', $encoder->encode(new FieldPresenceTestIntegers())); + self::assertSame('', $encoder->encode(new FieldPresenceTestNumbers())); + self::assertSame('', $encoder->encode(new FieldPresenceTestNumbers(new Number('0.0'), new Number('0.000')))); + } + + public function testNonZeroNumbersAreEncoded(): void + { + $encoder = Encoder\Builder::buildDefault(); + + self::assertSame('0801', bin2hex($encoder->encode(new FieldPresenceTestNumbers(i: 1)))); + self::assertSame('08ffffffffffffffffff01', bin2hex($encoder->encode(new FieldPresenceTestNumbers(i: -1)))); + self::assertSame('1005', bin2hex($encoder->encode(new FieldPresenceTestNumbers(u: new Number(5))))); + self::assertSame('190700000000000000', bin2hex($encoder->encode(new FieldPresenceTestNumbers(f: new Number(7))))); + } + + /** + * @param non-empty-string $property + * @param non-empty-string $expected + */ + #[DataProvider('provideNonZeroIntegersAreEncodedCases')] + public function testNonZeroIntegersAreEncoded(string $property, int $value, string $expected): void + { + $bytes = Encoder\Builder::buildDefault()->encode(new FieldPresenceTestIntegers(...[$property => $value])); + + self::assertSame($expected, bin2hex($bytes)); + } + + /** + * @return iterable + */ + public static function provideNonZeroIntegersAreEncodedCases(): iterable + { + yield 'int32' => ['int32', -1, '08ffffffffffffffffff01']; + yield 'uint32' => ['uint32', 1, '1001']; + yield 'sint32' => ['sint32', -1, '1801']; + yield 'sint64' => ['sint64', 1, '2002']; + yield 'fixed32' => ['fixed32', 1, '2d01000000']; + yield 'sfixed32' => ['sfixed32', -1, '35ffffffff']; + yield 'sfixed64' => ['sfixed64', 1, '390100000000000000']; + } +} + +final readonly class FieldPresenceTestIntOrFloat +{ + public function __construct( + #[Reflection\Field(1, Reflection\DoubleT::T)] + public int|float $d = 0.0, + #[Reflection\Field(2, Reflection\FloatT::T)] + public int|float $f = 0.0, + ) {} +} + +final readonly class FieldPresenceTestIntegers +{ + public function __construct( + #[Reflection\Field(1, Reflection\Int32T::T)] + public int $int32 = 0, + #[Reflection\Field(2, Reflection\Uint32T::T)] + public int $uint32 = 0, + #[Reflection\Field(3, Reflection\SInt32T::T)] + public int $sint32 = 0, + #[Reflection\Field(4, Reflection\SInt64T::T)] + public int $sint64 = 0, + #[Reflection\Field(5, Reflection\Fixed32T::T)] + public int $fixed32 = 0, + #[Reflection\Field(6, Reflection\SFixed32T::T)] + public int $sfixed32 = 0, + #[Reflection\Field(7, Reflection\SFixed64T::T)] + public int $sfixed64 = 0, + ) {} +} + +final readonly class FieldPresenceTestFloats +{ + public function __construct( + #[Reflection\Field(1, Reflection\DoubleT::T)] + public float $d = 0.0, + #[Reflection\Field(2, Reflection\FloatT::T)] + public float $f = 0.0, + ) {} +} + +final readonly class FieldPresenceTestNumbers +{ + public function __construct( + #[Reflection\Field(2, Reflection\Uint64T::T)] + public Number $u = new Number(0), + #[Reflection\Field(3, Reflection\Fixed64T::T)] + public Number $f = new Number(0), + #[Reflection\Field(1, Reflection\Int64T::T)] + public int $i = 0, + ) {} +}