From 8892aea2c3ed1b7dd14f4c3b4b889638833522e3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 27 Sep 2024 23:02:16 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 52 +- fixed-endian-float32/README.md | 443 +++++++++++++++ .../benchmark/benchmark.from.js | 310 ++++++++++ .../benchmark/benchmark.get.js | 85 +++ fixed-endian-float32/benchmark/benchmark.js | 533 ++++++++++++++++++ .../benchmark/benchmark.of.js | 69 +++ .../benchmark/benchmark.set.js | 209 +++++++ .../benchmark/benchmark.set.length.js | 110 ++++ .../benchmark/benchmark.to_string.js | 50 ++ .../benchmark/benchmark.to_string.length.js | 94 +++ fixed-endian-float32/examples/index.js | 42 ++ fixed-endian-float32/lib/index.js | 94 +++ fixed-endian-float32/lib/main.js | 100 ++++ fixed-endian-float32/package.json | 70 +++ fixed-endian-float32/test/test.js | 35 ++ lib/index.js | 18 + little-endian-float32/README.md | 438 ++++++++++++++ .../benchmark/benchmark.from.js | 310 ++++++++++ .../benchmark/benchmark.get.js | 57 ++ little-endian-float32/benchmark/benchmark.js | 346 ++++++++++++ .../benchmark/benchmark.of.js | 69 +++ .../benchmark/benchmark.set.js | 119 ++++ .../benchmark/benchmark.set.length.js | 110 ++++ .../benchmark/benchmark.to_string.js | 50 ++ .../benchmark/benchmark.to_string.length.js | 94 +++ little-endian-float32/examples/index.js | 42 ++ little-endian-float32/lib/index.js | 94 +++ little-endian-float32/lib/main.js | 98 ++++ little-endian-float32/package.json | 70 +++ little-endian-float32/test/test.js | 35 ++ 30 files changed, 4245 insertions(+), 1 deletion(-) create mode 100644 fixed-endian-float32/README.md create mode 100644 fixed-endian-float32/benchmark/benchmark.from.js create mode 100644 fixed-endian-float32/benchmark/benchmark.get.js create mode 100644 fixed-endian-float32/benchmark/benchmark.js create mode 100644 fixed-endian-float32/benchmark/benchmark.of.js create mode 100644 fixed-endian-float32/benchmark/benchmark.set.js create mode 100644 fixed-endian-float32/benchmark/benchmark.set.length.js create mode 100644 fixed-endian-float32/benchmark/benchmark.to_string.js create mode 100644 fixed-endian-float32/benchmark/benchmark.to_string.length.js create mode 100644 fixed-endian-float32/examples/index.js create mode 100644 fixed-endian-float32/lib/index.js create mode 100644 fixed-endian-float32/lib/main.js create mode 100644 fixed-endian-float32/package.json create mode 100644 fixed-endian-float32/test/test.js create mode 100644 little-endian-float32/README.md create mode 100644 little-endian-float32/benchmark/benchmark.from.js create mode 100644 little-endian-float32/benchmark/benchmark.get.js create mode 100644 little-endian-float32/benchmark/benchmark.js create mode 100644 little-endian-float32/benchmark/benchmark.of.js create mode 100644 little-endian-float32/benchmark/benchmark.set.js create mode 100644 little-endian-float32/benchmark/benchmark.set.length.js create mode 100644 little-endian-float32/benchmark/benchmark.to_string.js create mode 100644 little-endian-float32/benchmark/benchmark.to_string.length.js create mode 100644 little-endian-float32/examples/index.js create mode 100644 little-endian-float32/lib/index.js create mode 100644 little-endian-float32/lib/main.js create mode 100644 little-endian-float32/package.json create mode 100644 little-endian-float32/test/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index bc89b934..17755174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-09-26) +## Unreleased (2024-09-27)
@@ -20,6 +20,8 @@ ##### Features +- [`fbe6651`](https://github.com/stdlib-js/stdlib/commit/fbe6651766e6c9bf9dd941c08fbefb2cab03de1c) - add `Float32ArrayLE` to namespace +- [`65b1756`](https://github.com/stdlib-js/stdlib/commit/65b175691d20f64bdc5410232c5900b0bb2a8f1e) - add `Float32ArrayFE` to namespace - [`5a2c12b`](https://github.com/stdlib-js/stdlib/commit/5a2c12bd3973e3b3a6f5e971ecac17c391570e60) - add `Float64ArrayFE` and `Float64ArrayLE` to namespace - [`444e453`](https://github.com/stdlib-js/stdlib/commit/444e45363e751ab85736bc97b642520c3e5db301) - add `byteOrders` to namespace @@ -453,6 +455,28 @@ This release closes the following issue: +
+ +#### [@stdlib/array/fixed-endian-float32](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/fixed-endian-float32) + +
+ +
+ +##### Features + +- [`49824e6`](https://github.com/stdlib-js/stdlib/commit/49824e6545dcb1dd0ec475e0ebc7558ff71e63b2) - add `array/fixed-endian-float32` + +
+ + + +
+ +
+ + +
#### [@stdlib/array/fixed-endian-float64](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/fixed-endian-float64) @@ -497,6 +521,28 @@ This release closes the following issue: +
+ +#### [@stdlib/array/little-endian-float32](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/little-endian-float32) + +
+ +
+ +##### Features + +- [`4f1f2dd`](https://github.com/stdlib-js/stdlib/commit/4f1f2dd87a883e01b5e072b77c1443007051c1d5) - add `array/little-endian-float32` + +
+ + + +
+ +
+ + +
#### [@stdlib/array/little-endian-float64](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/little-endian-float64) @@ -559,6 +605,10 @@ A total of 7 people contributed to this release. Thank you to the following cont
+- [`fbe6651`](https://github.com/stdlib-js/stdlib/commit/fbe6651766e6c9bf9dd941c08fbefb2cab03de1c) - **feat:** add `Float32ArrayLE` to namespace _(by Athan Reines)_ +- [`4f1f2dd`](https://github.com/stdlib-js/stdlib/commit/4f1f2dd87a883e01b5e072b77c1443007051c1d5) - **feat:** add `array/little-endian-float32` _(by Athan Reines)_ +- [`65b1756`](https://github.com/stdlib-js/stdlib/commit/65b175691d20f64bdc5410232c5900b0bb2a8f1e) - **feat:** add `Float32ArrayFE` to namespace _(by Athan Reines)_ +- [`49824e6`](https://github.com/stdlib-js/stdlib/commit/49824e6545dcb1dd0ec475e0ebc7558ff71e63b2) - **feat:** add `array/fixed-endian-float32` _(by Athan Reines)_ - [`7b02c16`](https://github.com/stdlib-js/stdlib/commit/7b02c160d8c9ecf6742ea0178c733f938e0c94c4) - **chore:** minor clean-up _(by Philipp Burckhardt)_ - [`6207381`](https://github.com/stdlib-js/stdlib/commit/62073818e83d5bdd87b2b8ffea18a43767f1ae29) - **chore:** minor clean-up _(by Philipp Burckhardt)_ - [`bffda37`](https://github.com/stdlib-js/stdlib/commit/bffda37bf1104bc6e62776ee811c76873edf5461) - **refactor:** use `array/little-endian-factory` _(by Athan Reines)_ diff --git a/fixed-endian-float32/README.md b/fixed-endian-float32/README.md new file mode 100644 index 00000000..932da71e --- /dev/null +++ b/fixed-endian-float32/README.md @@ -0,0 +1,443 @@ + + +# Float32ArrayFE + +> Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order. + + + +
+ +In contrast to the [`Float32Array`][@stdlib/array/float32] typed array constructor which stores values according to the host platform byte order, the `Float32ArrayFE` constructor allows enforcing a specific byte order. Such enforcement can be particularly advantageous when working with memory buffers which do not necessarily follow host platform byte order, such as [WebAssembly memory][@stdlib/wasm/memory]. + +
+ + + + + +
+ +## Usage + +```javascript +var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +``` + +#### Float32ArrayFE( endianness ) + +A typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order. + +```javascript +var arr = new Float32ArrayFE( 'little-endian' ); +// returns +``` + +#### Float32ArrayFE( endianness, length ) + +Returns a typed array having a specified length and byte order. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +// returns +``` + +#### Float32ArrayFE( endianness, typedarray ) + +Creates a typed array from another typed array. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var arr1 = new Float32Array( [ 0.5, 0.5, 0.5 ] ); +var arr2 = new Float32ArrayFE( 'little-endian', arr1 ); +// returns + +var v = arr2.get( 0 ); +// returns 0.5 +``` + +#### Float32ArrayFE( endianness, obj ) + +Creates a typed array from an array-like object or iterable. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', [ 0.5, 0.5, 0.5 ] ); +// returns + +var v = arr.get( 0 ); +// returns 0.5 +``` + +#### Float32ArrayFE( endianness, buffer\[, byteOffset\[, length]] ) + +Returns a typed array view of an [`ArrayBuffer`][@stdlib/array/buffer]. + +```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); + +var buf = new ArrayBuffer( 32 ); +var arr = new Float32ArrayFE( 'little-endian', buf, 0, 4 ); +// returns +``` + +* * * + +### Properties + + + +#### Float32ArrayFE.BYTES_PER_ELEMENT + +Number of bytes per view element. + +```javascript +var nbytes = Float32ArrayFE.BYTES_PER_ELEMENT; +// returns 4 +``` + + + +#### Float32ArrayFE.name + +Typed array constructor name. + +```javascript +var str = Float32ArrayFE.name; +// returns 'Float32ArrayFE' +``` + + + +#### Float32ArrayFE.prototype.buffer + +**Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the typed array. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +var buf = arr.buffer; +// returns +``` + + + +#### Float32ArrayFE.prototype.byteLength + +**Read-only** property which returns the length (in bytes) of the typed array. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +var byteLength = arr.byteLength; +// returns 20 +``` + + + +#### Float32ArrayFE.prototype.byteOffset + +**Read-only** property which returns the offset (in bytes) of the typed array from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +var byteOffset = arr.byteOffset; +// returns 0 +``` + + + +#### Float32ArrayFE.prototype.BYTES_PER_ELEMENT + +Number of bytes per view element. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +var nbytes = arr.BYTES_PER_ELEMENT; +// returns 4 +``` + + + +#### Float32ArrayFE.prototype.length + +**Read-only** property which returns the number of view elements. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 5 ); +var len = arr.length; +// returns 5 +``` + +* * * + +### Methods + + + +#### Float32ArrayFE.from( endianness, src\[, map\[, thisArg]] ) + +Creates a new typed array from an array-like object or an iterable. + +```javascript +var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ] ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 +``` + +To invoke a function for each `src` value, provide a callback function. + +```javascript +function mapFcn( v ) { + return v * 2.0; +} + +var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ], mapFcn ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 +``` + +A callback function is provided two arguments: + +- **value**: source value. +- **index**: source index. + +To set the callback execution context, provide a `thisArg`. + +```javascript +function mapFcn( v ) { + this.count += 1; + return v * 2.0; +} + +var ctx = { + 'count': 0 +}; + +var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ], mapFcn, ctx ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 + +var n = ctx.count; +// returns 2 +``` + + + +#### Float32ArrayFE.of( endianness, element0\[, element1\[, ...elementN]] ) + +Creates a new typed array from a variable number of arguments. + +```javascript +var arr = Float32ArrayFE.of( 'little-endian', 1.0, -1.0 ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 +``` + + + +#### Float32ArrayFE.prototype.get( i ) + +Returns an array element located at a nonnegative integer position (index) `i`. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 10 ); + +// Set the first element: +arr.set( 1.0, 0 ); + +// Get the first element: +var v = arr.get( 0 ); +// returns 1.0 +``` + +If provided an out-of-bounds index, the method returns `undefined`. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', 10 ); + +var v = arr.get( 100 ); +// returns undefined +``` + + + +#### Float32ArrayFE.prototype.set( arr\[, offset] ) + +Sets array elements. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 + +v = arr.get( 1 ); +// returns 2.0 + +// Set the first two array elements: +arr.set( [ 4.0, 5.0 ] ); + +v = arr.get( 0 ); +// returns 4.0 + +v = arr.get( 1 ); +// returns 5.0 +``` + +By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); +// returns + +// Set the last two array elements: +arr.set( [ 4.0, 5.0 ], 1 ); + +var v = arr.get( 1 ); +// returns 4.0 + +v = arr.get( 2 ); +// returns 5.0 +``` + +A few notes: + +- If `i` is out-of-bounds, the method throws an error. +- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. +- If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + + +#### Float32ArrayFE.prototype.toString() + +Serializes an array as a string. + +```javascript +var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); + +var str = arr.toString(); +// returns '1,2,3' +``` + +
+ + + + + +
+ +* * * + +## Notes + +- The constructor supports the following byte orders: + + - **little-endian**: store values such that bytes are stored from least-to-most significant bytes. This is the dominant ordering for processor architectures and their associated memory. This is also the ordering for [WebAssembly memory][@stdlib/wasm/memory]. + - **big-endian**: store values such that bytes are stored from most-to-least significant bytes. This is the dominant ordering in network protocols. + +- While a `Float32ArrayFE` _strives_ to maintain (but does not **guarantee**) consistency with [typed arrays][@stdlib/array/typed], significant deviations from ECMAScript-defined [typed array][@stdlib/array/typed] behavior are as follows: + + - The constructor does **not** require the `new` operator. + - Accessing array elements using bracket syntax (e.g., `X[i]`) is **not** supported. Instead, one **must** use the `.get()` method. + +
+ + + + + +
+ +* * * + +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var logEach = require( '@stdlib/console/log-each' ); +var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); + +// Create a typed array by specifying a length: +var out = new Float32ArrayFE( 'little-endian', 3 ); +logEach( '%s', out ); + +// Create a typed array from an array: +var arr = [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ]; +out = new Float32ArrayFE( 'big-endian', arr ); +logEach( '%s', out ); + +// Create a typed array from an array buffer: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayFE( 'little-endian', arr.buffer ); +logEach( '%s', out ); + +// Create a typed array from an array buffer view: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayFE( 'big-endian', arr.buffer, 8, 2 ); +logEach( '%s', out ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/fixed-endian-float32/benchmark/benchmark.from.js b/fixed-endian-float32/benchmark/benchmark.from.js new file mode 100644 index 00000000..d8d94ca0 --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.from.js @@ -0,0 +1,310 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Float32Array = require( './../../float32' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ITERATOR_SYMBOL === null ) +}; + + +// MAIN // + +bench( pkg+'::typed_array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array,clbk:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf, clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +bench( pkg+'::array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = []; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array,clbk:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', buf, clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +bench( pkg+'::iterable:from', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', createIterable() ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } + } +}); + +bench( pkg+'::iterable:from:len=5', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', createIterable() ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + var it = { + 'next': next, + 'i': 0, + 'N': 5 + }; + return it; + + function next() { + it.i += 1; + if ( it.i <= it.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + } + } +}); + +bench( pkg+'::iterable,clbk:from:len=5', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.from( 'little-endian', createIterable(), clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + var it = { + 'next': next, + 'i': 0, + 'N': 5 + }; + return it; + + function next() { + it.i += 1; + if ( it.i <= it.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + } + } + + function clbk( v ) { + return v * 2.0; + } +}); diff --git a/fixed-endian-float32/benchmark/benchmark.get.js b/fixed-endian-float32/benchmark/benchmark.get.js new file mode 100644 index 00000000..525940eb --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.get.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':get:endianness=little-endian', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i ); + } + arr = new Float32ArrayFE( 'little-endian', arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.get( i%N ); + if ( typeof v !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNumber( v ) ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':get:endianness=big-endian', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i ); + } + arr = new Float32ArrayFE( 'big-endian', arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.get( i%N ); + if ( typeof v !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNumber( v ) ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-float32/benchmark/benchmark.js b/fixed-endian-float32/benchmark/benchmark.js new file mode 100644 index 00000000..907cb4a4 --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.js @@ -0,0 +1,533 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 ArrayBuffer = require( './../../buffer' ); +var Float32Array = require( './../../float32' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ITERATOR_SYMBOL === null ) +}; + + +// FUNCTIONS // + +/** +* Returns an "iterable" object. +* +* @private +* @returns {Object} iterable object +*/ +function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } +} + + +// MAIN // + +bench( pkg+'::instantiation,new:endianness=little-endian', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,new:endianness=big-endian', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,no_new:endianness=little-endian', function benchmark( b ) { + var ctor; + var arr; + var i; + + ctor = Float32ArrayFE; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = ctor( 'little-endian' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,no_new:endianness=big-endian', function benchmark( b ) { + var ctor; + var arr; + var i; + + ctor = Float32ArrayFE; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = ctor( 'big-endian' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,length:endianness=little-endian', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,length:endianness=big-endian', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,typed_array:endianness=little-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,typed_array:endianness=big-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,array:endianness=little-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = []; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,array:endianness=big-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = []; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,iterable:endianness=little-endian', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', createIterable() ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,iterable:endianness=big-endian', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', createIterable() ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer:endianness=little-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer:endianness=big-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset:endianness=little-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', buf, 8 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset:endianness=big-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', buf, 8 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset,length:endianness=little-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'little-endian', buf, 8, 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset,length:endianness=big-endian', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayFE( 'big-endian', buf, 8, 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:buffer', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayFE( 'little-endian' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.buffer; + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isArrayBuffer( v ) ) { + b.fail( 'should return an ArrayBuffer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteLength', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayFE( 'little-endian' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteLength; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteOffset', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayFE( 'little-endian' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteOffset; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:length', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayFE( 'little-endian' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.length; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-float32/benchmark/benchmark.of.js b/fixed-endian-float32/benchmark/benchmark.of.js new file mode 100644 index 00000000..6b9670ab --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.of.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':of', function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.of( 'little-endian' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 'little-endian', 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayFE.of.apply( Float32ArrayFE, buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayFE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-float32/benchmark/benchmark.set.js b/fixed-endian-float32/benchmark/benchmark.set.js new file mode 100644 index 00000000..aa7d7d9e --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.set.js @@ -0,0 +1,209 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Float32Array = require( './../../float32' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::number:set:endianness=little-endian', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayFE( 'little-endian', values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( values[ (i+1)%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::number:set:endianness=big-endian', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayFE( 'big-endian', values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( values[ (i+1)%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array:set:endianness=little-endian', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayFE( 'little-endian', values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( [ values[ (i+1)%N ] ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array:set:endianness=big-endian', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayFE( 'big-endian', values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( [ values[ (i+1)%N ] ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:set:endianness=little-endian', function benchmark( b ) { + var values; + var arr; + var buf; + var N; + var v; + var i; + + values = new Float32Array( 20 ); + N = values.length; + for ( i = 0; i < N; i++ ) { + values[ i ] = i; + } + arr = new Float32ArrayFE( 'little-endian', values ); + buf = new Float32Array( 1 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = values[ i%N ]; + v = arr.set( buf ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:set:endianness=big-endian', function benchmark( b ) { + var values; + var arr; + var buf; + var N; + var v; + var i; + + values = new Float32Array( 20 ); + N = values.length; + for ( i = 0; i < N; i++ ) { + values[ i ] = i; + } + arr = new Float32ArrayFE( 'big-endian', values ); + buf = new Float32Array( 1 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = values[ i%N ]; + v = arr.set( buf ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-float32/benchmark/benchmark.set.length.js b/fixed-endian-float32/benchmark/benchmark.set.length.js new file mode 100644 index 00000000..0281c407 --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.set.length.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/array/randu' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var values; + var arr1; + var arr2; + var arr3; + var N; + + arr1 = randu( len ); + arr2 = randu( len ); + arr3 = randu( len ); + arr1 = new Float32ArrayFE( 'little-endian', arr1 ); + + values = [ + new Float32ArrayFE( 'little-endian', arr2 ), + new Float32ArrayFE( 'big-endian', arr3 ) + ]; + N = values.length; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr1.set( values[ i%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':set:len='+len, f ); + } +} + +main(); diff --git a/fixed-endian-float32/benchmark/benchmark.to_string.js b/fixed-endian-float32/benchmark/benchmark.to_string.js new file mode 100644 index 00000000..3e1e5a1d --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.to_string.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toString', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-float32/benchmark/benchmark.to_string.length.js b/fixed-endian-float32/benchmark/benchmark.to_string.length.js new file mode 100644 index 00000000..e96ff135 --- /dev/null +++ b/fixed-endian-float32/benchmark/benchmark.to_string.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../zero-to' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayFE = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float32ArrayFE( 'little-endian', zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':toString:len='+len, f ); + } +} + +main(); diff --git a/fixed-endian-float32/examples/index.js b/fixed-endian-float32/examples/index.js new file mode 100644 index 00000000..12e93469 --- /dev/null +++ b/fixed-endian-float32/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +var Float32Array = require( './../../float32' ); +var logEach = require( '@stdlib/console/log-each' ); +var Float32ArrayFE = require( './../lib' ); + +// Create a typed array by specifying a length: +var out = new Float32ArrayFE( 'little-endian', 3 ); +logEach( '%s', out ); + +// Create a typed array from an array: +var arr = [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ]; +out = new Float32ArrayFE( 'big-endian', arr ); +logEach( '%s', out ); + +// Create a typed array from an array buffer: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayFE( 'little-endian', arr.buffer ); +logEach( '%s', out ); + +// Create a typed array from an array buffer view: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayFE( 'big-endian', arr.buffer, 8, 2 ); +logEach( '%s', out ); diff --git a/fixed-endian-float32/lib/index.js b/fixed-endian-float32/lib/index.js new file mode 100644 index 00000000..7ca737c2 --- /dev/null +++ b/fixed-endian-float32/lib/index.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order. +* +* @module @stdlib/array/fixed-endian-float32 +* +* @example +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var arr = new Float32ArrayFE( 'little-endian' ); +* // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var arr = new Float32ArrayFE( 'little-endian', 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var arr = new Float32ArrayFE( 'little-endian', [ 1.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayFE( 'little-endian', buf ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayFE( 'little-endian', buf, 4 ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float32ArrayFE( 'little-endian', buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/fixed-endian-float32/lib/main.js b/fixed-endian-float32/lib/main.js new file mode 100644 index 00000000..86f9d434 --- /dev/null +++ b/fixed-endian-float32/lib/main.js @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 factory = require( './../../fixed-endian-factory' ); + + +// MAIN // + +/** +* Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order. +* +* @name Float32ArrayFE +* @constructor +* @type {Function} +* @param {string} endianness - byte order +* @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable +* @param {NonNegativeInteger} [byteOffset=0] - byte offset +* @param {NonNegativeInteger} [length] - view length +* @throws {TypeError} first argument must be a supported byte order +* @throws {TypeError} if provided only two arguments, the second argument must be a valid argument +* @throws {TypeError} byte offset must be a nonnegative integer +* @throws {RangeError} must provide sufficient memory to accommodate byte offset and view length requirements +* @returns {Float32ArrayFE} typed array instance +* +* @example +* var arr = new Float32ArrayFE( 'little-endian' ); +* // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var arr = new Float32ArrayFE( 'little-endian', 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayFE( 'little-endian', buf ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayFE( 'little-endian', buf, 4 ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float32ArrayFE( 'little-endian', buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ +var Float32ArrayFE = factory( 'float32' ); + + +// EXPORTS // + +module.exports = Float32ArrayFE; diff --git a/fixed-endian-float32/package.json b/fixed-endian-float32/package.json new file mode 100644 index 00000000..1e288f40 --- /dev/null +++ b/fixed-endian-float32/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/array/fixed-endian-float32", + "version": "0.0.0", + "description": "Float32Array having a specified byte order.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "typed", + "typed array", + "typed-array", + "float32array", + "float32", + "float", + "single-precision", + "ieee754", + "endian", + "little-endian", + "big-endian", + "byte-order" + ] +} diff --git a/fixed-endian-float32/test/test.js b/fixed-endian-float32/test/test.js new file mode 100644 index 00000000..fc72bbf8 --- /dev/null +++ b/fixed-endian-float32/test/test.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 tape = require( 'tape' ); +var ctor = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +// TODO: add tests diff --git a/lib/index.js b/lib/index.js index b0c919b6..3d8015cc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -238,6 +238,15 @@ setReadOnly( ns, 'filledBy', require( './../filled-by' ) ); */ setReadOnly( ns, 'fixedEndianFactory', require( './../fixed-endian-factory' ) ); +/** +* @name Float32ArrayFE +* @memberof ns +* @readonly +* @constructor +* @see {@link module:@stdlib/array/fixed-endian-float32} +*/ +setReadOnly( ns, 'Float32ArrayFE', require( './../fixed-endian-float32' ) ); + /** * @name Float64ArrayFE * @memberof ns @@ -364,6 +373,15 @@ setReadOnly( ns, 'linspace', require( './../linspace' ) ); */ setReadOnly( ns, 'littleEndianFactory', require( './../little-endian-factory' ) ); +/** +* @name Float32ArrayLE +* @memberof ns +* @readonly +* @constructor +* @see {@link module:@stdlib/array/little-endian-float32} +*/ +setReadOnly( ns, 'Float32ArrayLE', require( './../little-endian-float32' ) ); + /** * @name Float64ArrayLE * @memberof ns diff --git a/little-endian-float32/README.md b/little-endian-float32/README.md new file mode 100644 index 00000000..6aa45df9 --- /dev/null +++ b/little-endian-float32/README.md @@ -0,0 +1,438 @@ + + +# Float32ArrayLE + +> Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in little-endian byte order. + + + +
+ +In contrast to the [`Float32Array`][@stdlib/array/float32] typed array constructor which stores values according to the host platform byte order, the `Float32ArrayLE` constructor always accesses elements in little-endian byte order. Such enforcement can be particularly advantageous when working with memory buffers which do not necessarily follow host platform byte order, such as [WebAssembly memory][@stdlib/wasm/memory]. + +
+ + + + + +
+ +## Usage + +```javascript +var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +``` + +#### Float32ArrayLE() + +A typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in little-endian byte order. + +```javascript +var arr = new Float32ArrayLE(); +// returns +``` + +#### Float32ArrayLE( length ) + +Returns a typed array having a specified length. + +```javascript +var arr = new Float32ArrayLE( 5 ); +// returns +``` + +#### Float32ArrayLE( typedarray ) + +Creates a typed array from another typed array. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var arr1 = new Float32Array( [ 0.5, 0.5, 0.5 ] ); +var arr2 = new Float32ArrayLE( arr1 ); +// returns + +var v = arr2.get( 0 ); +// returns 0.5 +``` + +#### Float32ArrayLE( obj ) + +Creates a typed array from an array-like object or iterable. + +```javascript +var arr = new Float32ArrayLE( [ 0.5, 0.5, 0.5 ] ); +// returns + +var v = arr.get( 0 ); +// returns 0.5 +``` + +#### Float32ArrayLE( buffer\[, byteOffset\[, length]] ) + +Returns a typed array view of an [`ArrayBuffer`][@stdlib/array/buffer]. + +```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); + +var buf = new ArrayBuffer( 32 ); +var arr = new Float32ArrayLE( buf, 0, 4 ); +// returns +``` + +* * * + +### Properties + + + +#### Float32ArrayLE.BYTES_PER_ELEMENT + +Number of bytes per view element. + +```javascript +var nbytes = Float32ArrayLE.BYTES_PER_ELEMENT; +// returns 4 +``` + + + +#### Float32ArrayLE.name + +Typed array constructor name. + +```javascript +var str = Float32ArrayLE.name; +// returns 'Float32ArrayLE' +``` + + + +#### Float32ArrayLE.prototype.buffer + +**Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the typed array. + +```javascript +var arr = new Float32ArrayLE( 5 ); +var buf = arr.buffer; +// returns +``` + + + +#### Float32ArrayLE.prototype.byteLength + +**Read-only** property which returns the length (in bytes) of the typed array. + +```javascript +var arr = new Float32ArrayLE( 5 ); +var byteLength = arr.byteLength; +// returns 20 +``` + + + +#### Float32ArrayLE.prototype.byteOffset + +**Read-only** property which returns the offset (in bytes) of the typed array from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. + +```javascript +var arr = new Float32ArrayLE( 5 ); +var byteOffset = arr.byteOffset; +// returns 0 +``` + + + +#### Float32ArrayLE.prototype.BYTES_PER_ELEMENT + +Number of bytes per view element. + +```javascript +var arr = new Float32ArrayLE( 5 ); +var nbytes = arr.BYTES_PER_ELEMENT; +// returns 4 +``` + + + +#### Float32ArrayLE.prototype.length + +**Read-only** property which returns the number of view elements. + +```javascript +var arr = new Float32ArrayLE( 5 ); +var len = arr.length; +// returns 5 +``` + +* * * + +### Methods + + + +#### Float32ArrayLE.from( src\[, map\[, thisArg]] ) + +Creates a new typed array from an array-like object or an iterable. + +```javascript +var arr = Float32ArrayLE.from( [ 1.0, -1.0 ] ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 +``` + +To invoke a function for each `src` value, provide a callback function. + +```javascript +function mapFcn( v ) { + return v * 2.0; +} + +var arr = Float32ArrayLE.from( [ 1.0, -1.0 ], mapFcn ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 +``` + +A callback function is provided two arguments: + +- **value**: source value. +- **index**: source index. + +To set the callback execution context, provide a `thisArg`. + +```javascript +function mapFcn( v ) { + this.count += 1; + return v * 2.0; +} + +var ctx = { + 'count': 0 +}; + +var arr = Float32ArrayLE.from( [ 1.0, -1.0 ], mapFcn, ctx ); +// returns + +var v = arr.get( 0 ); +// returns 2.0 + +var n = ctx.count; +// returns 2 +``` + + + +#### Float32ArrayLE.of( element0\[, element1\[, ...elementN]] ) + +Creates a new typed array from a variable number of arguments. + +```javascript +var arr = Float32ArrayLE.of( 1.0, -1.0 ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 +``` + + + +#### Float32ArrayLE.prototype.get( i ) + +Returns an array element located at a nonnegative integer position (index) `i`. + +```javascript +var arr = new Float32ArrayLE( 10 ); + +// Set the first element: +arr.set( 1.0, 0 ); + +// Get the first element: +var v = arr.get( 0 ); +// returns 1.0 +``` + +If provided an out-of-bounds index, the method returns `undefined`. + +```javascript +var arr = new Float32ArrayLE( 10 ); + +var v = arr.get( 100 ); +// returns undefined +``` + + + +#### Float32ArrayLE.prototype.set( arr\[, offset] ) + +Sets array elements. + +```javascript +var arr = new Float32ArrayLE( [ 1.0, 2.0, 3.0 ] ); +// returns + +var v = arr.get( 0 ); +// returns 1.0 + +v = arr.get( 1 ); +// returns 2.0 + +// Set the first two array elements: +arr.set( [ 4.0, 5.0 ] ); + +v = arr.get( 0 ); +// returns 4.0 + +v = arr.get( 1 ); +// returns 5.0 +``` + +By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. + +```javascript +var arr = new Float32ArrayLE( [ 1.0, 2.0, 3.0 ] ); +// returns + +// Set the last two array elements: +arr.set( [ 4.0, 5.0 ], 1 ); + +var v = arr.get( 1 ); +// returns 4.0 + +v = arr.get( 2 ); +// returns 5.0 +``` + +A few notes: + +- If `i` is out-of-bounds, the method throws an error. +- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. +- If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + + +#### Float32ArrayLE.prototype.toString() + +Serializes an array as a string. + +```javascript +var arr = new Float32ArrayLE( [ 1.0, 2.0, 3.0 ] ); + +var str = arr.toString(); +// returns '1,2,3' +``` + +
+ + + + + +
+ +* * * + +## Notes + +- While a `Float32ArrayLE` _strives_ to maintain (but does not **guarantee**) consistency with [typed arrays][@stdlib/array/typed], significant deviations from ECMAScript-defined [typed array][@stdlib/array/typed] behavior are as follows: + + - The constructor does **not** require the `new` operator. + - Accessing array elements using bracket syntax (e.g., `X[i]`) is **not** supported. Instead, one **must** use the `.get()` method. + +
+ + + + + +
+ +* * * + +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var logEach = require( '@stdlib/console/log-each' ); +var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); + +// Create a typed array by specifying a length: +var out = new Float32ArrayLE( 3 ); +logEach( '%s', out ); + +// Create a typed array from an array: +var arr = [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ]; +out = new Float32ArrayLE( arr ); +logEach( '%s', out ); + +// Create a typed array from an array buffer: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayLE( arr.buffer ); +logEach( '%s', out ); + +// Create a typed array from an array buffer view: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayLE( arr.buffer, 8, 2 ); +logEach( '%s', out ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/little-endian-float32/benchmark/benchmark.from.js b/little-endian-float32/benchmark/benchmark.from.js new file mode 100644 index 00000000..c0d15d5f --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.from.js @@ -0,0 +1,310 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Float32Array = require( './../../float32' ); +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ITERATOR_SYMBOL === null ) +}; + + +// MAIN // + +bench( pkg+'::typed_array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array,clbk:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf, clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +bench( pkg+'::array:from', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = []; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array,clbk:from:len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( buf, clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function clbk( v ) { + return v * 2.0; + } +}); + +bench( pkg+'::iterable:from', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( createIterable() ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } + } +}); + +bench( pkg+'::iterable:from:len=5', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( createIterable() ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + var it = { + 'next': next, + 'i': 0, + 'N': 5 + }; + return it; + + function next() { + it.i += 1; + if ( it.i <= it.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + } + } +}); + +bench( pkg+'::iterable,clbk:from:len=5', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.from( createIterable(), clbk ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + var it = { + 'next': next, + 'i': 0, + 'N': 5 + }; + return it; + + function next() { + it.i += 1; + if ( it.i <= it.N ) { + return { + 'value': 1.0 + }; + } + return { + 'done': true + }; + } + } + } + + function clbk( v ) { + return v * 2.0; + } +}); diff --git a/little-endian-float32/benchmark/benchmark.get.js b/little-endian-float32/benchmark/benchmark.get.js new file mode 100644 index 00000000..753a7628 --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.get.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':get', function benchmark( b ) { + var arr; + var N; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i ); + } + arr = new Float32ArrayLE( arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.get( i%N ); + if ( typeof v !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNumber( v ) ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/little-endian-float32/benchmark/benchmark.js b/little-endian-float32/benchmark/benchmark.js new file mode 100644 index 00000000..7d6c5c80 --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.js @@ -0,0 +1,346 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 ArrayBuffer = require( './../../buffer' ); +var Float32Array = require( './../../float32' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ITERATOR_SYMBOL === null ) +}; + + +// FUNCTIONS // + +/** +* Returns an "iterable" object. +* +* @private +* @returns {Object} iterable object +*/ +function createIterable() { + var out = {}; + out[ ITERATOR_SYMBOL ] = iterator; + return out; + + function iterator() { + return { + 'next': next + }; + } + + function next() { + return { + 'done': true + }; + } +} + + +// MAIN // + +bench( pkg+'::instantiation,new', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE(); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,no_new', function benchmark( b ) { + var ctor; + var arr; + var i; + + ctor = Float32ArrayLE; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = ctor(); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,length', function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,typed_array', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new Float32Array( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,array', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = []; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,iterable', opts, function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( createIterable() ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( buf ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( buf, 8 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::instantiation,arraybuffer,byte_offset,length', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = new ArrayBuffer( 8 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = new Float32ArrayLE( buf, 8, 0 ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:buffer', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayLE(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.buffer; + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isArrayBuffer( v ) ) { + b.fail( 'should return an ArrayBuffer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteLength', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayLE(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteLength; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:byteOffset', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayLE(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.byteOffset; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:length', function benchmark( b ) { + var arr; + var v; + var i; + + arr = new Float32ArrayLE(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless... + v = arr.length; + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( v ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/little-endian-float32/benchmark/benchmark.of.js b/little-endian-float32/benchmark/benchmark.of.js new file mode 100644 index 00000000..d0d1b40a --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.of.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':of', function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.of(); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':len=5', function benchmark( b ) { + var buf; + var arr; + var i; + + buf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = Float32ArrayLE.of.apply( Float32ArrayLE, buf ); + if ( arr.length !== 5 ) { + b.fail( 'should have length 5' ); + } + } + b.toc(); + if ( !(arr instanceof Float32ArrayLE) ) { + b.fail( 'should return an instance' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/little-endian-float32/benchmark/benchmark.set.js b/little-endian-float32/benchmark/benchmark.set.js new file mode 100644 index 00000000..ee3e8e93 --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.set.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Float32Array = require( './../../float32' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::number:set', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayLE( values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( values[ (i+1)%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array:set', function benchmark( b ) { + var values; + var arr; + var N; + var v; + var i; + + values = []; + for ( i = 0; i < 10; i++ ) { + values.push( i ); + } + arr = new Float32ArrayLE( values ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr.set( [ values[ (i+1)%N ] ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array:set', function benchmark( b ) { + var values; + var arr; + var buf; + var N; + var v; + var i; + + values = new Float32Array( 20 ); + N = values.length; + for ( i = 0; i < N; i++ ) { + values[ i ] = i; + } + arr = new Float32ArrayLE( values ); + buf = new Float32Array( 1 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buf[ 0 ] = values[ i%N ]; + v = arr.set( buf ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/little-endian-float32/benchmark/benchmark.set.length.js b/little-endian-float32/benchmark/benchmark.set.length.js new file mode 100644 index 00000000..052841da --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.set.length.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/array/randu' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var values; + var arr1; + var arr2; + var arr3; + var N; + + arr1 = randu( len ); + arr2 = randu( len ); + arr3 = randu( len ); + arr1 = new Float32ArrayLE( arr1 ); + + values = [ + new Float32ArrayLE( arr2 ), + new Float32ArrayLE( arr3 ) + ]; + N = values.length; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = arr1.set( values[ i%N ] ); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + } + b.toc(); + if ( typeof v !== 'undefined' ) { + b.fail( 'should return undefined' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':set:len='+len, f ); + } +} + +main(); diff --git a/little-endian-float32/benchmark/benchmark.to_string.js b/little-endian-float32/benchmark/benchmark.to_string.js new file mode 100644 index 00000000..198b9663 --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.to_string.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toString', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float32ArrayLE( [ 1.0, 2.0, 2.0, 1.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/little-endian-float32/benchmark/benchmark.to_string.length.js b/little-endian-float32/benchmark/benchmark.to_string.length.js new file mode 100644 index 00000000..ef68cdae --- /dev/null +++ b/little-endian-float32/benchmark/benchmark.to_string.length.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../zero-to' ); +var pkg = require( './../package.json' ).name; +var Float32ArrayLE = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float32ArrayLE( zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.toString(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':toString:len='+len, f ); + } +} + +main(); diff --git a/little-endian-float32/examples/index.js b/little-endian-float32/examples/index.js new file mode 100644 index 00000000..bbfb36c6 --- /dev/null +++ b/little-endian-float32/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +var Float32Array = require( './../../float32' ); +var logEach = require( '@stdlib/console/log-each' ); +var Float32ArrayLE = require( './../lib' ); + +// Create a typed array by specifying a length: +var out = new Float32ArrayLE( 3 ); +logEach( '%s', out ); + +// Create a typed array from an array: +var arr = [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ]; +out = new Float32ArrayLE( arr ); +logEach( '%s', out ); + +// Create a typed array from an array buffer: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayLE( arr.buffer ); +logEach( '%s', out ); + +// Create a typed array from an array buffer view: +arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order +out = new Float32ArrayLE( arr.buffer, 8, 2 ); +logEach( '%s', out ); diff --git a/little-endian-float32/lib/index.js b/little-endian-float32/lib/index.js new file mode 100644 index 00000000..11337b3d --- /dev/null +++ b/little-endian-float32/lib/index.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in little-endian byte order. +* +* @module @stdlib/array/little-endian-float32 +* +* @example +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var arr = new Float32ArrayLE(); +* // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var arr = new Float32ArrayLE( 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var arr = new Float32ArrayLE( [ 1.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayLE( buf ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayLE( buf, 4 ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var Float32ArrayLE = require( '@stdlib/array/little-endian-float32' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float32ArrayLE( buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/little-endian-float32/lib/main.js b/little-endian-float32/lib/main.js new file mode 100644 index 00000000..08b3a9d0 --- /dev/null +++ b/little-endian-float32/lib/main.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 factory = require( './../../little-endian-factory' ); + + +// MAIN // + +/** +* Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in little-endian byte order. +* +* @name Float32ArrayLE +* @constructor +* @type {Function} +* @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable +* @param {NonNegativeInteger} [byteOffset=0] - byte offset +* @param {NonNegativeInteger} [length] - view length +* @throws {TypeError} if provided only one argument, the argument must be a valid argument +* @throws {TypeError} byte offset must be a nonnegative integer +* @throws {RangeError} must provide sufficient memory to accommodate byte offset and view length requirements +* @returns {Float32ArrayLE} typed array instance +* +* @example +* var arr = new Float32ArrayLE(); +* // returns +* +* var len = arr.length; +* // returns 0 +* +* @example +* var arr = new Float32ArrayLE( 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var arr = new Float32ArrayLE( [ 1.0, 2.0 ] ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayLE( buf ); +* // returns +* +* var len = arr.length; +* // returns 2 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 8 ); +* var arr = new Float32ArrayLE( buf, 4 ); +* // returns +* +* var len = arr.length; +* // returns 1 +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var buf = new ArrayBuffer( 32 ); +* var arr = new Float32ArrayLE( buf, 8, 2 ); +* // returns +* +* var len = arr.length; +* // returns 2 +*/ +var Float32ArrayLE = factory( 'float32' ); + + +// EXPORTS // + +module.exports = Float32ArrayLE; diff --git a/little-endian-float32/package.json b/little-endian-float32/package.json new file mode 100644 index 00000000..d1cb24f2 --- /dev/null +++ b/little-endian-float32/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/array/little-endian-float32", + "version": "0.0.0", + "description": "Float32Array in little-endian byte order.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "typed", + "typed array", + "typed-array", + "float32array", + "float32", + "float", + "single-precision", + "ieee754", + "endian", + "little-endian", + "byte-order" + ] +} diff --git a/little-endian-float32/test/test.js b/little-endian-float32/test/test.js new file mode 100644 index 00000000..fc72bbf8 --- /dev/null +++ b/little-endian-float32/test/test.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 tape = require( 'tape' ); +var ctor = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +// TODO: add tests