All files / src/shared/lib/helpers 8bit.ts

77.03% Statements 57/74
0% Branches 0/4
50% Functions 11/22
69.05% Lines 29/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 6449x 49x   2812x 49x 49x   49x 1x   49x 8x   49x 9x   49x     49x     49x     49x 1x   49x 50x   384x   49x   49x   49x     49x                 52x   49x             49x 49x   49x 49x  
export const SIGNED_16BIT_MAX = 32767;
export const SIGNED_16BIT_MIN = -32768;
 
export const wrap8Bit = (val: number) => (256 + (val % 256)) % 256;
export const wrap16Bit = (val: number) => (65536 + (val % 65536)) % 65536;
export const clampSigned16Bit = (val: number) =>
  Math.max(SIGNED_16BIT_MIN, Math.min(SIGNED_16BIT_MAX, val));
export const wrap32Bit = (val: number) =>
  (4294967296 + (val % 4294967296)) % 4294967296;
 
export const decBin = (dec: number) =>
  wrap8Bit(dec).toString(2).padStart(8, "0");
 
export const decHex = (dec: number) =>
  `0x${wrap8Bit(dec).toString(16).padStart(2, "0").toUpperCase()}`;
 
export const decHexVal = (dec: number) =>
  wrap8Bit(dec).toString(16).padStart(2, "0").toUpperCase();
 
export const decHex16 = (dec: number) =>
  `0x${wrap16Bit(dec).toString(16).padStart(4, "0").toUpperCase()}`;
 
export const decHex16Val = (dec: number) =>
  wrap16Bit(dec).toString(16).padStart(4, "0").toUpperCase();
 
export const decHex32Val = (dec: number) =>
  wrap32Bit(dec).toString(16).padStart(8, "0").toUpperCase();
 
export const decOct = (dec: number) =>
  wrap8Bit(dec).toString(8).padStart(3, "0");
 
export const hexDec = (hex: string) => parseInt(hex, 16);
 
export const hi = (longNum: number) => wrap16Bit(longNum) >> 8;
 
export const lo = (longNum: number) => wrap16Bit(longNum) % 256;
 
export const signed8BitVal = (dec: number) =>
  dec >= 0 ? dec & 0x7f : 0x80 + (0x80 - (Math.abs(dec) & 0x7f));
 
export const fromSigned8Bit = (num: number): number => {
  const masked = num & 0xff;
  if (masked & 0x80) {
    return masked - 0x100;
  } else {
    return masked;
  }
};
 
export const divisibleBy8 = (n: number) => (n >> 3) << 3 === n;
 
export const convertHexTo15BitDec = (hex: string) => {
  const r = Math.floor(hexDec(hex.substring(0, 2)) * (32 / 256));
  const g = Math.floor(hexDec(hex.substring(2, 4)) * (32 / 256));
  const b = Math.max(1, Math.floor(hexDec(hex.substring(4, 6)) * (32 / 256)));
  return (b << 10) + (g << 5) + r;
};
 
export const roundDown8 = (v: number): number => 8 * Math.floor(v / 8);
export const roundDown16 = (v: number): number => 16 * Math.floor(v / 16);
 
export const roundUp16 = (x: number): number => Math.ceil(x / 16) * 16;
export const roundUp8 = (x: number): number => Math.ceil(x / 8) * 8;