nv-facutil-slct-ta 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +187 -44
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,6 +8,80 @@ const tcate = (o)=> {
8
8
  return 'other'
9
9
  }
10
10
 
11
+ // 判断是否为有符号整数类型(包括大整数)
12
+ const is_signed_int_cls = (TypedArrayClass) => {
13
+ return (
14
+ TypedArrayClass === Int8Array ||
15
+ TypedArrayClass === Int16Array ||
16
+ TypedArrayClass === Int32Array ||
17
+ TypedArrayClass === BigInt64Array // 有符号大整数(64位)
18
+ );
19
+ };
20
+
21
+ // 判断是否为无符号整数类型(包括大整数和 clamped 类型)
22
+ const is_unsigned_int_cls = (TypedArrayClass) => {
23
+ return (
24
+ TypedArrayClass === Uint8Array || // 无符号8位整数
25
+ TypedArrayClass === Uint8ClampedArray || // 无符号8位钳位整数(特殊无符号)
26
+ TypedArrayClass === Uint16Array || // 无符号16位整数
27
+ TypedArrayClass === Uint32Array || // 无符号32位整数
28
+ TypedArrayClass === BigUint64Array // 无符号大整数(64位)
29
+ );
30
+ };
31
+
32
+ function get_display_name_prefix(TypedArrayClass) {
33
+ const byteSize = TypedArrayClass.BYTES_PER_ELEMENT;
34
+ // 判断是否为有符号类型(需要添加"Half"前缀)
35
+
36
+
37
+ // 基础名称映射
38
+ let baseName;
39
+ if (byteSize === 1) {
40
+ baseName = "Tiny";
41
+ } else if (byteSize === 2) {
42
+ baseName = "Small";
43
+ } else if (byteSize === 4) {
44
+ baseName = "Normal";
45
+ } else {
46
+ baseName = "Large";
47
+ }
48
+
49
+ // 有符号类型添加"Half"前缀
50
+ return is_signed_int_cls(TypedArrayClass) ? `Half${baseName}` : baseName;
51
+ }
52
+
53
+ const DUMP_ONE_BYTE_TYPE_MP = {
54
+ 0: Uint8Array,
55
+ 1: Uint16Array,
56
+ 2: Uint32Array,
57
+ 3: BigUint64Array, // js version NOT supported
58
+ //-----------------------------------------------------
59
+ 4: Int8Array,
60
+ 5: Int16Array,
61
+ 6: Int32Array,
62
+ 7: BigInt64Array, // js version NOT supported
63
+ 255: Float64Array,
64
+ }
65
+
66
+ const CLS2BYT = new Map([
67
+ [Uint8Array,0],
68
+ [Uint16Array,1],
69
+ [Uint32Array,2],
70
+ [BigUint64Array,3],
71
+ //-----------------------------------------------------------
72
+ [Int8Array,4],
73
+ [Int16Array,5],
74
+ [Int32Array,6],
75
+ [BigInt64Array,7],
76
+ [Float64Array,255]
77
+ ]);
78
+
79
+
80
+
81
+
82
+
83
+
84
+
11
85
 
12
86
 
13
87
  ////
