float16.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*! @petamoriken/float16 v3.8.0 | MIT License - https://github.com/petamoriken/float16 */
  2. var float16 = (function (exports) {
  3. 'use strict';
  4. const THIS_IS_NOT_AN_OBJECT = "This is not an object";
  5. const THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT = "This is not a Float16Array object";
  6. const THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY =
  7. "This constructor is not a subclass of Float16Array";
  8. const THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT =
  9. "The constructor property value is not an object";
  10. const SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT =
  11. "Species constructor didn't return TypedArray object";
  12. const DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH =
  13. "Derived constructor created TypedArray object which was too small length";
  14. const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER =
  15. "Attempting to access detached ArrayBuffer";
  16. const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT =
  17. "Cannot convert undefined or null to object";
  18. const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
  19. "Cannot mix BigInt and other types, use explicit conversions";
  20. const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
  21. const REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE =
  22. "Reduce of empty array with no initial value";
  23. const THE_COMPARISON_FUNCTION_MUST_BE_EITHER_A_FUNCTION_OR_UNDEFINED =
  24. "The comparison function must be either a function or undefined";
  25. const OFFSET_IS_OUT_OF_BOUNDS = "Offset is out of bounds";
  26. function uncurryThis(target) {
  27. return (thisArg, ...args) => {
  28. return ReflectApply(target, thisArg, args);
  29. };
  30. }
  31. function uncurryThisGetter(target, key) {
  32. return uncurryThis(
  33. ReflectGetOwnPropertyDescriptor(
  34. target,
  35. key
  36. ).get
  37. );
  38. }
  39. const {
  40. apply: ReflectApply,
  41. construct: ReflectConstruct,
  42. defineProperty: ReflectDefineProperty,
  43. get: ReflectGet,
  44. getOwnPropertyDescriptor: ReflectGetOwnPropertyDescriptor,
  45. getPrototypeOf: ReflectGetPrototypeOf,
  46. has: ReflectHas,
  47. ownKeys: ReflectOwnKeys,
  48. set: ReflectSet,
  49. setPrototypeOf: ReflectSetPrototypeOf,
  50. } = Reflect;
  51. const NativeProxy = Proxy;
  52. const {
  53. MAX_SAFE_INTEGER,
  54. isFinite: NumberIsFinite,
  55. isNaN: NumberIsNaN,
  56. } = Number;
  57. const {
  58. iterator: SymbolIterator,
  59. species: SymbolSpecies,
  60. toStringTag: SymbolToStringTag,
  61. for: SymbolFor,
  62. } = Symbol;
  63. const NativeObject = Object;
  64. const {
  65. create: ObjectCreate,
  66. defineProperty: ObjectDefineProperty,
  67. freeze: ObjectFreeze,
  68. is: ObjectIs,
  69. } = NativeObject;
  70. const ObjectPrototype = NativeObject.prototype;
  71. const ObjectPrototype__lookupGetter__ = (ObjectPrototype).__lookupGetter__
  72. ? uncurryThis( (ObjectPrototype).__lookupGetter__)
  73. : (object, key) => {
  74. if (object == null) {
  75. throw NativeTypeError(
  76. CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT
  77. );
  78. }
  79. let target = NativeObject(object);
  80. do {
  81. const descriptor = ReflectGetOwnPropertyDescriptor(target, key);
  82. if (descriptor !== undefined) {
  83. if (ObjectHasOwn(descriptor, "get")) {
  84. return descriptor.get;
  85. }
  86. return;
  87. }
  88. } while ((target = ReflectGetPrototypeOf(target)) !== null);
  89. };
  90. const ObjectHasOwn = (NativeObject).hasOwn ||
  91. uncurryThis(ObjectPrototype.hasOwnProperty);
  92. const NativeArray = Array;
  93. const ArrayIsArray = NativeArray.isArray;
  94. const ArrayPrototype = NativeArray.prototype;
  95. const ArrayPrototypeJoin = uncurryThis(ArrayPrototype.join);
  96. const ArrayPrototypePush = uncurryThis(ArrayPrototype.push);
  97. const ArrayPrototypeToLocaleString = uncurryThis(
  98. ArrayPrototype.toLocaleString
  99. );
  100. const NativeArrayPrototypeSymbolIterator = ArrayPrototype[SymbolIterator];
  101. const ArrayPrototypeSymbolIterator = uncurryThis(NativeArrayPrototypeSymbolIterator);
  102. const MathTrunc = Math.trunc;
  103. const NativeArrayBuffer = ArrayBuffer;
  104. const ArrayBufferIsView = NativeArrayBuffer.isView;
  105. const ArrayBufferPrototype = NativeArrayBuffer.prototype;
  106. const ArrayBufferPrototypeSlice = uncurryThis(ArrayBufferPrototype.slice);
  107. const ArrayBufferPrototypeGetByteLength = uncurryThisGetter(ArrayBufferPrototype, "byteLength");
  108. const NativeSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer : null;
  109. const SharedArrayBufferPrototypeGetByteLength = NativeSharedArrayBuffer
  110. && uncurryThisGetter(NativeSharedArrayBuffer.prototype, "byteLength");
  111. const TypedArray = ReflectGetPrototypeOf(Uint8Array);
  112. const TypedArrayFrom = TypedArray.from;
  113. const TypedArrayPrototype = TypedArray.prototype;
  114. const NativeTypedArrayPrototypeSymbolIterator = TypedArrayPrototype[SymbolIterator];
  115. const TypedArrayPrototypeKeys = uncurryThis(TypedArrayPrototype.keys);
  116. const TypedArrayPrototypeValues = uncurryThis(
  117. TypedArrayPrototype.values
  118. );
  119. const TypedArrayPrototypeEntries = uncurryThis(
  120. TypedArrayPrototype.entries
  121. );
  122. const TypedArrayPrototypeSet = uncurryThis(TypedArrayPrototype.set);
  123. const TypedArrayPrototypeReverse = uncurryThis(
  124. TypedArrayPrototype.reverse
  125. );
  126. const TypedArrayPrototypeFill = uncurryThis(TypedArrayPrototype.fill);
  127. const TypedArrayPrototypeCopyWithin = uncurryThis(
  128. TypedArrayPrototype.copyWithin
  129. );
  130. const TypedArrayPrototypeSort = uncurryThis(TypedArrayPrototype.sort);
  131. const TypedArrayPrototypeSlice = uncurryThis(TypedArrayPrototype.slice);
  132. const TypedArrayPrototypeSubarray = uncurryThis(
  133. TypedArrayPrototype.subarray
  134. );
  135. const TypedArrayPrototypeGetBuffer = uncurryThisGetter(
  136. TypedArrayPrototype,
  137. "buffer"
  138. );
  139. const TypedArrayPrototypeGetByteOffset = uncurryThisGetter(
  140. TypedArrayPrototype,
  141. "byteOffset"
  142. );
  143. const TypedArrayPrototypeGetLength = uncurryThisGetter(
  144. TypedArrayPrototype,
  145. "length"
  146. );
  147. const TypedArrayPrototypeGetSymbolToStringTag = uncurryThisGetter(
  148. TypedArrayPrototype,
  149. SymbolToStringTag
  150. );
  151. const NativeUint16Array = Uint16Array;
  152. const Uint16ArrayFrom = (...args) => {
  153. return ReflectApply(TypedArrayFrom, NativeUint16Array, args);
  154. };
  155. const NativeUint32Array = Uint32Array;
  156. const NativeFloat32Array = Float32Array;
  157. const ArrayIteratorPrototype = ReflectGetPrototypeOf([][SymbolIterator]());
  158. const ArrayIteratorPrototypeNext = uncurryThis(ArrayIteratorPrototype.next);
  159. const GeneratorPrototypeNext = uncurryThis((function* () {})().next);
  160. const IteratorPrototype = ReflectGetPrototypeOf(ArrayIteratorPrototype);
  161. const DataViewPrototype = DataView.prototype;
  162. const DataViewPrototypeGetUint16 = uncurryThis(
  163. DataViewPrototype.getUint16
  164. );
  165. const DataViewPrototypeSetUint16 = uncurryThis(
  166. DataViewPrototype.setUint16
  167. );
  168. const NativeTypeError = TypeError;
  169. const NativeRangeError = RangeError;
  170. const NativeWeakSet = WeakSet;
  171. const WeakSetPrototype = NativeWeakSet.prototype;
  172. const WeakSetPrototypeAdd = uncurryThis(WeakSetPrototype.add);
  173. const WeakSetPrototypeHas = uncurryThis(WeakSetPrototype.has);
  174. const NativeWeakMap = WeakMap;
  175. const WeakMapPrototype = NativeWeakMap.prototype;
  176. const WeakMapPrototypeGet = uncurryThis(WeakMapPrototype.get);
  177. const WeakMapPrototypeHas = uncurryThis(WeakMapPrototype.has);
  178. const WeakMapPrototypeSet = uncurryThis(WeakMapPrototype.set);
  179. const arrayIterators = new NativeWeakMap();
  180. const SafeIteratorPrototype = ObjectCreate(null, {
  181. next: {
  182. value: function next() {
  183. const arrayIterator = WeakMapPrototypeGet(arrayIterators, this);
  184. return ArrayIteratorPrototypeNext(arrayIterator);
  185. },
  186. },
  187. [SymbolIterator]: {
  188. value: function values() {
  189. return this;
  190. },
  191. },
  192. });
  193. function safeIfNeeded(array) {
  194. if (
  195. array[SymbolIterator] === NativeArrayPrototypeSymbolIterator &&
  196. ArrayIteratorPrototype.next === ArrayIteratorPrototypeNext
  197. ) {
  198. return array;
  199. }
  200. const safe = ObjectCreate(SafeIteratorPrototype);
  201. WeakMapPrototypeSet(arrayIterators, safe, ArrayPrototypeSymbolIterator(array));
  202. return safe;
  203. }
  204. const generators = new NativeWeakMap();
  205. const DummyArrayIteratorPrototype = ObjectCreate(IteratorPrototype, {
  206. next: {
  207. value: function next() {
  208. const generator = WeakMapPrototypeGet(generators, this);
  209. return GeneratorPrototypeNext(generator);
  210. },
  211. writable: true,
  212. configurable: true,
  213. },
  214. });
  215. for (const key of ReflectOwnKeys(ArrayIteratorPrototype)) {
  216. if (key === "next") {
  217. continue;
  218. }
  219. ObjectDefineProperty(DummyArrayIteratorPrototype, key, ReflectGetOwnPropertyDescriptor(ArrayIteratorPrototype, key));
  220. }
  221. function wrap(generator) {
  222. const dummy = ObjectCreate(DummyArrayIteratorPrototype);
  223. WeakMapPrototypeSet(generators, dummy, generator);
  224. return dummy;
  225. }
  226. function isObject(value) {
  227. return (
  228. (value !== null && typeof value === "object") ||
  229. typeof value === "function"
  230. );
  231. }
  232. function isObjectLike(value) {
  233. return value !== null && typeof value === "object";
  234. }
  235. function isNativeTypedArray(value) {
  236. return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined;
  237. }
  238. function isNativeBigIntTypedArray(value) {
  239. const typedArrayName = TypedArrayPrototypeGetSymbolToStringTag(value);
  240. return (
  241. typedArrayName === "BigInt64Array" ||
  242. typedArrayName === "BigUint64Array"
  243. );
  244. }
  245. function isArrayBuffer(value) {
  246. try {
  247. ArrayBufferPrototypeGetByteLength( (value));
  248. return true;
  249. } catch (e) {
  250. return false;
  251. }
  252. }
  253. function isSharedArrayBuffer(value) {
  254. if (NativeSharedArrayBuffer === null) {
  255. return false;
  256. }
  257. try {
  258. SharedArrayBufferPrototypeGetByteLength( (value));
  259. return true;
  260. } catch (e) {
  261. return false;
  262. }
  263. }
  264. function isOrdinaryArray(value) {
  265. if (!ArrayIsArray(value)) {
  266. return false;
  267. }
  268. return (
  269. value[SymbolIterator] === NativeArrayPrototypeSymbolIterator &&
  270. ArrayIteratorPrototype.next === ArrayIteratorPrototypeNext
  271. );
  272. }
  273. function isOrdinaryNativeTypedArray(value) {
  274. if (!isNativeTypedArray(value)) {
  275. return false;
  276. }
  277. return (
  278. value[SymbolIterator] === NativeTypedArrayPrototypeSymbolIterator &&
  279. ArrayIteratorPrototype.next === ArrayIteratorPrototypeNext
  280. );
  281. }
  282. function isCanonicalIntegerIndexString(value) {
  283. if (typeof value !== "string") {
  284. return false;
  285. }
  286. const number = +value;
  287. if (value !== number + "") {
  288. return false;
  289. }
  290. if (!NumberIsFinite(number)) {
  291. return false;
  292. }
  293. return number === MathTrunc(number);
  294. }
  295. const brand = SymbolFor("__Float16Array__");
  296. function hasFloat16ArrayBrand(target) {
  297. if (!isObjectLike(target)) {
  298. return false;
  299. }
  300. const prototype = ReflectGetPrototypeOf(target);
  301. if (!isObjectLike(prototype)) {
  302. return false;
  303. }
  304. const constructor = prototype.constructor;
  305. if (constructor === undefined) {
  306. return false;
  307. }
  308. if (!isObject(constructor)) {
  309. throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
  310. }
  311. return ReflectHas(constructor, brand);
  312. }
  313. const buffer = new NativeArrayBuffer(4);
  314. const floatView = new NativeFloat32Array(buffer);
  315. const uint32View = new NativeUint32Array(buffer);
  316. const baseTable = new NativeUint32Array(512);
  317. const shiftTable = new NativeUint32Array(512);
  318. for (let i = 0; i < 256; ++i) {
  319. const e = i - 127;
  320. if (e < -27) {
  321. baseTable[i] = 0x0000;
  322. baseTable[i | 0x100] = 0x8000;
  323. shiftTable[i] = 24;
  324. shiftTable[i | 0x100] = 24;
  325. } else if (e < -14) {
  326. baseTable[i] = 0x0400 >> (-e - 14);
  327. baseTable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000;
  328. shiftTable[i] = -e - 1;
  329. shiftTable[i | 0x100] = -e - 1;
  330. } else if (e <= 15) {
  331. baseTable[i] = (e + 15) << 10;
  332. baseTable[i | 0x100] = ((e + 15) << 10) | 0x8000;
  333. shiftTable[i] = 13;
  334. shiftTable[i | 0x100] = 13;
  335. } else if (e < 128) {
  336. baseTable[i] = 0x7c00;
  337. baseTable[i | 0x100] = 0xfc00;
  338. shiftTable[i] = 24;
  339. shiftTable[i | 0x100] = 24;
  340. } else {
  341. baseTable[i] = 0x7c00;
  342. baseTable[i | 0x100] = 0xfc00;
  343. shiftTable[i] = 13;
  344. shiftTable[i | 0x100] = 13;
  345. }
  346. }
  347. function roundToFloat16Bits(num) {
  348. floatView[0] = (num);
  349. const f = uint32View[0];
  350. const e = (f >> 23) & 0x1ff;
  351. return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]);
  352. }
  353. const mantissaTable = new NativeUint32Array(2048);
  354. const exponentTable = new NativeUint32Array(64);
  355. const offsetTable = new NativeUint32Array(64);
  356. for (let i = 1; i < 1024; ++i) {
  357. let m = i << 13;
  358. let e = 0;
  359. while((m & 0x00800000) === 0) {
  360. m <<= 1;
  361. e -= 0x00800000;
  362. }
  363. m &= ~0x00800000;
  364. e += 0x38800000;
  365. mantissaTable[i] = m | e;
  366. }
  367. for (let i = 1024; i < 2048; ++i) {
  368. mantissaTable[i] = 0x38000000 + ((i - 1024) << 13);
  369. }
  370. for (let i = 1; i < 31; ++i) {
  371. exponentTable[i] = i << 23;
  372. }
  373. exponentTable[31] = 0x47800000;
  374. exponentTable[32] = 0x80000000;
  375. for (let i = 33; i < 63; ++i) {
  376. exponentTable[i] = 0x80000000 + ((i - 32) << 23);
  377. }
  378. exponentTable[63] = 0xc7800000;
  379. for (let i = 1; i < 64; ++i) {
  380. if (i !== 32) {
  381. offsetTable[i] = 1024;
  382. }
  383. }
  384. function convertToNumber(float16bits) {
  385. const m = float16bits >> 10;
  386. uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m];
  387. return floatView[0];
  388. }
  389. function ToIntegerOrInfinity(target) {
  390. const number = +target;
  391. if (NumberIsNaN(number) || number === 0) {
  392. return 0;
  393. }
  394. return MathTrunc(number);
  395. }
  396. function ToLength(target) {
  397. const length = ToIntegerOrInfinity(target);
  398. if (length < 0) {
  399. return 0;
  400. }
  401. return length < MAX_SAFE_INTEGER
  402. ? length
  403. : MAX_SAFE_INTEGER;
  404. }
  405. function SpeciesConstructor(target, defaultConstructor) {
  406. if (!isObject(target)) {
  407. throw NativeTypeError(THIS_IS_NOT_AN_OBJECT);
  408. }
  409. const constructor = target.constructor;
  410. if (constructor === undefined) {
  411. return defaultConstructor;
  412. }
  413. if (!isObject(constructor)) {
  414. throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
  415. }
  416. const species = constructor[SymbolSpecies];
  417. if (species == null) {
  418. return defaultConstructor;
  419. }
  420. return species;
  421. }
  422. function IsDetachedBuffer(buffer) {
  423. if (isSharedArrayBuffer(buffer)) {
  424. return false;
  425. }
  426. try {
  427. ArrayBufferPrototypeSlice(buffer, 0, 0);
  428. return false;
  429. } catch (e) {}
  430. return true;
  431. }
  432. function defaultCompare(x, y) {
  433. const isXNaN = NumberIsNaN(x);
  434. const isYNaN = NumberIsNaN(y);
  435. if (isXNaN && isYNaN) {
  436. return 0;
  437. }
  438. if (isXNaN) {
  439. return 1;
  440. }
  441. if (isYNaN) {
  442. return -1;
  443. }
  444. if (x < y) {
  445. return -1;
  446. }
  447. if (x > y) {
  448. return 1;
  449. }
  450. if (x === 0 && y === 0) {
  451. const isXPlusZero = ObjectIs(x, 0);
  452. const isYPlusZero = ObjectIs(y, 0);
  453. if (!isXPlusZero && isYPlusZero) {
  454. return -1;
  455. }
  456. if (isXPlusZero && !isYPlusZero) {
  457. return 1;
  458. }
  459. }
  460. return 0;
  461. }
  462. const BYTES_PER_ELEMENT = 2;
  463. const float16bitsArrays = new NativeWeakMap();
  464. function isFloat16Array(target) {
  465. return WeakMapPrototypeHas(float16bitsArrays, target) ||
  466. (!ArrayBufferIsView(target) && hasFloat16ArrayBrand(target));
  467. }
  468. function assertFloat16Array(target) {
  469. if (!isFloat16Array(target)) {
  470. throw NativeTypeError(THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT);
  471. }
  472. }
  473. function assertSpeciesTypedArray(target, count) {
  474. const isTargetFloat16Array = isFloat16Array(target);
  475. const isTargetTypedArray = isNativeTypedArray(target);
  476. if (!isTargetFloat16Array && !isTargetTypedArray) {
  477. throw NativeTypeError(SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT);
  478. }
  479. if (typeof count === "number") {
  480. let length;
  481. if (isTargetFloat16Array) {
  482. const float16bitsArray = getFloat16BitsArray(target);
  483. length = TypedArrayPrototypeGetLength(float16bitsArray);
  484. } else {
  485. length = TypedArrayPrototypeGetLength(target);
  486. }
  487. if (length < count) {
  488. throw NativeTypeError(
  489. DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH
  490. );
  491. }
  492. }
  493. if (isNativeBigIntTypedArray(target)) {
  494. throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES);
  495. }
  496. }
  497. function getFloat16BitsArray(float16) {
  498. const float16bitsArray = WeakMapPrototypeGet(float16bitsArrays, float16);
  499. if (float16bitsArray !== undefined) {
  500. const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray);
  501. if (IsDetachedBuffer(buffer)) {
  502. throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
  503. }
  504. return float16bitsArray;
  505. }
  506. const buffer = (float16).buffer;
  507. if (IsDetachedBuffer(buffer)) {
  508. throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
  509. }
  510. const cloned = ReflectConstruct(Float16Array, [
  511. buffer,
  512. (float16).byteOffset,
  513. (float16).length,
  514. ], float16.constructor);
  515. return WeakMapPrototypeGet(float16bitsArrays, cloned);
  516. }
  517. function copyToArray(float16bitsArray) {
  518. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  519. const array = [];
  520. for (let i = 0; i < length; ++i) {
  521. array[i] = convertToNumber(float16bitsArray[i]);
  522. }
  523. return array;
  524. }
  525. const TypedArrayPrototypeGetters = new NativeWeakSet();
  526. for (const key of ReflectOwnKeys(TypedArrayPrototype)) {
  527. if (key === SymbolToStringTag) {
  528. continue;
  529. }
  530. const descriptor = ReflectGetOwnPropertyDescriptor(TypedArrayPrototype, key);
  531. if (ObjectHasOwn(descriptor, "get") && typeof descriptor.get === "function") {
  532. WeakSetPrototypeAdd(TypedArrayPrototypeGetters, descriptor.get);
  533. }
  534. }
  535. const handler = ObjectFreeze( ({
  536. get(target, key, receiver) {
  537. if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) {
  538. return convertToNumber(ReflectGet(target, key));
  539. }
  540. if (WeakSetPrototypeHas(TypedArrayPrototypeGetters, ObjectPrototype__lookupGetter__(target, key))) {
  541. return ReflectGet(target, key);
  542. }
  543. return ReflectGet(target, key, receiver);
  544. },
  545. set(target, key, value, receiver) {
  546. if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) {
  547. return ReflectSet(target, key, roundToFloat16Bits(value));
  548. }
  549. return ReflectSet(target, key, value, receiver);
  550. },
  551. getOwnPropertyDescriptor(target, key) {
  552. if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) {
  553. const descriptor = ReflectGetOwnPropertyDescriptor(target, key);
  554. descriptor.value = convertToNumber(descriptor.value);
  555. return descriptor;
  556. }
  557. return ReflectGetOwnPropertyDescriptor(target, key);
  558. },
  559. defineProperty(target, key, descriptor) {
  560. if (
  561. isCanonicalIntegerIndexString(key) &&
  562. ObjectHasOwn(target, key) &&
  563. ObjectHasOwn(descriptor, "value")
  564. ) {
  565. descriptor.value = roundToFloat16Bits(descriptor.value);
  566. return ReflectDefineProperty(target, key, descriptor);
  567. }
  568. return ReflectDefineProperty(target, key, descriptor);
  569. },
  570. }));
  571. class Float16Array {
  572. constructor(input, _byteOffset, _length) {
  573. let float16bitsArray;
  574. if (isFloat16Array(input)) {
  575. float16bitsArray = ReflectConstruct(NativeUint16Array, [getFloat16BitsArray(input)], new.target);
  576. } else if (isObject(input) && !isArrayBuffer(input)) {
  577. let list;
  578. let length;
  579. if (isNativeTypedArray(input)) {
  580. list = input;
  581. length = TypedArrayPrototypeGetLength(input);
  582. const buffer = TypedArrayPrototypeGetBuffer(input);
  583. if (IsDetachedBuffer(buffer)) {
  584. throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
  585. }
  586. if (isNativeBigIntTypedArray(input)) {
  587. throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES);
  588. }
  589. const data = new NativeArrayBuffer(
  590. length * BYTES_PER_ELEMENT
  591. );
  592. float16bitsArray = ReflectConstruct(NativeUint16Array, [data], new.target);
  593. } else {
  594. const iterator = input[SymbolIterator];
  595. if (iterator != null && typeof iterator !== "function") {
  596. throw NativeTypeError(ITERATOR_PROPERTY_IS_NOT_CALLABLE);
  597. }
  598. if (iterator != null) {
  599. if (isOrdinaryArray(input)) {
  600. list = input;
  601. length = input.length;
  602. } else {
  603. list = [... (input)];
  604. length = list.length;
  605. }
  606. } else {
  607. list = (input);
  608. length = ToLength(list.length);
  609. }
  610. float16bitsArray = ReflectConstruct(NativeUint16Array, [length], new.target);
  611. }
  612. for (let i = 0; i < length; ++i) {
  613. float16bitsArray[i] = roundToFloat16Bits(list[i]);
  614. }
  615. } else {
  616. float16bitsArray = ReflectConstruct(NativeUint16Array, arguments, new.target);
  617. }
  618. const proxy = (new NativeProxy(float16bitsArray, handler));
  619. WeakMapPrototypeSet(float16bitsArrays, proxy, float16bitsArray);
  620. return proxy;
  621. }
  622. static from(src, ...opts) {
  623. const Constructor = this;
  624. if (!ReflectHas(Constructor, brand)) {
  625. throw NativeTypeError(
  626. THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY
  627. );
  628. }
  629. if (Constructor === Float16Array) {
  630. if (isFloat16Array(src) && opts.length === 0) {
  631. const float16bitsArray = getFloat16BitsArray(src);
  632. const uint16 = new NativeUint16Array(
  633. TypedArrayPrototypeGetBuffer(float16bitsArray),
  634. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  635. TypedArrayPrototypeGetLength(float16bitsArray)
  636. );
  637. return new Float16Array(
  638. TypedArrayPrototypeGetBuffer(TypedArrayPrototypeSlice(uint16))
  639. );
  640. }
  641. if (opts.length === 0) {
  642. return new Float16Array(
  643. TypedArrayPrototypeGetBuffer(
  644. Uint16ArrayFrom(src, roundToFloat16Bits)
  645. )
  646. );
  647. }
  648. const mapFunc = opts[0];
  649. const thisArg = opts[1];
  650. return new Float16Array(
  651. TypedArrayPrototypeGetBuffer(
  652. Uint16ArrayFrom(src, function (val, ...args) {
  653. return roundToFloat16Bits(
  654. ReflectApply(mapFunc, this, [val, ...safeIfNeeded(args)])
  655. );
  656. }, thisArg)
  657. )
  658. );
  659. }
  660. let list;
  661. let length;
  662. const iterator = src[SymbolIterator];
  663. if (iterator != null && typeof iterator !== "function") {
  664. throw NativeTypeError(ITERATOR_PROPERTY_IS_NOT_CALLABLE);
  665. }
  666. if (iterator != null) {
  667. if (isOrdinaryArray(src)) {
  668. list = src;
  669. length = src.length;
  670. } else if (isOrdinaryNativeTypedArray(src)) {
  671. list = src;
  672. length = TypedArrayPrototypeGetLength(src);
  673. } else {
  674. list = [...src];
  675. length = list.length;
  676. }
  677. } else {
  678. if (src == null) {
  679. throw NativeTypeError(
  680. CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT
  681. );
  682. }
  683. list = NativeObject(src);
  684. length = ToLength(list.length);
  685. }
  686. const array = new Constructor(length);
  687. if (opts.length === 0) {
  688. for (let i = 0; i < length; ++i) {
  689. array[i] = (list[i]);
  690. }
  691. } else {
  692. const mapFunc = opts[0];
  693. const thisArg = opts[1];
  694. for (let i = 0; i < length; ++i) {
  695. array[i] = ReflectApply(mapFunc, thisArg, [list[i], i]);
  696. }
  697. }
  698. return array;
  699. }
  700. static of(...items) {
  701. const Constructor = this;
  702. if (!ReflectHas(Constructor, brand)) {
  703. throw NativeTypeError(
  704. THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY
  705. );
  706. }
  707. const length = items.length;
  708. if (Constructor === Float16Array) {
  709. const proxy = new Float16Array(length);
  710. const float16bitsArray = getFloat16BitsArray(proxy);
  711. for (let i = 0; i < length; ++i) {
  712. float16bitsArray[i] = roundToFloat16Bits(items[i]);
  713. }
  714. return proxy;
  715. }
  716. const array = new Constructor(length);
  717. for (let i = 0; i < length; ++i) {
  718. array[i] = items[i];
  719. }
  720. return array;
  721. }
  722. keys() {
  723. assertFloat16Array(this);
  724. const float16bitsArray = getFloat16BitsArray(this);
  725. return TypedArrayPrototypeKeys(float16bitsArray);
  726. }
  727. values() {
  728. assertFloat16Array(this);
  729. const float16bitsArray = getFloat16BitsArray(this);
  730. return wrap((function* () {
  731. for (const val of TypedArrayPrototypeValues(float16bitsArray)) {
  732. yield convertToNumber(val);
  733. }
  734. })());
  735. }
  736. entries() {
  737. assertFloat16Array(this);
  738. const float16bitsArray = getFloat16BitsArray(this);
  739. return wrap((function* () {
  740. for (const [i, val] of TypedArrayPrototypeEntries(float16bitsArray)) {
  741. yield ([i, convertToNumber(val)]);
  742. }
  743. })());
  744. }
  745. at(index) {
  746. assertFloat16Array(this);
  747. const float16bitsArray = getFloat16BitsArray(this);
  748. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  749. const relativeIndex = ToIntegerOrInfinity(index);
  750. const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
  751. if (k < 0 || k >= length) {
  752. return;
  753. }
  754. return convertToNumber(float16bitsArray[k]);
  755. }
  756. with(index, value) {
  757. assertFloat16Array(this);
  758. const float16bitsArray = getFloat16BitsArray(this);
  759. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  760. const relativeIndex = ToIntegerOrInfinity(index);
  761. const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
  762. const number = +value;
  763. if (k < 0 || k >= length) {
  764. throw NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS);
  765. }
  766. const uint16 = new NativeUint16Array(
  767. TypedArrayPrototypeGetBuffer(float16bitsArray),
  768. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  769. TypedArrayPrototypeGetLength(float16bitsArray)
  770. );
  771. const cloned = new Float16Array(
  772. TypedArrayPrototypeGetBuffer(
  773. TypedArrayPrototypeSlice(uint16)
  774. )
  775. );
  776. const array = getFloat16BitsArray(cloned);
  777. array[k] = roundToFloat16Bits(number);
  778. return cloned;
  779. }
  780. map(callback, ...opts) {
  781. assertFloat16Array(this);
  782. const float16bitsArray = getFloat16BitsArray(this);
  783. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  784. const thisArg = opts[0];
  785. const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
  786. if (Constructor === Float16Array) {
  787. const proxy = new Float16Array(length);
  788. const array = getFloat16BitsArray(proxy);
  789. for (let i = 0; i < length; ++i) {
  790. const val = convertToNumber(float16bitsArray[i]);
  791. array[i] = roundToFloat16Bits(
  792. ReflectApply(callback, thisArg, [val, i, this])
  793. );
  794. }
  795. return proxy;
  796. }
  797. const array = new Constructor(length);
  798. assertSpeciesTypedArray(array, length);
  799. for (let i = 0; i < length; ++i) {
  800. const val = convertToNumber(float16bitsArray[i]);
  801. array[i] = ReflectApply(callback, thisArg, [val, i, this]);
  802. }
  803. return (array);
  804. }
  805. filter(callback, ...opts) {
  806. assertFloat16Array(this);
  807. const float16bitsArray = getFloat16BitsArray(this);
  808. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  809. const thisArg = opts[0];
  810. const kept = [];
  811. for (let i = 0; i < length; ++i) {
  812. const val = convertToNumber(float16bitsArray[i]);
  813. if (ReflectApply(callback, thisArg, [val, i, this])) {
  814. ArrayPrototypePush(kept, val);
  815. }
  816. }
  817. const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
  818. const array = new Constructor(kept);
  819. assertSpeciesTypedArray(array);
  820. return (array);
  821. }
  822. reduce(callback, ...opts) {
  823. assertFloat16Array(this);
  824. const float16bitsArray = getFloat16BitsArray(this);
  825. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  826. if (length === 0 && opts.length === 0) {
  827. throw NativeTypeError(REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE);
  828. }
  829. let accumulator, start;
  830. if (opts.length === 0) {
  831. accumulator = convertToNumber(float16bitsArray[0]);
  832. start = 1;
  833. } else {
  834. accumulator = opts[0];
  835. start = 0;
  836. }
  837. for (let i = start; i < length; ++i) {
  838. accumulator = callback(
  839. accumulator,
  840. convertToNumber(float16bitsArray[i]),
  841. i,
  842. this
  843. );
  844. }
  845. return accumulator;
  846. }
  847. reduceRight(callback, ...opts) {
  848. assertFloat16Array(this);
  849. const float16bitsArray = getFloat16BitsArray(this);
  850. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  851. if (length === 0 && opts.length === 0) {
  852. throw NativeTypeError(REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE);
  853. }
  854. let accumulator, start;
  855. if (opts.length === 0) {
  856. accumulator = convertToNumber(float16bitsArray[length - 1]);
  857. start = length - 2;
  858. } else {
  859. accumulator = opts[0];
  860. start = length - 1;
  861. }
  862. for (let i = start; i >= 0; --i) {
  863. accumulator = callback(
  864. accumulator,
  865. convertToNumber(float16bitsArray[i]),
  866. i,
  867. this
  868. );
  869. }
  870. return accumulator;
  871. }
  872. forEach(callback, ...opts) {
  873. assertFloat16Array(this);
  874. const float16bitsArray = getFloat16BitsArray(this);
  875. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  876. const thisArg = opts[0];
  877. for (let i = 0; i < length; ++i) {
  878. ReflectApply(callback, thisArg, [
  879. convertToNumber(float16bitsArray[i]),
  880. i,
  881. this,
  882. ]);
  883. }
  884. }
  885. find(callback, ...opts) {
  886. assertFloat16Array(this);
  887. const float16bitsArray = getFloat16BitsArray(this);
  888. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  889. const thisArg = opts[0];
  890. for (let i = 0; i < length; ++i) {
  891. const value = convertToNumber(float16bitsArray[i]);
  892. if (ReflectApply(callback, thisArg, [value, i, this])) {
  893. return value;
  894. }
  895. }
  896. }
  897. findIndex(callback, ...opts) {
  898. assertFloat16Array(this);
  899. const float16bitsArray = getFloat16BitsArray(this);
  900. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  901. const thisArg = opts[0];
  902. for (let i = 0; i < length; ++i) {
  903. const value = convertToNumber(float16bitsArray[i]);
  904. if (ReflectApply(callback, thisArg, [value, i, this])) {
  905. return i;
  906. }
  907. }
  908. return -1;
  909. }
  910. findLast(callback, ...opts) {
  911. assertFloat16Array(this);
  912. const float16bitsArray = getFloat16BitsArray(this);
  913. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  914. const thisArg = opts[0];
  915. for (let i = length - 1; i >= 0; --i) {
  916. const value = convertToNumber(float16bitsArray[i]);
  917. if (ReflectApply(callback, thisArg, [value, i, this])) {
  918. return value;
  919. }
  920. }
  921. }
  922. findLastIndex(callback, ...opts) {
  923. assertFloat16Array(this);
  924. const float16bitsArray = getFloat16BitsArray(this);
  925. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  926. const thisArg = opts[0];
  927. for (let i = length - 1; i >= 0; --i) {
  928. const value = convertToNumber(float16bitsArray[i]);
  929. if (ReflectApply(callback, thisArg, [value, i, this])) {
  930. return i;
  931. }
  932. }
  933. return -1;
  934. }
  935. every(callback, ...opts) {
  936. assertFloat16Array(this);
  937. const float16bitsArray = getFloat16BitsArray(this);
  938. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  939. const thisArg = opts[0];
  940. for (let i = 0; i < length; ++i) {
  941. if (
  942. !ReflectApply(callback, thisArg, [
  943. convertToNumber(float16bitsArray[i]),
  944. i,
  945. this,
  946. ])
  947. ) {
  948. return false;
  949. }
  950. }
  951. return true;
  952. }
  953. some(callback, ...opts) {
  954. assertFloat16Array(this);
  955. const float16bitsArray = getFloat16BitsArray(this);
  956. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  957. const thisArg = opts[0];
  958. for (let i = 0; i < length; ++i) {
  959. if (
  960. ReflectApply(callback, thisArg, [
  961. convertToNumber(float16bitsArray[i]),
  962. i,
  963. this,
  964. ])
  965. ) {
  966. return true;
  967. }
  968. }
  969. return false;
  970. }
  971. set(input, ...opts) {
  972. assertFloat16Array(this);
  973. const float16bitsArray = getFloat16BitsArray(this);
  974. const targetOffset = ToIntegerOrInfinity(opts[0]);
  975. if (targetOffset < 0) {
  976. throw NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS);
  977. }
  978. if (input == null) {
  979. throw NativeTypeError(
  980. CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT
  981. );
  982. }
  983. if (isNativeBigIntTypedArray(input)) {
  984. throw NativeTypeError(
  985. CANNOT_MIX_BIGINT_AND_OTHER_TYPES
  986. );
  987. }
  988. if (isFloat16Array(input)) {
  989. return TypedArrayPrototypeSet(
  990. getFloat16BitsArray(this),
  991. getFloat16BitsArray(input),
  992. targetOffset
  993. );
  994. }
  995. if (isNativeTypedArray(input)) {
  996. const buffer = TypedArrayPrototypeGetBuffer(input);
  997. if (IsDetachedBuffer(buffer)) {
  998. throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
  999. }
  1000. }
  1001. const targetLength = TypedArrayPrototypeGetLength(float16bitsArray);
  1002. const src = NativeObject(input);
  1003. const srcLength = ToLength(src.length);
  1004. if (targetOffset === Infinity || srcLength + targetOffset > targetLength) {
  1005. throw NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS);
  1006. }
  1007. for (let i = 0; i < srcLength; ++i) {
  1008. float16bitsArray[i + targetOffset] = roundToFloat16Bits(src[i]);
  1009. }
  1010. }
  1011. reverse() {
  1012. assertFloat16Array(this);
  1013. const float16bitsArray = getFloat16BitsArray(this);
  1014. TypedArrayPrototypeReverse(float16bitsArray);
  1015. return this;
  1016. }
  1017. toReversed() {
  1018. assertFloat16Array(this);
  1019. const float16bitsArray = getFloat16BitsArray(this);
  1020. const uint16 = new NativeUint16Array(
  1021. TypedArrayPrototypeGetBuffer(float16bitsArray),
  1022. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  1023. TypedArrayPrototypeGetLength(float16bitsArray)
  1024. );
  1025. const cloned = new Float16Array(
  1026. TypedArrayPrototypeGetBuffer(
  1027. TypedArrayPrototypeSlice(uint16)
  1028. )
  1029. );
  1030. const clonedFloat16bitsArray = getFloat16BitsArray(cloned);
  1031. TypedArrayPrototypeReverse(clonedFloat16bitsArray);
  1032. return cloned;
  1033. }
  1034. fill(value, ...opts) {
  1035. assertFloat16Array(this);
  1036. const float16bitsArray = getFloat16BitsArray(this);
  1037. TypedArrayPrototypeFill(
  1038. float16bitsArray,
  1039. roundToFloat16Bits(value),
  1040. ...safeIfNeeded(opts)
  1041. );
  1042. return this;
  1043. }
  1044. copyWithin(target, start, ...opts) {
  1045. assertFloat16Array(this);
  1046. const float16bitsArray = getFloat16BitsArray(this);
  1047. TypedArrayPrototypeCopyWithin(float16bitsArray, target, start, ...safeIfNeeded(opts));
  1048. return this;
  1049. }
  1050. sort(compareFn) {
  1051. assertFloat16Array(this);
  1052. const float16bitsArray = getFloat16BitsArray(this);
  1053. const sortCompare = compareFn !== undefined ? compareFn : defaultCompare;
  1054. TypedArrayPrototypeSort(float16bitsArray, (x, y) => {
  1055. return sortCompare(convertToNumber(x), convertToNumber(y));
  1056. });
  1057. return this;
  1058. }
  1059. toSorted(compareFn) {
  1060. assertFloat16Array(this);
  1061. const float16bitsArray = getFloat16BitsArray(this);
  1062. if (compareFn !== undefined && typeof compareFn !== "function") {
  1063. throw new NativeTypeError(THE_COMPARISON_FUNCTION_MUST_BE_EITHER_A_FUNCTION_OR_UNDEFINED);
  1064. }
  1065. const sortCompare = compareFn !== undefined ? compareFn : defaultCompare;
  1066. const uint16 = new NativeUint16Array(
  1067. TypedArrayPrototypeGetBuffer(float16bitsArray),
  1068. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  1069. TypedArrayPrototypeGetLength(float16bitsArray)
  1070. );
  1071. const cloned = new Float16Array(
  1072. TypedArrayPrototypeGetBuffer(
  1073. TypedArrayPrototypeSlice(uint16)
  1074. )
  1075. );
  1076. const clonedFloat16bitsArray = getFloat16BitsArray(cloned);
  1077. TypedArrayPrototypeSort(clonedFloat16bitsArray, (x, y) => {
  1078. return sortCompare(convertToNumber(x), convertToNumber(y));
  1079. });
  1080. return cloned;
  1081. }
  1082. slice(start, end) {
  1083. assertFloat16Array(this);
  1084. const float16bitsArray = getFloat16BitsArray(this);
  1085. const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
  1086. if (Constructor === Float16Array) {
  1087. const uint16 = new NativeUint16Array(
  1088. TypedArrayPrototypeGetBuffer(float16bitsArray),
  1089. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  1090. TypedArrayPrototypeGetLength(float16bitsArray)
  1091. );
  1092. return new Float16Array(
  1093. TypedArrayPrototypeGetBuffer(
  1094. TypedArrayPrototypeSlice(uint16, start, end)
  1095. )
  1096. );
  1097. }
  1098. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  1099. const relativeStart = ToIntegerOrInfinity(start);
  1100. const relativeEnd = end === undefined ? length : ToIntegerOrInfinity(end);
  1101. let k;
  1102. if (relativeStart === -Infinity) {
  1103. k = 0;
  1104. } else if (relativeStart < 0) {
  1105. k = length + relativeStart > 0 ? length + relativeStart : 0;
  1106. } else {
  1107. k = length < relativeStart ? length : relativeStart;
  1108. }
  1109. let final;
  1110. if (relativeEnd === -Infinity) {
  1111. final = 0;
  1112. } else if (relativeEnd < 0) {
  1113. final = length + relativeEnd > 0 ? length + relativeEnd : 0;
  1114. } else {
  1115. final = length < relativeEnd ? length : relativeEnd;
  1116. }
  1117. const count = final - k > 0 ? final - k : 0;
  1118. const array = new Constructor(count);
  1119. assertSpeciesTypedArray(array, count);
  1120. if (count === 0) {
  1121. return array;
  1122. }
  1123. const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray);
  1124. if (IsDetachedBuffer(buffer)) {
  1125. throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
  1126. }
  1127. let n = 0;
  1128. while (k < final) {
  1129. array[n] = convertToNumber(float16bitsArray[k]);
  1130. ++k;
  1131. ++n;
  1132. }
  1133. return (array);
  1134. }
  1135. subarray(begin, end) {
  1136. assertFloat16Array(this);
  1137. const float16bitsArray = getFloat16BitsArray(this);
  1138. const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
  1139. const uint16 = new NativeUint16Array(
  1140. TypedArrayPrototypeGetBuffer(float16bitsArray),
  1141. TypedArrayPrototypeGetByteOffset(float16bitsArray),
  1142. TypedArrayPrototypeGetLength(float16bitsArray)
  1143. );
  1144. const uint16Subarray = TypedArrayPrototypeSubarray(uint16, begin, end);
  1145. const array = new Constructor(
  1146. TypedArrayPrototypeGetBuffer(uint16Subarray),
  1147. TypedArrayPrototypeGetByteOffset(uint16Subarray),
  1148. TypedArrayPrototypeGetLength(uint16Subarray)
  1149. );
  1150. assertSpeciesTypedArray(array);
  1151. return (array);
  1152. }
  1153. indexOf(element, ...opts) {
  1154. assertFloat16Array(this);
  1155. const float16bitsArray = getFloat16BitsArray(this);
  1156. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  1157. let from = ToIntegerOrInfinity(opts[0]);
  1158. if (from === Infinity) {
  1159. return -1;
  1160. }
  1161. if (from < 0) {
  1162. from += length;
  1163. if (from < 0) {
  1164. from = 0;
  1165. }
  1166. }
  1167. for (let i = from; i < length; ++i) {
  1168. if (
  1169. ObjectHasOwn(float16bitsArray, i) &&
  1170. convertToNumber(float16bitsArray[i]) === element
  1171. ) {
  1172. return i;
  1173. }
  1174. }
  1175. return -1;
  1176. }
  1177. lastIndexOf(element, ...opts) {
  1178. assertFloat16Array(this);
  1179. const float16bitsArray = getFloat16BitsArray(this);
  1180. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  1181. let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1;
  1182. if (from === -Infinity) {
  1183. return -1;
  1184. }
  1185. if (from >= 0) {
  1186. from = from < length - 1 ? from : length - 1;
  1187. } else {
  1188. from += length;
  1189. }
  1190. for (let i = from; i >= 0; --i) {
  1191. if (
  1192. ObjectHasOwn(float16bitsArray, i) &&
  1193. convertToNumber(float16bitsArray[i]) === element
  1194. ) {
  1195. return i;
  1196. }
  1197. }
  1198. return -1;
  1199. }
  1200. includes(element, ...opts) {
  1201. assertFloat16Array(this);
  1202. const float16bitsArray = getFloat16BitsArray(this);
  1203. const length = TypedArrayPrototypeGetLength(float16bitsArray);
  1204. let from = ToIntegerOrInfinity(opts[0]);
  1205. if (from === Infinity) {
  1206. return false;
  1207. }
  1208. if (from < 0) {
  1209. from += length;
  1210. if (from < 0) {
  1211. from = 0;
  1212. }
  1213. }
  1214. const isNaN = NumberIsNaN(element);
  1215. for (let i = from; i < length; ++i) {
  1216. const value = convertToNumber(float16bitsArray[i]);
  1217. if (isNaN && NumberIsNaN(value)) {
  1218. return true;
  1219. }
  1220. if (value === element) {
  1221. return true;
  1222. }
  1223. }
  1224. return false;
  1225. }
  1226. join(separator) {
  1227. assertFloat16Array(this);
  1228. const float16bitsArray = getFloat16BitsArray(this);
  1229. const array = copyToArray(float16bitsArray);
  1230. return ArrayPrototypeJoin(array, separator);
  1231. }
  1232. toLocaleString(...opts) {
  1233. assertFloat16Array(this);
  1234. const float16bitsArray = getFloat16BitsArray(this);
  1235. const array = copyToArray(float16bitsArray);
  1236. return ArrayPrototypeToLocaleString(array, ...safeIfNeeded(opts));
  1237. }
  1238. get [SymbolToStringTag]() {
  1239. if (isFloat16Array(this)) {
  1240. return ("Float16Array");
  1241. }
  1242. }
  1243. }
  1244. ObjectDefineProperty(Float16Array, "BYTES_PER_ELEMENT", {
  1245. value: BYTES_PER_ELEMENT,
  1246. });
  1247. ObjectDefineProperty(Float16Array, brand, {});
  1248. ReflectSetPrototypeOf(Float16Array, TypedArray);
  1249. const Float16ArrayPrototype = Float16Array.prototype;
  1250. ObjectDefineProperty(Float16ArrayPrototype, "BYTES_PER_ELEMENT", {
  1251. value: BYTES_PER_ELEMENT,
  1252. });
  1253. ObjectDefineProperty(Float16ArrayPrototype, SymbolIterator, {
  1254. value: Float16ArrayPrototype.values,
  1255. writable: true,
  1256. configurable: true,
  1257. });
  1258. ReflectSetPrototypeOf(Float16ArrayPrototype, TypedArrayPrototype);
  1259. function isTypedArray(target) {
  1260. return isNativeTypedArray(target) || isFloat16Array(target);
  1261. }
  1262. function getFloat16(dataView, byteOffset, ...opts) {
  1263. return convertToNumber(
  1264. DataViewPrototypeGetUint16(dataView, byteOffset, ...safeIfNeeded(opts))
  1265. );
  1266. }
  1267. function setFloat16(dataView, byteOffset, value, ...opts) {
  1268. return DataViewPrototypeSetUint16(
  1269. dataView,
  1270. byteOffset,
  1271. roundToFloat16Bits(value),
  1272. ...safeIfNeeded(opts)
  1273. );
  1274. }
  1275. function f16round(x) {
  1276. const number = +x;
  1277. if (!NumberIsFinite(number) || number === 0) {
  1278. return number;
  1279. }
  1280. const x16 = roundToFloat16Bits(number);
  1281. return convertToNumber(x16);
  1282. }
  1283. exports.Float16Array = Float16Array;
  1284. exports.f16round = f16round;
  1285. exports.getFloat16 = getFloat16;
  1286. exports.hfround = f16round;
  1287. exports.isFloat16Array = isFloat16Array;
  1288. exports.isTypedArray = isTypedArray;
  1289. exports.setFloat16 = setFloat16;
  1290. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  1291. return exports;
  1292. })({});