GY-63_MS5611/libraries/IEEE754tools/examples/FastNegate/FastNegate.ino

193 lines
3.8 KiB
Arduino
Raw Normal View History

2020-11-27 05:16:22 -05:00
//
// FILE: FastNegate.ino
// AUTHOR: Rob dot Tillaart at gmail dot com
// PURPOSE: Fast negate for floating points
//
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
volatile float zz = 100;
volatile int x = 3;
uint32_t start, duration1, duration2;
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
test_negfabs();
test_fabs();
test_negate();
test_less_zero();
test5();
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test_negfabs()
{
Serial.println(__FUNCTION__);
2021-11-05 12:24:10 -04:00
Serial.println("TEST : zz = -fabs(zz)");
2020-11-27 05:16:22 -05:00
start = micros();
for (int i = 0; i < 30000; i++)
{
*(((byte*) &zz) + 3) |= 0x80; // Force negative == -fabs(zz);
}
duration1 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration1 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
delay(10);
zz = 100;
start = micros();
for (int i = 0; i < 30000; i++)
{
zz = -fabs(zz);
}
duration2 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration2 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
2021-11-05 12:24:10 -04:00
Serial.print("GAIN : ");
Serial.println(1.0 * duration2 / duration1, 2);
2020-11-27 05:16:22 -05:00
Serial.println();
delay(10);
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test_fabs()
{
Serial.println(__FUNCTION__);
2021-11-05 12:24:10 -04:00
Serial.println("TEST : zz = fabs(zz)");
2020-11-27 05:16:22 -05:00
start = micros();
for (int i = 0; i < 30000; i++)
{
*(((byte*) &zz) + 3) &= 0x7F; // force positive == fabs(zz);
}
duration1 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration1 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
delay(10);
start = micros();
for (int i = 0; i < 30000; i++)
{
zz = fabs(zz);
}
duration2 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration2 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
2021-11-05 12:24:10 -04:00
Serial.print("GAIN : ");
Serial.println(1.0 * duration2 / duration1, 2);
2020-11-27 05:16:22 -05:00
Serial.println();
delay(10);
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test_negate()
{
Serial.println(__FUNCTION__);
2021-11-05 12:24:10 -04:00
Serial.println("TEST : zz = -zz");
2020-11-27 05:16:22 -05:00
start = micros();
for (int i = 0; i < 30000; i++)
{
*(((byte*) &zz) + 3) ^= 0x80;
}
duration1 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration1 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
start = micros();
for (int i = 0; i < 30000; i++)
{
zz = -zz;
}
duration2 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration2 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
2021-11-05 12:24:10 -04:00
Serial.print("GAIN : ");
Serial.println(1.0 * duration2 / duration1, 2);
2020-11-27 05:16:22 -05:00
Serial.println();
delay(10);
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test_less_zero()
{
Serial.println(__FUNCTION__);
2021-11-05 12:24:10 -04:00
Serial.println("TEST : if (zz < 0) ");
2020-11-27 05:16:22 -05:00
start = micros();
for (int i = 0; i < 30000; i++)
{
if ( *(((byte*) &zz) + 3) & 0x80) x = 2; // equals if (zz < 0);
}
duration1 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration1 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
start = micros();
for (int i = 0; i < 30000; i++)
{
if (zz < 0) x = 2;
}
duration2 = micros() - start;
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
Serial.println(duration2 / 30000.0, 4);
Serial.print("VALUE: ");
2020-11-27 05:16:22 -05:00
Serial.println(zz);
2021-11-05 12:24:10 -04:00
Serial.print("GAIN : ");
Serial.println(1.0 * duration2 / duration1, 2);
2020-11-27 05:16:22 -05:00
start = micros();
for (int i = 0; i < 30000; i++)
{
x = 2;
}
2021-11-05 12:24:10 -04:00
Serial.print("TIME : ");
2020-11-27 05:16:22 -05:00
Serial.println((micros() - start) / 30000.0, 4);
Serial.println();
delay(10);
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test5()
{
Serial.println(__FUNCTION__);
zz = -100;
if (zz < 0) Serial.println("N");
else Serial.println("P");
zz = 100;
if (zz < 0) Serial.println("N");
else Serial.println("P");
zz = -100;
if (*(((byte*) &zz) + 3) & 0x80) Serial.println("N");
else Serial.println("P");
zz = 100;
if (*(((byte*) &zz) + 3) & 0x80) Serial.println("N");
else Serial.println("P");
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void loop()
{
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
// -- END OF FILE --
2021-12-20 02:09:14 -05:00