/* Examine an auxv data block and determine its format.
   Return true iff we figured it out.  */
static bool
auxv_format_probe (const void *auxv, size_t size,
		   uint_fast8_t *elfclass, uint_fast8_t *elfdata)
{
  const Elf32_auxv_t (*a32)[size / sizeof (Elf32_auxv_t)] = (void *) auxv;
  const Elf64_auxv_t (*a64)[size / sizeof (Elf64_auxv_t)] = (void *) auxv;

  for (size_t i = 0; i < size / sizeof (Elf64_auxv_t); ++i)
    {
      if (check64 (i))
	{
	  *elfclass = ELFCLASS64;
	  return true;
	}

      if (check32 (i * 2) || check32 (i * 2 + 1))
	{
	  *elfclass = ELFCLASS32;
	  return true;
	}
    }

  return false;
}
/* Examine an auxv data block and determine its format.
   Return true iff we figured it out.  */
static bool
auxv_format_probe (const void *auxv, size_t size,
		   uint_fast8_t *elfclass, uint_fast8_t *elfdata)
{
  const union
  {
    char buf[size];
    Elf32_auxv_t a32[size / sizeof (Elf32_auxv_t)];
    Elf64_auxv_t a64[size / sizeof (Elf64_auxv_t)];
  } *u = auxv;

  inline bool check64 (size_t i)
  {
    if (u->a64[i].a_type == BE64 (PROBE_TYPE)
	&& u->a64[i].a_un.a_val == BE64 (PROBE_VAL64))
      {
	*elfdata = ELFDATA2MSB;
	return true;
      }

    if (u->a64[i].a_type == LE64 (PROBE_TYPE)
	&& u->a64[i].a_un.a_val == LE64 (PROBE_VAL64))
      {
	*elfdata = ELFDATA2LSB;
	return true;
      }

    return false;
  }

  inline bool check32 (size_t i)
  {
    if (u->a32[i].a_type == BE32 (PROBE_TYPE)
	&& u->a32[i].a_un.a_val == BE32 (PROBE_VAL32))
      {
	*elfdata = ELFDATA2MSB;
	return true;
      }

    if (u->a32[i].a_type == LE32 (PROBE_TYPE)
	&& u->a32[i].a_un.a_val == LE32 (PROBE_VAL32))
      {
	*elfdata = ELFDATA2LSB;
	return true;
      }

    return false;
  }

  for (size_t i = 0; i < size / sizeof (Elf64_auxv_t); ++i)
    {
      if (check64 (i))
	{
	  *elfclass = ELFCLASS64;
	  return true;
	}

      if (check32 (i * 2) || check32 (i * 2 + 1))
	{
	  *elfclass = ELFCLASS32;
	  return true;
	}
    }

  return false;
}
void TDD_VerifyCalibration(void) {

    temp32 = DivI32Rnd(1550,100);
    check32(temp32, 15);

    temp32 = DivI32Rnd(1549,100);
    check32(temp32, 15);

    temp32 = DivI32Rnd(1551,100);
    check32(temp32, 16);

    temp32 = DivI32Rnd(1550,-100);
    check32(temp32, -15);

    temp32 = DivI32Rnd(1549,-100);
    check32(temp32, -15);

    temp32 = DivI32Rnd(1551,-100);
    check32(temp32, -16);

    temp32 = DivI32Rnd(-1550,100);
    check32(temp32, -15);

    temp32 = DivI32Rnd(-1549,100);
    check32(temp32, -15);

    temp32 = DivI32Rnd(-1551,100);
    check32(temp32, -16);

    temp32 = DivI32Rnd(-1550,-100);
    check32(temp32, 15);

    temp32 = DivI32Rnd(-1549,-100);
    check32(temp32, 15);

    temp32 = DivI32Rnd(-1551,-100);
    check32(temp32, 16);




    adc_calibration.point1.value = 0;
    adc_calibration.point1.code = 2000;
    adc_calibration.point2.value = 40000;
    adc_calibration.point2.code = 4000;
    adc_calibration.scale = 1;
    CalculateCoefficients(&adc_calibration);

    // First basic functions at calibration points
    temp32u = adc_calibration.point1.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point1.value);

    temp32u = adc_calibration.point2.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point2.value);

    temp32u = 2000 + 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 2000);

    temp32u = 2000 - 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -2000);

    temp32u = 2000 + 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 2260);

    temp32u = 2000 - 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -2260);

    //-----------------------------------------------------//


    adc_calibration.point1.value = 0;
    adc_calibration.point1.code = 2000;
    adc_calibration.point2.value = 400000;
    adc_calibration.point2.code = 4000;
    adc_calibration.scale = 1;
    CalculateCoefficients(&adc_calibration);

    // First basic functions at calibration points
    temp32u = adc_calibration.point1.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point1.value);

    temp32u = adc_calibration.point2.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point2.value);

    temp32u = 2000 + 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 20000);

    temp32u = 2000 - 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -20000);

    temp32u = 2000 + 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 22600);

    temp32u = 2000 - 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -22600);



    //-----------------------------------------------------//


    adc_calibration.point1.value = 0;
    adc_calibration.point1.code = 2047;
    adc_calibration.point2.value = 400000;
    adc_calibration.point2.code = 3859;
    adc_calibration.scale = 1000;
    CalculateCoefficients(&adc_calibration);

    // First basic functions at calibration points
    temp32u = adc_calibration.point1.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point1.value);

    temp32u = adc_calibration.point2.code;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, adc_calibration.point2.value);

    temp32u = 2047 + 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 22075);

    temp32u = 2047 - 100;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -22075);

    temp32u = 2047 + 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, 24945);

    temp32u = 2047 - 113;
    temp32 = GetValueForCode(&adc_calibration, temp32u);
    check32(temp32, -24945);
}
void TDD_VerifyRoundSigned(void) {

    temp32 = 15349;
    round_int32(&temp32, 0);
    check32(temp32, 15350);

    temp32 = 15346;
    round_int32(&temp32, 0);
    check32(temp32, 15350);

    temp32 = 15345;
    round_int32(&temp32, 0);
    check32(temp32, 15350);

    temp32 = 15344;
    round_int32(&temp32, 0);
    check32(temp32, 15340);

    temp32 = 15341;
    round_int32(&temp32, 0);
    check32(temp32, 15340);

    temp32 = 15340;
    round_int32(&temp32, 0);
    check32(temp32, 15340);


    temp32 = 15349;
    round_int32(&temp32, 1);
    check32(temp32, 15300);

    temp32 = 15350;
    round_int32(&temp32, 1);
    check32(temp32, 15400);

    temp32 = 15351;
    round_int32(&temp32, 1);
    check32(temp32, 15400);

    temp32 = 00001;
    round_int32(&temp32, 1);
    check32(temp32, 00000);


    temp32 = 15501;
    round_int32(&temp32, 2);
    check32(temp32, 16000);

    temp32 = 15499;
    round_int32(&temp32, 2);
    check32(temp32, 15000);

    temp32 = 15999;
    round_int32(&temp32, 2);
    check32(temp32, 16000);

    temp32 = 15123;
    round_int32(&temp32, 2);
    check32(temp32, 15000);

    temp32 = 00001;
    round_int32(&temp32, 2);
    check32(temp32, 00000);

    //------------------------------------//

    temp32 = -15349;
    round_int32(&temp32, 0);
    check32(temp32, -15350);

    temp32 = -15346;
    round_int32(&temp32, 0);
    check32(temp32, -15350);

    temp32 = -15345;
    round_int32(&temp32, 0);
    check32(temp32, -15350);

    temp32 = -15344;
    round_int32(&temp32, 0);
    check32(temp32, -15340);

    temp32 = -15341;
    round_int32(&temp32, 0);
    check32(temp32, -15340);

    temp32 = -15340;
    round_int32(&temp32, 0);
    check32(temp32, -15340);


    temp32 = -15349;
    round_int32(&temp32, 1);
    check32(temp32, -15300);

    temp32 = -15350;
    round_int32(&temp32, 1);
    check32(temp32, -15400);

    temp32 = -15351;
    round_int32(&temp32, 1);
    check32(temp32, -15400);

    temp32 = -00001;
    round_int32(&temp32, 1);
    check32(temp32, 0);


    temp32 = -15501;
    round_int32(&temp32, 2);
    check32(temp32, -16000);

    temp32 = -15499;
    round_int32(&temp32, 2);
    check32(temp32, -15000);

    temp32 = -15999;
    round_int32(&temp32, 2);
    check32(temp32, -16000);

    temp32 = -15123;
    round_int32(&temp32, 2);
    check32(temp32, -15000);

    temp32 = -00001;
    round_int32(&temp32, 2);
    check32(temp32, 0);


}