float16.mjs 40 KB

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