@@ -137,24 +211,44 @@ function to_u8avw(ta) {
137
211
  function cp_rtrn_u8a(ta) {return to_u8avw(ta).slice(0)}
138
212
 
139
213
 
140
-
141
-
142
- function calc_max_incr_cnt(Cls,curr_cnt) {
143
- if(Cls === Float64Array) {
144
- return 2**53 - curr_cnt;
145
- } else if(Cls === BigUint64Array) { //JS version NOT supported
146
- var limit = 2n** (8n* BigInt(Cls.BYTES_PER_ELEMENT));
147
- return limit - BigInt(curr_cnt);
148
- } else {
149
- var limit = 2** (8*Cls.BYTES_PER_ELEMENT);
150
- return limit - curr_cnt;
214
+ function calc_max_incr_cnt(Cls, curr_cnt) {
215
+ // 处理 Float64Array
216
+ if (Cls === Float64Array) {
217
+ return Number.MAX_SAFE_INTEGER - curr_cnt; // 2^53-1 是最大安全整数
218
+ }
219
+ // 处理 BigUint64Array
220
+ else if (Cls === BigUint64Array) {
221
+ const n = 8n * BigInt(Cls.BYTES_PER_ELEMENT); // 64
222
+ const limit = 2n ** n; // 2^64
223
+ return limit - 1n- BigInt(curr_cnt);
224
+ }
225
+ // 处理有符号整数类型
226
+ else if (Cls === Int8Array) {
227
+ return 127 - curr_cnt; // 2^7 - 1
228
+ }
229
+ else if (Cls === Int16Array) {
230
+ return 32767 - curr_cnt; // 2^15 - 1
231
+ }
232
+ else if (Cls === Int32Array) {
233
+ return 2147483647 - curr_cnt; // 2^31 - 1
234
+ }
235
+ // 处理无符号整数类型(使用原逻辑)
236
+ else {
237
+ // 计算无符号整数最大值:2^(8*字节数) - 1
238
+ const n = 8 * Cls.BYTES_PER_ELEMENT;
239
+ const limit = 2 ** n; // 2^8, 2^16, 2^32
240
+ return limit -1 - curr_cnt;
151
241
  }
152
242
  }
153
243
 
244
+
154
245
  function get_ta_cls_with_max_cnt(cnt,using_u64_when_gt_safe_f64_int=false) {
155
246
  cnt = BigInt(cnt);
156
- if(cnt<=255n) { return Uint8Array;
247
+ if(cnt <= 127n) { return Int8Array;
248
+ } else if(cnt<=255n) { return Uint8Array;
249
+ } else if(cnt<=32767n) { return Int16Array;
157
250
  } else if(cnt<=65535n) { return Uint16Array;
251
+ } else if(cnt<=2147483647n) { return Int32Array;
158
252
  } else if(cnt<=(2n**32n-1n)) { return Uint32Array;
159
253
  } else if(cnt<=(2n**53n-1n)) {
160
254
  return using_u64_when_gt_safe_f64_int?BigUint64Array:Float64Array;
@@ -163,37 +257,19 @@ function get_ta_cls_with_max_cnt(cnt,using_u64_when_gt_safe_f64_int=false) {
163
257
  }
164
258
  }
165
259
 
166
- const DUMP_ONE_BYTE_TYPE_MP = {
167
- 0: Uint8Array,
168
- 1: Uint16Array,
169
- 2: Uint32Array,
170
- 3: BigUint64Array, // js version NOT supported
171
- 255: Float64Array,
172
- }
173
-
174
- const CLS2BYT = new Map([
175
- [Uint8Array,0],
176
- [Uint16Array,1],
177
- [Uint32Array,2],
178
- [BigUint64Array,3],
179
- [Float64Array,255]
180
- ]);
181
260
 
182
261
  function get_byt_from_cls(Cls) {
183
262
  return CLS2BYT.get(Cls)
184
263
  }
264
+ function get_fst_byte_for_dump(Cls) { //旧代码兼容
265
+ return CLS2BYT.get(Cls)
266
+ }
267
+
185
268
 
186
269
  function get_cls_from_dump_fst_byte(byt) {
187
270
  return DUMP_ONE_BYTE_TYPE_MP[byt]??Float64Array
188
271
  }
189
272
 
190
- function get_fst_byte_for_dump(Cls) {
191
- if(Cls === Uint8Array) return 0;
192
- if(Cls === Uint16Array) return 1;
193
- if(Cls === Uint32Array) return 2;
194
- if(Cls === BigUint64Array) return 3; // js version NOT supported
195
- return 255;
196
- }
197
273
 
198
274
  function set_fst_byte_for_dump(ta) {
199
275
  var u8a = new Uint8Array(ta.buffer,ta.byteOffset,1);
@@ -211,14 +287,6 @@ function get_nth_byte(ta,I) {
211
287
  }
212
288
 
213
289
 
214
- function get_display_name_prefix(TypedArrayClass) {
215
- if(TypedArrayClass.BYTES_PER_ELEMENT === 1) {return "Tiny"}
216
- if(TypedArrayClass.BYTES_PER_ELEMENT === 2) {return "Small"}
217
- if(TypedArrayClass.BYTES_PER_ELEMENT === 4) {return "Normal"}
218
- return "Large";
219
- }
220
-
221
-
222
290
  //------------------------------------------------------------------------------
223
291
 
224
292
 
@@ -412,8 +480,80 @@ const min_tacls = (...Clss) => {
412
480
  return min;
413
481
  };
414
482
 
483
+ const read_dv_with_cls = (dv, Cls, offset, little_endian = true) => {
484
+ if (Cls === Uint8Array) {
485
+ return dv.getUint8(offset);
486
+ } else if (Cls === Int8Array) {
487
+ return dv.getInt8(offset);
488
+
489
+ } else if (Cls === Uint16Array) {
490
+ return dv.getUint16(offset, little_endian);
491
+ } else if (Cls === Int16Array) {
492
+ return dv.getInt16(offset, little_endian);
493
+
494
+ } else if (Cls === Uint32Array) {
495
+ return dv.getUint32(offset, little_endian);
496
+ } else if (Cls === Int32Array) {
497
+ return dv.getInt32(offset, little_endian);
498
+
499
+ } else if (Cls === Float32Array) {
500
+ return dv.getFloat32(offset, little_endian);
501
+ } else if (Cls === Float64Array) {
502
+ return dv.getFloat64(offset, little_endian);
503
+
504
+ } else if (Cls === BigUint64Array) {
505
+ return dv.getBigUint64(offset, little_endian);
506
+ } else if (Cls === BigInt64Array) {
507
+ return dv.getBigInt64(offset, little_endian);
508
+
509
+ } else if (globalThis.Float16Array && Cls === Float16Array) {
510
+ const u16 = dv.getUint16(offset, little_endian);
511
+ return f16_to_f32(u16);
512
+
513
+ } else {
514
+ throw new Error("Unsupported TypedArray class");
515
+ }
516
+ };
517
+
518
+
519
+ const write_dv_with_cls = (dv, Cls, offset, value, little_endian = true) => {
520
+ if (Cls === Uint8Array) {
521
+ dv.setUint8(offset, value);
522
+ } else if (Cls === Int8Array) {
523
+ dv.setInt8(offset, value);
524
+
525
+ } else if (Cls === Uint16Array) {
526
+ dv.setUint16(offset, value, little_endian);
527
+ } else if (Cls === Int16Array) {
528
+ dv.setInt16(offset, value, little_endian);
529
+
530
+ } else if (Cls === Uint32Array) {
531
+ dv.setUint32(offset, value, little_endian);
532
+ } else if (Cls === Int32Array) {
533
+ dv.setInt32(offset, value, little_endian);
534
+
535
+ } else if (Cls === Float32Array) {
536
+ dv.setFloat32(offset, value, little_endian);
537
+ } else if (Cls === Float64Array) {
538
+ dv.setFloat64(offset, value, little_endian);
539
+
540
+ } else if (Cls === BigUint64Array) {
541
+ dv.setBigUint64(offset, value, little_endian);
542
+ } else if (Cls === BigInt64Array) {
543
+ dv.setBigInt64(offset, value, little_endian);
544
+
545
+ } else if (globalThis.Float16Array && Cls === Float16Array) {
546
+ const u16 = f32_to_f16(value);
547
+ dv.setUint16(offset, u16, little_endian);
548
+
549
+ } else {
550
+ throw new Error("Unsupported TypedArray class");
551
+ }
552
+ };
553
+
554
+
415
555
  module.exports = {
416
- tcate,
556
+ tcate, is_signed_int_cls , is_unsigned_int_cls,
417
557
  ////
418
558
  check_sz_and_algn,
419
559
  calc_algn_si,
@@ -464,5 +604,8 @@ module.exports = {
464
604
  ele_tbrief_to_tacls,
465
605
  tacls_to_ele_tbrief,
466
606
  ////
467
- min_tacls,max_tacls
607
+ min_tacls,max_tacls,
608
+ read_dv_with_cls,
609
+ write_dv_with_cls,
610
+ ////
468
611
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-facutil-slct-ta",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"