Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /** * @license Apache-2.0 * * Copyright (c) 2025 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 setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var isObject = require( '@stdlib/assert/is-object' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isCollection = require( '@stdlib/assert/is-collection' ); var isFunction = require( '@stdlib/assert/is-function' ); var floor = require( '@stdlib/math/base/special/floor' ); var min = require( '@stdlib/math/base/special/min' ); var max = require( '@stdlib/math/base/special/max' ); var isArray = require( '@stdlib/assert/is-array' ); var format = require( '@stdlib/string/format' ); var iteratorSymbol = require( '@stdlib/symbol/iterator' ); var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); // MAIN // /** * Creates a new Float16Array constructor. * * @constructor * @param {(number|Array|ArrayBuffer|TypedArray)} input - input data or buffer * @param {NonNegativeInteger} [byteOffset=0] - byte offset * @param {NonNegativeInteger} [length] - number of elements * @throws {TypeError} must provide valid input */ function Float16Array( input, byteOffset, length ) { var i; var len; var offset; byteOffset = byteOffset || 0; if ( isNonNegativeInteger( input ) ) { this._buffer = new Uint16Array( input ); } else if ( input instanceof ArrayBuffer ) { offset = Number( byteOffset ); if ( !isNonNegativeInteger( offset ) ) { throw new RangeError( format( 'invalid argument. Byte offset must be a non-negative integer. Value: `%s`.', byteOffset ) ); } if ( arguments.length > 2 ) { if ( !isNonNegativeInteger( length ) ) { throw new RangeError( format( 'invalid argument. Length must be a non-negative integer. Value: `%s`.', length ) ); } len = length; } else { len = ( input.byteLength - offset ) / 2; } this._buffer = new Uint16Array( input, offset, len ); } else if ( isArray( input ) ) { this._buffer = new Uint16Array( input.length ); for ( i = 0; i < input.length; i++ ) { this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); } } else if ( isCollection( input ) ) { this._buffer = new Uint16Array( input.length ); for ( i = 0; i < input.length; i++ ) { this._buffer[ i ] = Float16Array.toFloat16( input[ i ] ); } } else { throw new TypeError( format( 'invalid argument. Must provide a number, Array, ArrayBuffer, or typed array. Value: `%s`.', input ) ); } this.length = this._buffer.length; } setReadOnly( Float16Array, 'name', 'Float16Array' ); setReadOnly( Float16Array, 'BYTES_PER_ELEMENT', 2 ); /** * Get the underlying `ArrayBuffer`. * * @name buffer * @memberof Float16Array.prototype * @readonly * @type {ArrayBuffer} */ setReadOnlyAccessor( Float16Array.prototype, 'buffer', function getBuffer() { return this._buffer.buffer; }); /** * Get the total number of bytes in the `Float16Array`. * * @name byteLength * @memberof Float16Array.prototype * @readonly * @type {NonNegativeInteger} */ setReadOnlyAccessor( Float16Array.prototype, 'byteLength', function getByteLength() { return this._buffer.byteLength; }); /** * Get the byte offset within the underlying `ArrayBuffer`. * * @name byteOffset * @memberof Float16Array.prototype * @readonly * @type {NonNegativeInteger} */ setReadOnlyAccessor( Float16Array.prototype, 'byteOffset', function getByteOffset() { return this._buffer.byteOffset; }); /** * Size in bytes of each element. * * @name BYTES_PER_ELEMENT * @memberof Float16Array.prototype * @readonly * @type {PositiveInteger} */ setReadOnlyAccessor( Float16Array.prototype, 'BYTES_PER_ELEMENT', function getBytesPerElement() { return 2; }); /** * Returns the default string tag. * * @name [Symbol.toStringTag] * @memberof Float16Array.prototype * @readonly * @type {string} */ var symbolToStringTag = hasToStringTag() ? Symbol.toStringTag : '__toStringTag__'; setReadOnlyAccessor( Float16Array.prototype, symbolToStringTag, function getToStringTag() { return 'Float16Array'; }); /** * Copy a sequence of array elements within the array. * * @name copyWithin * @memberof Float16Array.prototype * @type {Function} * @param {integer} target - target index * @param {integer} start - start index * @param {integer} [end] - end index (exclusive) * @throws {TypeError} if any index is not an integer * @throws {RangeError} if any index is out of bounds * @returns {Float16Array} modified array */ setReadOnly( Float16Array.prototype, 'copyWithin', function copyWithin( target, start, end ) { var to; var from; var final; var count; var i; if ( !isInteger( target ) ) { throw new TypeError( format( 'invalid argument. Target index must be an integer. Value: `%s`.', target ) ); } if ( !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } if ( arguments.length > 2 && !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); } to = ( target < 0 ) ? this.length + target : target; from = ( start < 0 ) ? this.length + start : start; final = ( arguments.length < 3 ) ? this.length : ( end < 0 ? this.length + end : end ); if ( to < 0 || to > this.length ) { throw new RangeError( format( 'invalid argument. Target index is out of bounds. Value: `%s`.', target ) ); } if ( from < 0 || from > this.length ) { throw new RangeError( format( 'invalid argument. Start index is out of bounds. Value: `%s`.', start ) ); } if ( final < 0 || final > this.length ) { throw new RangeError( format( 'invalid argument. End index is out of bounds. Value: `%s`.', end ) ); } count = min( final - from, this.length - to ); if ( from < to && to < ( from + count ) ) { for ( i = count - 1; i >= 0; i-- ) { this._buffer[ to + i ] = this._buffer[ from + i ]; } } else { for ( i = 0; i < count; i++ ) { this._buffer[ to + i ] = this._buffer[ from + i ]; } } return this; }); /** * Return an iterator over [index, value] pairs. * * @name entries * @memberof Float16Array.prototype * @type {Function} * @returns {Object} Iterator */ setReadOnly( Float16Array.prototype, 'entries', function entries() { var self = this; var i = 0; function nextEntries() { if ( i < self.length ) { var v = [ i, self.get( i ) ]; i += 1; return { value: v, done: false }; } return { value: void 0, done: true }; } function getEntriesIterator() { return iterator; } var iterator = { next: nextEntries }; if ( iteratorSymbol ) { iterator[ iteratorSymbol ] = getEntriesIterator; } return iterator; }); /** * Test whether all elements pass the test. * * @name every * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - predicate function * @param {*} [thisArg] - context * @returns {boolean} whether all pass */ setReadOnly( Float16Array.prototype, 'every', function every( callback, thisArg ) { var i; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { if ( !callback.call( thisArg, this.get( i ), i, this ) ) { return false; } } return true; }); /** * Fill the array with a static value. * * @name fill * @memberof Float16Array.prototype * @type {Function} * @param {*} value - value to fill * @param {integer} [start=0] - start index * @param {integer} [end=array.length] - end index * @throws {TypeError} if start or end are not integers * @returns {Float16Array} modified array */ setReadOnly( Float16Array.prototype, 'fill', function fill( value, start, end ) { var i; var bits; var s; var e; function createFloat16Getter( index ) { return function getFloat16Index() { return this.get( index ); }; } function createFloat16Setter( index ) { return function setFloat16Index( val ) { this.set( index, val ); }; } if ( arguments.length > 1 && !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } if ( arguments.length > 2 && !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. End index must be an integer when provided. Value: `%s`.', end ) ); } s = ( arguments.length < 2 ) ? 0 : ( start < 0 ? max( this.length + start, 0 ) : min( start, this.length ) ); e = ( arguments.length < 3 ) ? this.length : ( end < 0 ? max( this.length + end, 0 ) : min( end, this.length ) ); bits = Float16Array.toFloat16( value ); for ( i = s; i < e; i++ ) { this._buffer[ i ] = bits; Object.defineProperty( this, i, { get: createFloat16Getter( i ), set: createFloat16Setter( i ), enumerable: true, configurable: true }); } return this; }); /** * Creates a new Float16Array with all elements that pass the test implemented by the provided function. * * @name filter * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - function to test each element * @param {*} [thisArg] - value to use as `this` when executing `callback` * @throws {TypeError} must provide a function as callback * @returns {Float16Array} new Float16Array with filtered elements */ setReadOnly( Float16Array.prototype, 'filter', function filter( callback, thisArg ) { var i; var value; var result; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } result = []; for ( i = 0; i < this.length; i++ ) { value = this.get( i ); if ( callback.call( thisArg, value, i, this ) ) { result.push( value ); } } return new Float16Array( result ); }); /** * Returns the value of the first element that satisfies the provided testing function. * * @name find * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - function to execute on each value * @param {*} [thisArg] - value to use as `this` when executing `callback` * @throws {TypeError} must provide a function as callback * @returns {*} first matching element, or undefined if no match */ setReadOnly( Float16Array.prototype, 'find', function find( callback, thisArg ) { var i; var value; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { value = this.get( i ); if ( callback.call( thisArg, value, i, this ) ) { return value; } } return void 0; }); /** * Returns the index of the first element that satisfies the provided testing function. * * @name findIndex * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - function to execute on each value * @param {*} [thisArg] - value to use as `this` when executing `callback` * @throws {TypeError} must provide a function as callback * @returns {integer} index of found element, or -1 if none found */ setReadOnly( Float16Array.prototype, 'findIndex', function findIndexfunction( callback, thisArg ) { var i; var value; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { value = this.get( i ); if ( callback.call( thisArg, value, i, this ) ) { return i; } } return -1; }); /** * Execute a function for each element of the array. * * @name forEach * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - function to invoke * @param {*} [thisArg] - context for callback */ setReadOnly( Float16Array.prototype, 'forEach', function forEachfunction( callback, thisArg ) { var i; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { callback.call( thisArg, this.get( i ), i, this ); } }); /** * Create a new Float16Array from an array-like or iterable object. * * @name from * @memberof Float16Array * @type {Function} * @param {*} source - source to convert * @param {Function} [mapFn] - optional mapping function * @param {*} [thisArg] - execution context for mapFn * @returns {Float16Array} new instance * @throws {TypeError} if source is invalid or mapFn is not a function */ setReadOnly( Float16Array, 'from', function from( source, mapFn, thisArg ) { if ( source === null ) { throw new TypeError( format( 'invalid argument. Source cannot be null or undefined. Value: %s.', source ) ); } if ( arguments.length > 1 && !isFunction( mapFn ) ) { throw new TypeError( format( 'invalid argument. When provided, mapFn must be a function. Value: %s.', mapFn ) ); } var values = []; var i = 0; if ( isCollection( source ) ) { for ( i = 0; i < source.length; i++ ) { values.push( source[ i ] ); } } else if ( isObject( source ) && isFunction( source[ Symbol.iterator ] ) ) { var iterator = source[ Symbol.iterator ](); while ( true ) { var next = iterator.next(); if ( next.done ) { break; } values.push( next.value ); } } else { throw new TypeError( format( 'invalid argument. Source must be iterable or array-like. Value: %s.', source ) ); } if ( mapFn ) { for ( i = 0; i < values.length; i++ ) { values[ i ] = mapFn.call( thisArg, values[ i ], i ); } } var result = new Float16Array( values.length ); for ( i = 0; i < values.length; i++ ) { result.set( i, values[ i ] ); } return result; }); /** * Converts a float16 binary representation to float32. * * @name fromFloat16 * @memberof Float16Array * @type {Function} * @param {integer} bits - Float16 bit pattern * @throws {TypeError} must provide a 16-bit unsigned integer * @returns {number} Float32 value */ setReadOnly( Float16Array, 'fromFloat16', function fromFloat16( bits ) { var sign; var exponent; var fraction; var intView; var floatView; var result; var exp32; var frac32; if ( !isNonNegativeInteger( bits ) || bits > 0xFFFF ) { throw new TypeError( format( 'invalid argument. Must provide a 16-bit unsigned integer. Value: `%s`.', bits ) ); } sign = ( bits & 0x8000 ) << 16; exponent = ( bits & 0x7C00 ) >> 10; fraction = bits & 0x03FF; intView = new Uint32Array( 1 ); if ( exponent === 0 ) { if ( fraction === 0 ) { result = sign; } else { exponent = 1; while ( ( fraction & 0x0400 ) === 0 ) { fraction <<= 1; exponent -= 1; } fraction &= ~0x0400; exp32 = 127 - 15 - exponent; frac32 = fraction << 13; result = sign | ( exp32 << 23 ) | frac32; } } else if ( exponent === 0x1F ) { result = sign | 0x7F800000 | ( fraction << 13 ); } else { exp32 = exponent - 15 + 127; result = sign | ( exp32 << 23 ) | ( fraction << 13 ); } intView[ 0 ] = result; floatView = new Float32Array( intView.buffer ); return floatView[ 0 ]; }); /** * Get a value at a given index. * * @name get * @memberof Float16Array.prototype * @type {Function} * @param {NonNegativeInteger} index - array index * @throws {RangeError} if index is out of bounds * @returns {number} Float32 value at index */ setReadOnly( Float16Array.prototype, 'get', function getfunction( index ) { if ( !isNonNegativeInteger( index ) || index >= this.length ) { throw new RangeError( format( 'invalid argument. Index out of bounds. Value: `%s`.', index ) ); } return Float16Array.fromFloat16( this._buffer[ index ] ); }); /** * Determines whether the array includes a certain value among its entries. * * @name includes * @memberof Float16Array.prototype * @type {Function} * @param {*} searchElement - element to search for * @param {integer} [fromIndex=0] - index to start the search at * @throws {TypeError} if fromIndex is not an integer * @returns {boolean} true if the value is found, false otherwise */ setReadOnly( Float16Array.prototype, 'includes', function includes( searchElement, fromIndex ) { var i; var start; if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } start = ( arguments.length < 2 ) ? 0 : fromIndex; if ( start < 0 ) { start = max( this.length + start, 0 ); } for ( i = start; i < this.length; i++ ) { if ( this.get( i ) === searchElement ) { return true; } } return false; }); /** * Returns the first index at which a given element can be found in the array, or -1 if it is not present. * * @name indexOf * @memberof Float16Array.prototype * @type {Function} * @param {*} searchElement - element to locate * @param {integer} [fromIndex=0] - index to start the search at * @throws {TypeError} if fromIndex is not an integer * @returns {integer} index of the element, or -1 if not found */ setReadOnly( Float16Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { var i; var start; if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } start = ( arguments.length < 2 ) ? 0 : fromIndex; if ( start < 0 ) { start = max( this.length + start, 0 ); } for ( i = start; i < this.length; i++ ) { if ( this.get( i ) === searchElement ) { return i; } } return -1; }); /** * Joins all elements of the Float16Array into a string, separated by a specified separator. * * @name join * @memberof Float16Array.prototype * @type {Function} * @param {string} [separator=','] - separator to insert between values * @returns {string} joined string */ setReadOnly( Float16Array.prototype, 'join', function join( separator ) { var result = ''; var i; separator = ( arguments.length < 0 ) ? ',' : String( separator ); for ( i = 0; i < this.length; i++ ) { if ( i > 0 ) { result += separator; } result += String( this.get( i ) ); } return result; }); /** * Return an iterator over indices. * * @name keys * @memberof Float16Array.prototype * @type {Function} * @returns {Object} Iterator */ setReadOnly( Float16Array.prototype, 'keys', function keys() { var self = this; var i = 0; function nextKeys() { if ( i < self.length ) { var v = i; i += 1; return { value: v, done: false }; } return { value: void 0, done: true }; } function getKeysIterator() { return iterator; } var iterator = { next: nextKeys }; if ( iteratorSymbol ) { iterator[ iteratorSymbol ] = getKeysIterator; } return iterator; }); /** * Returns the last index at which a given element can be found in the array, or -1 if it is not present. * * @name lastIndexOf * @memberof Float16Array.prototype * @type {Function} * @param {*} searchElement - element to locate * @param {integer} [fromIndex=this.length-1] - index to start the search backwards from * @throws {TypeError} if fromIndex is not an integer * @returns {integer} last index of the element, or -1 if not found */ setReadOnly( Float16Array.prototype, 'lastIndexOf', function lastIndexOffunction( searchElement, fromIndex ) { var i; if ( arguments.length > 1 && !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. FromIndex must be an integer. Value: `%s`.', fromIndex ) ); } i = ( arguments.length > 1 ) ? fromIndex : this.length - 1; if ( i >= this.length ) { i = this.length - 1; } if ( i < 0 ) { i = this.length + i; } for ( ; i >= 0; i-- ) { if ( Object.is( this.get( i ), searchElement ) ) { return i; } } return -1; }); /** * Apply a function to each element and return a new Float16Array. * * @name map * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - mapping function * @param {*} [thisArg] - context for callback * @returns {Float16Array} mapped array */ setReadOnly( Float16Array.prototype, 'map', function mapfunction( callback, thisArg ) { var result = new Float16Array( this.length ); var i; var val; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { val = callback.call( thisArg, this.get( i ), i, this ); result.set( i, val ); } return result; }); /** * Create a new Float16Array from a variable number of arguments. * * @name of * @memberof Float16Array * @type {Function} * @returns {Float16Array} new instance */ setReadOnly( Float16Array, 'of', function of() { var len = arguments.length; var result = new Float16Array( len ); var i; for ( i = 0; i < len; i++ ) { result.set( i, arguments[ i ] ); } return result; }); /** * Reduce the array to a single value (left-to-right). * * @name reduce * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - reducer function * @param {*} [initialValue] - initial value * @returns {*} accumulated result */ setReadOnly( Float16Array.prototype, 'reduce', function reduce( callback, initialValue ) { var i; var acc; var len; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } len = this.length; if ( arguments.length >= 2 ) { acc = initialValue; i = 0; } else { if ( len === 0 ) { throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); } acc = this.get( 0 ); i = 1; } for ( ; i < len; i++ ) { acc = callback( acc, this.get( i ), i, this ); } return acc; }); /** * Reduce the array to a single value (right-to-left). * * @name reduceRight * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - reducer function * @param {*} [initialValue] - initial value * @returns {*} accumulated result */ setReadOnly( Float16Array.prototype, 'reduceRight', function reduceRight( callback, initialValue ) { var i; var acc; var len; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } len = this.length; if ( arguments.length >= 2 ) { acc = initialValue; i = len - 1; } else { if ( len === 0 ) { throw new TypeError( format( 'invalid operation. Reduce of empty array with no initial value.' ) ); } acc = this.get( len - 1 ); i = len - 2; } for ( ; i >= 0; i-- ) { acc = callback( acc, this.get( i ), i, this ); } return acc; }); /** * Reverse the array in place. * * @name reverse * @memberof Float16Array.prototype * @type {Function} * @returns {Float16Array} modified array */ setReadOnly( Float16Array.prototype, 'reverse', function reverse() { var i; var j; var tmp; var mid = floor( this.length / 2 ); for ( i = 0; i < mid; i++ ) { j = this.length - 1 - i; tmp = this._buffer[ i ]; this._buffer[ i ] = this._buffer[ j ]; this._buffer[ j ] = tmp; } return this; }); /** * Set a value at a given index, or copy values from an array-like source. * * @name set * @memberof Float16Array.prototype * @type {Function} * @param {(NonNegativeInteger|ArrayLike)} indexOrArray - index or source array * @param {*} [value] - value to set or offset if copying * @throws {RangeError} index or offset out of bounds * @throws {TypeError} invalid arguments */ setReadOnly( Float16Array.prototype, 'set', function set( indexOrArray, value ) { var i; var len; var offset; var srcVal; if ( isNonNegativeInteger( indexOrArray ) ) { if ( indexOrArray >= this.length ) { throw new RangeError( format( 'invalid argument. Index must be a nonnegative integer less than the array length. Value: `%s`.', indexOrArray ) ); } this._buffer[ indexOrArray ] = Float16Array.toFloat16( value ); return; } offset = ( arguments.length > 1 ) ? Number( value ) : 0; if ( !isNonNegativeInteger( offset ) || offset > this.length ) { throw new RangeError( format( 'invalid argument. Offset must be a nonnegative integer less than or equal to the array length. Value: `%s`.', offset ) ); } if ( isArray( indexOrArray ) || isCollection( indexOrArray ) ) { len = min( indexOrArray.length, this.length - offset ); for ( i = 0; i < len; i++ ) { srcVal = isFunction( indexOrArray.get ) ? indexOrArray.get( i ) : indexOrArray[ i ]; this._buffer[ offset + i ] = Float16Array.toFloat16( srcVal ); } return; } throw new TypeError( format( 'invalid argument. Must provide index and value or source and offset. Value: `%s`.', indexOrArray ) ); }); /** * Returns a shallow copy of a portion of the array. * * @name slice * @memberof Float16Array.prototype * @type {Function} * @param {integer} [start=0] - start index * @param {integer} [end=array.length] - end index (exclusive) * @throws {TypeError} if start or end is not an integer * @returns {Float16Array} new subarray */ setReadOnly( Float16Array.prototype, 'slice', function slice( start, end ) { var len = this.length; var i; var count; var relativeStart; var relativeEnd; var result; if ( arguments.length > 0 && !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Start index must be an integer. Value: `%s`.', start ) ); } if ( arguments.length > 1 && !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. End index must be an integer. Value: `%s`.', end ) ); } relativeStart = ( arguments.length < 1 ) ? 0 : ( start < 0 ? max( len + start, 0 ) : min( start, len ) ); relativeEnd = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); count = max( relativeEnd - relativeStart, 0 ); result = new Float16Array( count ); for ( i = 0; i < count; i++ ) { result.set( i, this.get( relativeStart + i ) ); } return result; }); /** * Test whether at least one element passes the test. * * @name some * @memberof Float16Array.prototype * @type {Function} * @param {Function} callback - predicate function * @param {*} [thisArg] - context * @returns {boolean} whether at least one passes */ setReadOnly( Float16Array.prototype, 'some', function some( callback, thisArg ) { var i; var value; if ( !isFunction( callback ) ) { throw new TypeError( format( 'invalid argument. Callback must be a function. Value: `%s`.', callback ) ); } for ( i = 0; i < this.length; i++ ) { value = this.get( i ); if ( callback.call( thisArg, value, i, this ) ) { return true; } } return false; }); /** * Sort the array in place. * * @name sort * @memberof Float16Array.prototype * @type {Function} * @param {Function} [compareFn] - optional comparison function * @throws {TypeError} if compareFn is not a function * @returns {Float16Array} sorted array */ setReadOnly( Float16Array.prototype, 'sort', function sort( compareFn ) { var values = []; var i; var j; if ( arguments.length > 0 && !isFunction( compareFn ) ) { throw new TypeError( format( 'invalid argument. Comparison function must be a function. Value: `%s`.', compareFn ) ); } function getComparator( compareFn ) { return function compareFloat16( a, b ) { if ( compareFn ) { return compareFn( a, b ); } if ( a !== a ) { return 1; } if ( b !== b ) { return -1; } return a - b; }; } for ( i = 0; i < this.length; i++ ) { values.push( this.get( i ) ); } values.sort( getComparator( compareFn ) ); for ( j = 0; j < this.length; j++ ) { this.set( j, values[ j ] ); } return this; }); /** * Returns a new Float16Array view on the same buffer, for the specified range. * * @name subarray * @memberof Float16Array.prototype * @type {Function} * @param {integer} [begin=0] - start index * @param {integer} [end=this.length] - end index * @returns {Float16Array} new Float16Array */ setReadOnly( Float16Array.prototype, 'subarray', function subarray( begin, end ) { var len = this.length; var start = ( arguments.length < 1 ) ? 0 : ( begin < 0 ? max( len + begin, 0 ) : min( begin, len ) ); var finish = ( arguments.length < 2 ) ? len : ( end < 0 ? max( len + end, 0 ) : min( end, len ) ); var count = max( finish - start, 0 ); var result = new Float16Array( count ); for ( var i = 0; i < count; i++ ) { result.set( i, this.get( start + i ) ); } return result; }); /** * Converts a float32 number to a float16 (binary) representation. * * @name toFloat16 * @memberof Float16Array * @type {Function} * @param {*} val - Float32 value (coerced if not number) * @returns {integer} Float16 bit pattern */ setReadOnly( Float16Array, 'toFloat16', function toFloat16( val ) { var num = Number( val ); var floatView = new Float32Array( 1 ); var intView = new Uint32Array( floatView.buffer ); var sign; var exponent; var fraction; var exp; var frac; var x; floatView[ 0 ] = num; x = intView[ 0 ]; sign = ( x >> 16 ) & 0x8000; exponent = ( ( x >> 23 ) & 0xff ) - 127; fraction = x & 0x7fffff; if ( exponent <= -15 ) { if ( exponent < -24 ) { return sign; } return sign | ( ( ( fraction | 0x800000 ) >> ( -exponent - 1 ) ) >> 13 ); } if ( exponent >= 16 ) { return sign | 0x7c00; } exp = exponent + 15; frac = fraction >> 13; return sign | ( exp << 10 ) | frac; }); /** * Return an iterator over values. * * @name values * @memberof Float16Array.prototype * @type {Function} * @returns {Object} Iterator */ setReadOnly( Float16Array.prototype, 'values', function values() { var self = this; var i = 0; var iterator = { next: function nextValues() { if ( i < self.length ) { var v = self.get( i ); i += 1; return { value: v, done: false }; } return { value: void 0, done: true }; } }; if ( iteratorSymbol ) { iterator[ iteratorSymbol ] = function getValuesIterator() { return iterator; }; } return iterator; }); // EXPORTS // module.exports = Float16Array; |