RISC-V/math/compute.cpp

42 lines
1.3 KiB
C++
Raw Normal View History

2024-03-11 15:05:36 +01:00
#include "compute.h"
void ReadData (int * data, const int len) {
static int t = 0;
for (int n=0; n<len; n++) data [n] = t++;
}
float msqrtf(const float x) {
if (x <= 0.0f) return 0.0f;
float y = x;
for (unsigned n=0; n<10; n++) {
y = 0.5f * (y + x/y);
}
// Debug ("delta=%f\n", fabs(y - sqrtf(x)));
return y;
}
static constexpr float mfabs (const float a) { return a < 0.0f ? -a : +a; }
//static constexpr int iround (const float a) { return a < 0.0f ? int (a - 0.5f) : int (a + 0.5f); }
static constexpr float D_PI = 2.0f * 3.141592653f;
/**
* @brief sinus nebo kosinus
* @param x úhel v radiánech
* @param even počítá sinus, pokud even=true i kosinus, pokud even=false
* @return float výsledek
*/
float sincos (const float x, const bool even) {
if (x > +D_PI) return sincos (x - D_PI, even);
if (x < -D_PI) return sincos (x + D_PI, even);
float result (0.0f), element(1.0f), divider(0.0f);
if (even) { element *= x; divider += 1.0f; }
constexpr float eps = 1.0e-8f; // maximální chyba výpočtu
const float aa = - (x * x);
for (;;) {
result += element;
if (mfabs (element) < eps) break;
divider += 1.0f;
float fact = divider;
divider += 1.0f;
fact *= divider;
element *= aa / fact;
}
return result;
}