chaokunyang commented on code in PR #3527: URL: https://github.com/apache/fory/pull/3527#discussion_r3062376165
########## dart/packages/fory-test/test/datatype_test/bfloat16_test.dart: ########## @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'package:test/test.dart'; +import 'package:fory/fory.dart'; + +void main() { + group('BFloat16', () { + test('converts from and to bits', () { + var bf = BFloat16.fromBits(0x3f80); // 1.0 in bfloat16 + expect(bf.toBits(), 0x3f80); + expect(bf.toFloat32(), 1.0); + expect(bf.value, 1.0); + }); + + test('converts from float32', () { + var bf = BFloat16.fromFloat32(1.0); + expect(bf.toBits(), 0x3f80); + expect(bf.toFloat32(), 1.0); + + var bf2 = BFloat16.fromFloat32(-1.0); + expect(bf2.toFloat32(), -1.0); + + var bf3 = BFloat16.fromFloat32(0.0); + expect(bf3.toFloat32(), 0.0); + }); + + test('equality and hashcode', () { + var bf1 = BFloat16.fromBits(0x3f80); + var bf2 = BFloat16.fromFloat32(1.0); + var bf3 = BFloat16.fromFloat32(2.0); + + expect(bf1 == bf2, isTrue); + expect(bf1 == bf3, isFalse); + expect(bf1.hashCode == bf2.hashCode, isTrue); + }); + }); + + group('BFloat16Array', () { + test('creates from length', () { + var arr = BFloat16Array.fromLength(5); + expect(arr.length, 5); + expect(arr.raw.length, 5); + }); + + test('creates from list and sets values', () { Review Comment: These tests only cover the wrapper API. They never round-trip `BFloat16Array` through `Fory.serialize` / `deserialize`, which is why they still pass even though `BFloat16Array` is currently unregistered and cannot be serialized. Please add a serializer round-trip case for the new array type so this feature is actually verified. ########## dart/packages/fory/lib/src/datatype/bfloat16.dart: ########## @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'dart:typed_data'; + +import 'fory_fixed_num.dart'; + +/// BFloat16: 16-bit brain floating point type. +/// Wraps a 16-bit integer representing the bfloat16 format. +final class BFloat16 extends FixedNum { + /// The raw 16-bit integer storage. + final int _bits; + + /// Internal constructor from raw bits. + const BFloat16._(this._bits); + + /// Creates a [BFloat16] from a number. + factory BFloat16(num value) => BFloat16.fromFloat32(value.toDouble()); + + /// Returns the raw 16-bit integer representation. + int toBits() { + return _bits; + } + + /// Creates a [BFloat16] from a raw 16-bit integer. + factory BFloat16.fromBits(int bits) { + return BFloat16._(bits & 0xffff); + } + + /// Creates a [BFloat16] by converting a standard 32-bit floating-point number. + factory BFloat16.fromFloat32(double f32) { + var float32View = Float32List(1); + var uint32View = Uint32List.view(float32View.buffer); + + float32View[0] = f32; + var bits = uint32View[0]; + var exponent = (bits >> 23) & 0xff; + if (exponent == 255) { + return BFloat16.fromBits((bits >> 16) & 0xffff); + } + var remainder = bits & 0x1ffff; + var u = (bits + 0x8000) >> 16; + if (remainder == 0x8000 && (u & 1) != 0) { + u--; + } + return BFloat16.fromBits(u & 0xffff); + } + + /// Converts this [BFloat16] to a standard 32-bit floating-point number. + double toFloat32() { + var float32View = Float32List(1); + var uint32View = Uint32List.view(float32View.buffer); + + float32View[0] = 0.0; + uint32View[0] = _bits << 16; + return float32View[0]; + } + + /// Gets the numeric value as a double. + @override + num get value => toFloat32(); + + @override + String toString() => 'BFloat16($value)'; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is BFloat16 && + runtimeType == other.runtimeType && + _bits == other._bits; + + @override + int get hashCode => _bits.hashCode; +} + +/// A fixed-length list of [BFloat16] values, backed by a [Uint16List]. +class BFloat16Array { Review Comment: `BFloat16Array` is public here, but the runtime wiring still only handles the scalar `BFloat16`. `DartTypeResolver.getForyType()` only special-cases `List` and `TypedDataList`, and neither `SerializerPool` nor `DartTypeEnum` register `BFloat16Array` / `ObjType.BFLOAT16_ARRAY`, so `Fory().serialize(BFloat16Array.fromList([1.0]))` currently fails with `Unregistered type: BFloat16Array`. This needs the same built-in array registration path as the other default array types before `bfloat16_array` support is actually complete. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
