11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst types = [
51cb0ef41Sopenharmony_ci  'Uint8',
61cb0ef41Sopenharmony_ci  'Uint16LE',
71cb0ef41Sopenharmony_ci  'Uint16BE',
81cb0ef41Sopenharmony_ci  'Uint32LE',
91cb0ef41Sopenharmony_ci  'Uint32BE',
101cb0ef41Sopenharmony_ci  'Int8',
111cb0ef41Sopenharmony_ci  'Int16LE',
121cb0ef41Sopenharmony_ci  'Int16BE',
131cb0ef41Sopenharmony_ci  'Int32LE',
141cb0ef41Sopenharmony_ci  'Int32BE',
151cb0ef41Sopenharmony_ci  'Float32LE',
161cb0ef41Sopenharmony_ci  'Float32BE',
171cb0ef41Sopenharmony_ci  'Float64LE',
181cb0ef41Sopenharmony_ci  'Float64BE',
191cb0ef41Sopenharmony_ci];
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
221cb0ef41Sopenharmony_ci  type: types,
231cb0ef41Sopenharmony_ci  n: [1e6],
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst INT8 = 0x7f;
271cb0ef41Sopenharmony_ciconst INT16 = 0x7fff;
281cb0ef41Sopenharmony_ciconst INT32 = 0x7fffffff;
291cb0ef41Sopenharmony_ciconst UINT8 = INT8 * 2;
301cb0ef41Sopenharmony_ciconst UINT16 = INT16 * 2;
311cb0ef41Sopenharmony_ciconst UINT32 = INT32 * 2;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst mod = {
341cb0ef41Sopenharmony_ci  setInt8: INT8,
351cb0ef41Sopenharmony_ci  setInt16: INT16,
361cb0ef41Sopenharmony_ci  setInt32: INT32,
371cb0ef41Sopenharmony_ci  setUint8: UINT8,
381cb0ef41Sopenharmony_ci  setUint16: UINT16,
391cb0ef41Sopenharmony_ci  setUint32: UINT32,
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cifunction main({ n, type }) {
431cb0ef41Sopenharmony_ci  const ab = new ArrayBuffer(8);
441cb0ef41Sopenharmony_ci  const dv = new DataView(ab, 0, 8);
451cb0ef41Sopenharmony_ci  const le = /LE$/.test(type);
461cb0ef41Sopenharmony_ci  const fn = `set${type.replace(/[LB]E$/, '')}`;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (/int/i.test(fn))
491cb0ef41Sopenharmony_ci    benchInt(dv, fn, n, le);
501cb0ef41Sopenharmony_ci  else
511cb0ef41Sopenharmony_ci    benchFloat(dv, fn, n, le);
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cifunction benchInt(dv, fn, len, le) {
551cb0ef41Sopenharmony_ci  const m = mod[fn];
561cb0ef41Sopenharmony_ci  const method = dv[fn];
571cb0ef41Sopenharmony_ci  bench.start();
581cb0ef41Sopenharmony_ci  for (let i = 0; i < len; i++) {
591cb0ef41Sopenharmony_ci    method.call(dv, 0, i % m, le);
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci  bench.end(len);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cifunction benchFloat(dv, fn, len, le) {
651cb0ef41Sopenharmony_ci  const method = dv[fn];
661cb0ef41Sopenharmony_ci  bench.start();
671cb0ef41Sopenharmony_ci  for (let i = 0; i < len; i++) {
681cb0ef41Sopenharmony_ci    method.call(dv, 0, i * 0.1, le);
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci  bench.end(len);
711cb0ef41Sopenharmony_ci}
72