Skip to content
Draft
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
213 changes: 213 additions & 0 deletions lib/node_modules/@stdlib/number/float16/base/add/README.md
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 -->
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' );
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();
});
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();
});
Loading