-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add number/float16/base/add
#9442
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
Draft
Neerajpathak07
wants to merge
13
commits into
stdlib-js:develop
Choose a base branch
from
Neerajpathak07:float16/base/add
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bc48843
feat: adding initial js implementation
Neerajpathak07 86f020c
chore: updating markdown
Neerajpathak07 b9372de
chore: update package.json
Neerajpathak07 1ecbf96
feat: add initial C implementation
Neerajpathak07 14c8b5d
chore: clean up
Neerajpathak07 6add93e
chore: update C implementation
Neerajpathak07 6fbf509
chore: update makefile
Neerajpathak07 147ca79
chore: update copyright years
stdlib-bot 99780ce
chore: apply suggested changes
Neerajpathak07 fafe154
chore: update example description
Neerajpathak07 5d2e388
Apply suggestions from code review
kgryte 8a1b051
docs: update example
kgryte 8ff5d43
Apply suggestions from code review
kgryte 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
213 changes: 213 additions & 0 deletions
213
lib/node_modules/@stdlib/number/float16/base/add/README.md
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,213 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed 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. | ||
|
|
||
| --> | ||
|
|
||
| # add | ||
|
|
||
| > Compute the sum of two half-precision floating-point numbers. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var add = require( '@stdlib/number/float16/base/add' ); | ||
| ``` | ||
|
|
||
| #### add( x, y ) | ||
|
|
||
| Computes the sum of two half-precision floating-point numbers. | ||
|
|
||
| ```javascript | ||
| var v = add( -1.0, 5.0 ); | ||
| // returns 4.0 | ||
|
|
||
| v = add( 2.0, 5.0 ); | ||
| // returns 7.0 | ||
|
|
||
| v = add( 0.0, 5.0 ); | ||
| // returns 5.0 | ||
|
|
||
| v = add( -0.0, 0.0 ); | ||
| // returns 0.0 | ||
|
|
||
| v = add( NaN, NaN ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||
| var add = require( '@stdlib/number/float16/base/add' ); | ||
|
|
||
| var x = map( uniform( 100, -50.0, 50.0 ), toFloat16 ); | ||
| var y = map( uniform( 100, -50.0, 50.0 ), toFloat16 ); | ||
|
|
||
| logEachMap( 'add(%f, %f) => %f', x, y, add ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| #include "stdlib/number/float16/base/add.h" | ||
| ``` | ||
|
|
||
| #### stdlib_base_float16_add( x, y ) | ||
|
|
||
| Computes the sum of two half-precision floating-point numbers. | ||
|
|
||
| ```c | ||
| #include "stdlib/number/float16/ctor.h" | ||
|
|
||
| stdlib_float16_t x = stdlib_float16_from_bits( 17664 ); // => 5.0 | ||
| stdlib_float16_t y = stdlib_float16_from_bits( 16384 ); // => 2.0 | ||
|
|
||
| stdlib_float16_t v = stdlib_base_float16_add( x, y ); | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **x**: `[in] stdlib_float16_t` first input value. | ||
| - **y**: `[in] stdlib_float16_t` second input value. | ||
|
|
||
| ```c | ||
| stdlib_float16_t stdlib_base_float16_add( const stdlib_float16_t x, const stdlib_float16_t y ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ### Examples | ||
|
|
||
| ```c | ||
| #include "stdlib/number/float16/base/add.h" | ||
| #include "stdlib/number/float16/ctor.h" | ||
| #include "stdlib/number/float32/base/to_float16.h" | ||
| #include "stdlib/number/float16/base/to_float32.h" | ||
| #include <stdio.h> | ||
|
|
||
| int main( void ) { | ||
| const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; | ||
| const float y[] = { 3.14f, -3.14f, -0.0f, 0.0f/0.0f }; | ||
|
|
||
| stdlib_float16_t v; | ||
| stdlib_float16_t w; | ||
| stdlib_float16_t z; | ||
| int i; | ||
| for ( i = 0; i < 4; i++ ) { | ||
| v = stdlib_base_float32_to_float16( x[ i ] ); | ||
| w = stdlib_base_float32_to_float16( y[ i ] ); | ||
| z = stdlib_base_float16_add( v, w ); | ||
| printf( "%f + %f = %f\n", stdlib_base_float16_to_float32( v ), stdlib_base_float16_to_float32( w ), stdlib_base_float16_to_float32( z ) ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
80 changes: 80 additions & 0 deletions
80
lib/node_modules/@stdlib/number/float16/base/add/benchmark/benchmark.js
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,80 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var add = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var out; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
| y = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = add( x[ i%x.length ], y[ i%y.length ] ); | ||
| if ( isnan( out ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( out ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s::inline', pkg ), function benchmark( b ) { | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = x[ i%x.length ] + 5.0; | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
67 changes: 67 additions & 0 deletions
67
lib/node_modules/@stdlib/number/float16/base/add/benchmark/benchmark.native.js
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,67 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var resolve = require( 'path' ).resolve; | ||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var tryRequire = require( '@stdlib/utils/try-require' ); | ||
| var pkg = require( './../package.json' ).name; | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var add = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
| var opts = { | ||
| 'skip': ( add instanceof Error ) | ||
| }; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::native', pkg ), opts, function benchmark( b ) { | ||
| var out; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
| y = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = add( x[ i%x.length ], y[ i%y.length ] ); | ||
| if ( isnan( out ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( out ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.