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

100% Statements 75/75
100% Branches 4/4
100% Functions 22/22
100% Lines 43/43

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 64 65 66 67130x 130x   6481925x 130x   130x 30x 30x     130x 11x 130x 25x   130x 8x   130x 9x   130x 8x   130x 9x   130x 9x   130x 17x   130x 129x   2110x   130x   130x   130x 11x 11x 8x   3x       130x   130x 8x 8x 8x 8x     130x 130x   130x 130x  
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 wrapSigned8Bit = (val: number) => {
  const u = wrap8Bit(val);
  return u >= 128 ? u - 256 : u;
};
 
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 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;