Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/fory_compiler/generators/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def generate_bytes_methods(self, class_name: str) -> List[str]:
PrimitiveKind.UINT64: "long",
PrimitiveKind.VAR_UINT64: "long",
PrimitiveKind.TAGGED_UINT64: "long",
PrimitiveKind.FLOAT16: "float",
PrimitiveKind.FLOAT16: "Float16",
PrimitiveKind.FLOAT32: "float",
PrimitiveKind.FLOAT64: "double",
PrimitiveKind.STRING: "String",
Expand Down Expand Up @@ -255,7 +255,7 @@ def generate_bytes_methods(self, class_name: str) -> List[str]:
PrimitiveKind.UINT64: "Long",
PrimitiveKind.VAR_UINT64: "Long",
PrimitiveKind.TAGGED_UINT64: "Long",
PrimitiveKind.FLOAT16: "Float",
PrimitiveKind.FLOAT16: "Float16",
PrimitiveKind.FLOAT32: "Float",
PrimitiveKind.FLOAT64: "Double",
PrimitiveKind.ANY: "Object",
Expand All @@ -278,7 +278,7 @@ def generate_bytes_methods(self, class_name: str) -> List[str]:
PrimitiveKind.UINT64: "long[]",
PrimitiveKind.VAR_UINT64: "long[]",
PrimitiveKind.TAGGED_UINT64: "long[]",
PrimitiveKind.FLOAT16: "float[]",
PrimitiveKind.FLOAT16: "Float16[]",
PrimitiveKind.FLOAT32: "float[]",
PrimitiveKind.FLOAT64: "double[]",
}
Expand Down Expand Up @@ -1165,6 +1165,8 @@ def collect_type_imports(
imports.add("java.time.LocalDate")
elif field_type.kind == PrimitiveKind.TIMESTAMP:
imports.add("java.time.Instant")
elif field_type.kind == PrimitiveKind.FLOAT16:
imports.add("org.apache.fory.type.Float16")

elif isinstance(field_type, ListType):
# Primitive arrays don't need List import
Expand Down Expand Up @@ -1379,7 +1381,11 @@ def generate_equals_method(self, message: Message) -> List[str]:
)
elif isinstance(field.field_type, PrimitiveType):
kind = field.field_type.kind
if kind in (PrimitiveKind.FLOAT32,):
if kind in (PrimitiveKind.FLOAT16,):
comparisons.append(
f"{field_name}.equalsValue(that.{field_name})"
)
elif kind in (PrimitiveKind.FLOAT32,):
comparisons.append(
f"Float.compare({field_name}, that.{field_name}) == 0"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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.
*/

package org.apache.fory.collection;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Objects;
import java.util.RandomAccess;
import org.apache.fory.type.Float16;

public final class Float16List extends AbstractList<Float16> implements RandomAccess {
private static final int DEFAULT_CAPACITY = 10;

private short[] array;
private int size;

public Float16List() {
this(DEFAULT_CAPACITY);
}

public Float16List(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal capacity: " + initialCapacity);
}
this.array = new short[initialCapacity];
this.size = 0;
}

public Float16List(short[] array) {
this.array = array;
this.size = array.length;
}

@Override
public Float16 get(int index) {
checkIndex(index);
return Float16.fromBits(array[index]);
}

@Override
public int size() {
return size;
}

@Override
public Float16 set(int index, Float16 element) {
checkIndex(index);
Objects.requireNonNull(element, "element");
short prev = array[index];
array[index] = element.toBits();
return Float16.fromBits(prev);
}

public void set(int index, short bits) {
checkIndex(index);
array[index] = bits;
}

public void set(int index, float value) {
checkIndex(index);
array[index] = Float16.valueOf(value).toBits();
}

@Override
public void add(int index, Float16 element) {
checkPositionIndex(index);
ensureCapacity(size + 1);
System.arraycopy(array, index, array, index + 1, size - index);
array[index] = element.toBits();
size++;
modCount++;
}

@Override
public boolean add(Float16 element) {
Objects.requireNonNull(element, "element");
ensureCapacity(size + 1);
array[size++] = element.toBits();
modCount++;
return true;
}

public boolean add(short bits) {
ensureCapacity(size + 1);
array[size++] = bits;
modCount++;
return true;
}

public boolean add(float value) {
ensureCapacity(size + 1);
array[size++] = Float16.valueOf(value).toBits();
modCount++;
return true;
}

public float getFloat(int index) {
checkIndex(index);
return Float16.fromBits(array[index]).toFloat();
}

public short getShort(int index) {
checkIndex(index);
return array[index];
}

public boolean hasArray() {
return array != null;
}

public short[] getArray() {
return array;
}

public short[] copyArray() {
return Arrays.copyOf(array, size);
}

private void ensureCapacity(int minCapacity) {
if (array.length >= minCapacity) {
return;
}
int newCapacity = array.length + (array.length >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
array = Arrays.copyOf(array, newCapacity);
}

private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}

private void checkPositionIndex(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.fory.resolver.TypeResolver;
import org.apache.fory.serializer.collection.CollectionFlags;
import org.apache.fory.serializer.collection.ForyArrayAsListSerializer;
import org.apache.fory.type.Float16;
import org.apache.fory.type.GenericType;
import org.apache.fory.type.TypeUtils;
import org.apache.fory.util.Preconditions;
Expand Down Expand Up @@ -881,6 +882,50 @@ private void readFloat64BySwapEndian(MemoryBuffer buffer, double[] values, int n
}
}

public static final class Float16ArraySerializer extends PrimitiveArraySerializer<Float16[]> {

public Float16ArraySerializer(Fory fory) {
super(fory, Float16[].class);
}

@Override
public void write(MemoryBuffer buffer, Float16[] value) {
int length = value.length;
int size = length * 2;
buffer.writeVarUint32Small7(size);

int writerIndex = buffer.writerIndex();
buffer.ensure(writerIndex + size);

for (int i = 0; i < length; i++) {
buffer._unsafePutInt16(writerIndex + i * 2, value[i].toBits());
}
buffer._unsafeWriterIndex(writerIndex + size);
}

@Override
public Float16[] copy(Float16[] originArray) {
return Arrays.copyOf(originArray, originArray.length);
}

@Override
public Float16[] read(MemoryBuffer buffer) {
int size = buffer.readVarUint32Small7();
int numElements = size / 2;
Float16[] values = new Float16[numElements];

int readerIndex = buffer.readerIndex();
buffer.checkReadableBytes(size);

for (int i = 0; i < numElements; i++) {
values[i] = Float16.fromBits(buffer._unsafeGetInt16(readerIndex + i * 2));
}
buffer._increaseReaderIndexUnsafe(size);

return values;
}
}

public static final class StringArraySerializer extends Serializer<String[]> {
private final StringSerializer stringSerializer;
private final ForyArrayAsListSerializer collectionSerializer;
Expand Down Expand Up @@ -1008,6 +1053,7 @@ public static void registerDefaultSerializers(Fory fory) {
resolver.registerInternalSerializer(double[].class, new DoubleArraySerializer(fory));
resolver.registerInternalSerializer(
Double[].class, new ObjectArraySerializer<>(fory, Double[].class));
resolver.registerInternalSerializer(Float16[].class, new Float16ArraySerializer(fory));
resolver.registerInternalSerializer(boolean[].class, new BooleanArraySerializer(fory));
resolver.registerInternalSerializer(
Boolean[].class, new ObjectArraySerializer<>(fory, Boolean[].class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

package org.apache.fory.serializer;

import org.apache.fory.Fory;
import org.apache.fory.memory.MemoryBuffer;
import org.apache.fory.type.Float16;

public final class Float16Serializer extends ImmutableSerializer<Float16> {

public Float16Serializer(Fory fory) {
super(fory, Float16.class);
}

@Override
public void write(MemoryBuffer buffer, Float16 value) {
buffer.writeInt16(value.toBits());
}

@Override
public Float16 read(MemoryBuffer buffer) {
return Float16.fromBits(buffer.readInt16());
}

@Override
public void xwrite(MemoryBuffer buffer, Float16 value) {
buffer.writeInt16(value.toBits());
}

@Override
public Float16 xread(MemoryBuffer buffer) {
return Float16.fromBits(buffer.readInt16());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.fory.memory.Platform;
import org.apache.fory.resolver.TypeResolver;
import org.apache.fory.serializer.Serializers.CrossLanguageCompatibleSerializer;
import org.apache.fory.type.Float16;
import org.apache.fory.util.Preconditions;

/** Serializers for java primitive types. */
Expand Down Expand Up @@ -342,6 +343,22 @@ public Double read(MemoryBuffer buffer) {
}
}

public static final class Float16Serializer extends CrossLanguageCompatibleSerializer<Float16> {
public Float16Serializer(Fory fory, Class<?> cls) {
super(fory, (Class) cls, false, true);
}

@Override
public void write(MemoryBuffer buffer, Float16 value) {
buffer.writeInt16(value.toBits());
}

@Override
public Float16 read(MemoryBuffer buffer) {
return Float16.fromBits(buffer.readInt16());
}
}

public static void registerDefaultSerializers(Fory fory) {
// primitive types will be boxed.
TypeResolver resolver = fory.getTypeResolver();
Expand All @@ -361,5 +378,6 @@ public static void registerDefaultSerializers(Fory fory) {
resolver.registerInternalSerializer(Long.class, new LongSerializer(fory, Long.class));
resolver.registerInternalSerializer(Float.class, new FloatSerializer(fory, Float.class));
resolver.registerInternalSerializer(Double.class, new DoubleSerializer(fory, Double.class));
resolver.registerInternalSerializer(Float16.class, new Float16Serializer(fory, Float16.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.fory.meta.TypeDef;
import org.apache.fory.reflect.ReflectionUtils;
import org.apache.fory.resolver.TypeResolver;
import org.apache.fory.type.Float16;
import org.apache.fory.util.ExceptionUtils;
import org.apache.fory.util.GraalvmSupport;
import org.apache.fory.util.GraalvmSupport.GraalvmSerializerHolder;
Expand Down Expand Up @@ -595,6 +596,7 @@ public static void registerDefaultSerializers(Fory fory) {
resolver.registerInternalSerializer(URI.class, new URISerializer(fory));
resolver.registerInternalSerializer(Pattern.class, new RegexSerializer(fory));
resolver.registerInternalSerializer(UUID.class, new UUIDSerializer(fory));
resolver.registerInternalSerializer(Float16.class, new Float16Serializer(fory));
resolver.registerInternalSerializer(Object.class, new EmptyObjectSerializer(fory));
}
}
Loading
Loading