Skip to content

feat: add array/float16 #7435

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
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

udaykakade25
Copy link
Contributor

progresses #7347 and #7273

Description

What is the purpose of this pull request?

This pull request:

  • Adds array/float16 folder

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

This is the third PR. First two PRs are interconnected to each other. In order to merge this PR, we need to first merge PR 1: #7273 and #7347

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Jun 21, 2025
@udaykakade25
Copy link
Contributor Author

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Jun 21, 2025
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Jun 21, 2025
@udaykakade25
Copy link
Contributor Author

Hello @Planeshifter and @kgryte sir,
With this PR, i have completed array/float16.
Please do review and merge PR along with two previously mentioned PRs.

So, i can start working on adding float16 datatype support for array/base, array/*, strided/8 and ndarray/*


// MAIN //

class Float16Array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire implementation should be refactored. We do not use ES6+. All code must be written in ES5. Furthermore, all methods and properties should be documented. See array/bool and array/complex128 for what we expect.

return Float16Array.fromFloat16(this._buffer[index]);
}

get buffer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file also does not conform to our style conventions and includes mixed tabs and spaces. Please follow our development guide and ensure that your local environment is properly setup. Our automated linting and EditorConfig configuration would have flagged all these issues.

const offset = value !== undefined ? Number(value) : 0;

if (offset < 0 || offset > this.length) {
throw new RangeError('Offset is out of bounds');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how we format error messages.

(ArrayBuffer.isView(source) && typeof source.length === 'number') ||
(source instanceof Float16Array))
) {
const len = Math.min(source.length, this.length - offset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use Math built-ins. Everything should be implemented in terms of stdlib functionality.

return this._entriesGenerator();
}

*_entriesGenerator() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entries should be implemented using iterators directly. Use of generators is not allowed in ES5.

Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@udaykakade25 Thank you for working on this. I left a few initial comments. The TL;DR is that the entire implementation needs to be reworked to be implemented in ES5. I strongly encourage you to emulate the style and approach used in array/bool and other custom array types implemented in stdlib.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jun 21, 2025
@stdlib-bot stdlib-bot added the Potential Duplicate There might be another pull request resolving the same issue. label Jun 22, 2025
Signed-off-by: Uday Kakade <[email protected]>
@udaykakade25
Copy link
Contributor Author

@udaykakade25 Thank you for working on this. I left a few initial comments. The TL;DR is that the entire implementation needs to be reworked to be implemented in ES5. I strongly encourage you to emulate the style and approach used in array/bool and other custom array types implemented in stdlib.

Hello @kgryte sir, I have refactored the whole polyfill.js file that follows stdlib style. Thanks for reviewing!!

* @throws {TypeError} Must provide valid input
*/
function Float16Array( input, byteOffset, length ) {
var i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be all TAB indentation. Currently, you are using space indentation.

if ( typeof input === 'number' ) {
this._buffer = new Uint16Array( input );
} else if ( input instanceof ArrayBuffer ) {
offset = byteOffset >>> 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct. >>> 0 casts byteOffset to uint32, but typed arrays can have more than 2**32 - 1 elements.

* @readonly
* @type {ArrayBuffer}
*/
Object.defineProperty( Float16Array.prototype, 'buffer', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in array/bool, you should be using our utilities for defining properties. E.g., utils/define-nonenumerable-read-only-accessor. Right now, buffer is enumerable, which it shouldn't be.

*/
Float16Array.from = function( source, mapFn, thisArg ) {
if ( source == null ) {
throw new TypeError( 'invalid argument. Source cannot be null or undefined. Value: `' + source + '`.' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For error formatting, as in array/bool, you should be using @stdlib/string/format.

var result = new Float16Array( this.length );
var i;
var val;
if ( typeof callback !== 'function' ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to be using stdlib assertion utilities for validation here and everywhere else. E.g., assert/is-function.

* @param {*} [thisArg] - Context
* @returns {boolean} Whether all pass
*/
Float16Array.prototype.every = function( callback, thisArg ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define methods in alphabetical order. This makes searching for a particular easier for future readers of this code.

*
* @returns {Object} Iterator
*/
Float16Array.prototype.keys = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use anonymous functions. We used named functions to assist in making stack traces more legible.


this.length = this._buffer.length;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing static constructor properties, such the constructor name and BYTES_PER_ELEMENT.

@kgryte
Copy link
Member

kgryte commented Jun 29, 2025

Also, for this PR to move forward, CI should be passing. Currently, CI is failing on linting, benchmarks, examples, and tests.

Signed-off-by: Uday Kakade <[email protected]>
@udaykakade25
Copy link
Contributor Author

Also, for this PR to move forward, CI should be passing. Currently, CI is failing on linting, benchmarks, examples, and tests.

Hello @kgryte sir,
I have changes that follow the standards below
TABS Indentation ✅
Alphabetical Order ✅
Offset >>> 0 fix ✅
Readonly added ✅
Error formatting ✅
Utilities Added ✅
No Anonymous functions ✅
Missing Statistics Added ✅

Regarding failed CI checks:
Just Linting errors in Readme.md and Repl.txt are remaining to fix.
Rest other issues are encountered because the assert\has-float16array-support #7347 and assert/is-float16array #7273 PRs is still pending.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Changes Pull request which needs changes before being merged. Potential Duplicate There might be another pull request resolving the same issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants