/*
 * Copyright (C) 2008 Tom Phelan
 *
 * This file is part of dccp-tp.
 *
 * Dccp-tp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Dccp-tp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with dccp-tp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Documentation and source code for dccp-tp is available at
 * http://www.phelan-4.com/dccp-tp/.
 */

#ifndef _FIXEDPOINTMATH_H_
#define _FIXEDPOINTMATH_H_

#include "dctpSupportTypes.h"

typedef int64_t fixedp;

#define FPTP_RES         1000000000    /* Nine digits after the decimal point */
/* Create fixedp value from constants, as in fptpCreate */
#define FPTP_CONST(i, f) ((fixedp)(((uint64_t)i)*FPTP_RES +((uint64_t)f)))
#define FPTP_MAX         0x7fffffffffffffff
#define FPTP_MAXINT      (FPTP_MAX/FPTP_RES)
#define FPTP_ZERO        0
#define FPTP_4G          0x100000000       /* 2**32 as integer */
#define FPTP_ONE         FPTP_CONST(1, 0)  /* 1.0 as fixedp */
#define FPTP_TWO         FPTP_CONST(2, 0)  /* 2.0 as fixedp */
#define FPTP_SQRTERR     FPTP_CONST(0, 10) /* Maximum error for square roots */

fixedp fptpCreate(int64_t i, uint64_t f);
int64_t fptpInteger(fixedp x);
uint64_t fptpFraction(fixedp x);
char *fptp2str(fixedp x);
fixedp fptpMult(fixedp a, fixedp b);
fixedp fptpDiv(fixedp a, fixedp b);
fixedp fptpSqrt(fixedp x);
uint64_t fptpUsecs(fixedp s);
fixedp fptpCreateInt(uint64_t i);
fixedp fptpConvertUsec(uint64_t usec);

#endif  /* FIXEDPOINTMATH_H_ */

