Commit 66a2bdbe authored by Federico Vaga's avatar Federico Vaga

lib: add math library functions

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent 53b124a3
......@@ -11,6 +11,7 @@ ZIO ?= ../zio
LIB = libfmctdc.a
LOBJ := fmctdc-lib.o
LOBJ += fmctdc-lib-math.o
CFLAGS = -Wall -ggdb -O2 -I../kernel -I$(ZIO)/include $(EXTRACFLAGS)
......
/*
* The fmc-tdc (a.k.a. FmcTdc1ns5cha) library - timestamp math.
*
* Copyright (C) 2012-2013 CERN (www.cern.ch)
* Author: Federico Vaga <federico.vaga@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 3 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <unistd.h>
#include <errno.h>
#include "fmctdc-lib.h"
#include "fmctdc-lib-private.h"
/**
* It provides a nano-second approximation of the timestamp.
* @param[in] a timestamp
* @return it returns the time stamp in nano-seconds
*/
uint64_t fmctdc_ts_approx_ns(struct fmctdc_time *a)
{
uint64_t ns = 0;
ns += a->seconds * 1000000000;
ns += a->coarse * 8;
ns += a->frac * 81.03 / 1000; /* FIXME check this because in other func is different */
return ns;
}
/**
* It provides a pico-seconds representation of the time stamp. Bear in mind
* that it may overflow. If you thing that it may happen, check the timestamp
* @param[in] a timestamp
* @return it returns the time stamp in pico-seconds
*/
uint64_t fmctdc_ts_ps(struct fmctdc_time *a)
{
uint64_t ps = 0;
ps += a->seconds * 1000000000000;
ps += a->coarse * 8000;
ps += a->frac * 81.03; /* FIXME check this because in other func is different */
return ps;
}
/**
* It perform the subtraction: a = a - b
* @param[in] a timestamp
* @param[in] b timestamp
*/
void fmctdc_ts_sub(struct fmctdc_time *a, struct fmctdc_time *b)
{
int32_t d_frac, d_coarse = 0;
d_frac = a->frac - b->frac;
if (d_frac < 0) {
d_frac += 4096;
d_coarse--;
}
d_coarse += a->coarse - b->coarse;
if (d_coarse < 0) {
d_coarse += 125000000;
a->seconds--;
}
a->coarse = d_coarse;
a->frac = d_frac;
a->seconds -= b->seconds;
}
/**
* It perform an addiction: a = a + b
* @param[in] a timestamp
* @param[in] b timestamp
*/
void ft_ts_add(struct fmctdc_time *a, struct fmctdc_time *b)
{
a->frac += b->frac;
if (a->frac >= 4096) {
a->frac -= 4096;
a->coarse++;
}
a->coarse += b->coarse;
if (a->coarse >= 125000000) {
a->coarse -= 125000000;
a->seconds++;
}
a->seconds += b->seconds;
}
......@@ -13,7 +13,8 @@
#ifndef __FMCTDC_LIB_PRIVATE_H__
#define __FMCTDC_LIB_PRIVATE_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
......
......@@ -418,28 +418,3 @@ extern int fmctdc_check_wr_mode(struct fmctdc_board *userb)
return errno;
}
/* It perform the subtraction:
* a = a - b
*/
void fmctdc_ts_sub(struct fmctdc_time *a, struct fmctdc_time *b)
{
int32_t d_frac, d_coarse = 0;
d_frac = a->frac - b->frac;
if (d_frac < 0) {
d_frac += 4096;
d_coarse--;
}
d_coarse += a->coarse - b->coarse;
if (d_coarse < 0) {
d_coarse += 125000000;
a->seconds--;
}
a->coarse = d_coarse;
a->frac = d_frac;
a->seconds -= b->seconds;
}
......@@ -35,6 +35,10 @@ struct fmctdc_time {
uint32_t seq_id;
};
/**
* @file fmctdc-lib.c
*/
extern int fmctdc_init(void);
extern void fmctdc_exit(void);
......@@ -66,4 +70,12 @@ extern int fmctdc_check_wr_mode(struct fmctdc_board *b);
extern void fmctdc_ts_sub(struct fmctdc_time *a, struct fmctdc_time *b);
/**
*@file fmctdc-lib-math.c
*/
extern uint64_t fmctdc_ts_approx_ns(struct fmctdc_time *a);
extern uint64_t fmctdc_ts_ps(struct fmctdc_time *a);
extern void fmctdc_ts_sub(struct fmctdc_time *a, struct fmctdc_time *b);
extern void ft_ts_add(struct fmctdc_time *a, struct fmctdc_time *b);
#endif /* __FMCTDC_LIB_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment