hamnet70/impl/src/utils.c

100 lines
1.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include <errno.h>
#include <time.h>
#include "utils.h"
bool dump_array_cf(const float complex *data, size_t n, float T, const char *filename)
{
FILE *f = fopen(filename, "w");
if(!f) {
return false;
}
size_t written = fwrite("CPX_", 1, 4, f);
if(written != 4) {
goto err_close;
}
written = fwrite(&T, sizeof(T), 1, f);
if(written != 1) {
goto err_close;
}
written = fwrite(data, sizeof(data[0]), n, f);
if(written != n) {
goto err_close;
}
fclose(f);
return true;
err_close:
fclose(f);
return false;
}
bool dump_array_f(const float *data, size_t n, float T, const char *filename)
{
FILE *f = fopen(filename, "w");
if(!f) {
return false;
}
size_t written = fwrite("FLT_", 1, 4, f);
if(written != 4) {
goto err_close;
}
written = fwrite(&T, sizeof(T), 1, f);
if(written != 1) {
goto err_close;
}
written = fwrite(data, sizeof(data[0]), n, f);
if(written != n) {
goto err_close;
}
fclose(f);
return true;
err_close:
fclose(f);
return false;
}
double get_hires_time(void)
{
struct timespec clk;
clock_gettime(CLOCK_MONOTONIC, &clk);
return clk.tv_sec + 1e-9 * clk.tv_nsec;
}
void fsleep(double d)
{
struct timespec ts;
ts.tv_sec = (time_t)d;
ts.tv_nsec = (long)(1e9 * (d - (long)d));
nanosleep(&ts, NULL);
}
void sleep_until(double hires_time)
{
struct timespec tv;
int ret;
tv.tv_sec = hires_time;
tv.tv_nsec = (uint64_t)(1e9 * hires_time) % 1000000000;
do {
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tv, NULL);
} while(ret == EINTR);
}