-
Notifications
You must be signed in to change notification settings - Fork 401
feat(java): add float support #3254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mengnankkkk
wants to merge
26
commits into
apache:main
Choose a base branch
from
mengnankkkk:hotfix/add_float16
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
46e0e29
fix: init
mengnankkkk a610788
fix: remove
mengnankkkk 7fc45b8
fix: add float16
mengnankkkk 65f6ea3
fix: add float16serializer
mengnankkkk 6e37abb
fix: add float16list
mengnankkkk f5ba942
fix: add native
mengnankkkk ad18586
Update native-image.properties
mengnankkkk 5cfb49f
fix: add
mengnankkkk 2772ff2
Update native-image.properties
mengnankkkk f6f38d3
Update native-image.properties
mengnankkkk 578c05e
fix: ci
mengnankkkk 0b98295
Update java/fory-core/src/main/java/org/apache/fory/type/Float16.java
mengnankkkk 004663d
fix: remove something
mengnankkkk 809ced1
Merge branch 'main' into hotfix/add_float16
mengnankkkk 65f2667
fix: remove
mengnankkkk 602aaed
fix: ci
mengnankkkk 18f899c
fix
mengnankkkk 77178dd
fix: fix codereview
mengnankkkk 343a6ce
fix: from cr
mengnankkkk f5ee398
Merge branch 'main' into hotfix/add_float16
mengnankkkk b096c9b
Update Float16Serializer.java
mengnankkkk a692296
fix: ci
mengnankkkk ec14a50
Update Float16Serializer.java
mengnankkkk 91e5bbd
Merge branch 'main' into hotfix/add_float16
mengnankkkk 831009d
fix
mengnankkkk c33e97e
fix: ci
mengnankkkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
java/fory-core/src/main/java/org/apache/fory/collection/Float16List.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.toBits(value); | ||
| } | ||
|
|
||
| @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.toBits(value); | ||
| modCount++; | ||
| return true; | ||
| } | ||
|
|
||
| public float getFloat(int index) { | ||
| checkIndex(index); | ||
| return Float16.toFloat(array[index]); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Float16List primitive methods still allocate Float16 objects on the hot path.
add(float), set(float), and getFloat(int) all go through Float16.valueOf/fromBits, so these paths allocate even though this is a primitive-style container. A static bits conversion helper would keep these methods allocation-free.