/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _COMPARE_H_
#define _COMPARE_H_

/*! \brief  Contains public interface to various functions related
 *          to the compare object
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include <stdbool.h>
#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

#define FLOAT_EQUAL_DEADBAND (0.0001)

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief The greater than (>) comparison
 *
 * This function do the greater comparison between 2 IQ value
 *
 * \param[in] A : value A in IQ format
 * \param[in] B : value B in IQ format
 */
static inline bool Compare_GreaterThan(_iq A, _iq B)
{
    return A > B;
}

/*! \brief The smaller than (<) comparison
 *
 * This function do the smaller comparison between 2 IQ value
 *
 * \param[in] A : value A in IQ format
 * \param[in] B : value B in IQ format
 */
static inline bool Compare_SmallerThan(_iq A, _iq B)
{
    return A < B;
}

/*! \brief  The greater than or equal (>=) comparison
 *
 * This function do the greater than or equal (>=) comparison between 2 IQ value
 *
 * \param[in] A : value A in IQ format
 * \param[in] B : value B in IQ format
 */
static inline bool Compare_GreaterOrEqual(_iq A, _iq B)
{
    return A >= B;
}

/*! \brief  The smaller than or equal (<=) comparison
 *
 * This function do the smaller than or equal (<=) comparison between 2 IQ value
 *
 * \param[in] A : value A in IQ format
 * \param[in] B : value B in IQ format
 */
static inline bool Compare_SmallerOrEqual(_iq A, _iq B)
{
    return A <= B;
}

/*! \brief  The equal (==) comparison
 *
 * This function do the equal (==) comparison between 2 IQ value
 *
 * \note If the IQ is floating value, it's not the exact equal because of the precision
 *       of floating value.
 *
 * \param[in] A : value A in IQ format
 * \param[in] B : value B in IQ format
 */
static inline bool Compare_Equal(_iq A, _iq B)
{
    if((long)_IQ(0.1))
    { /* Fix-point IQ math */
        return (A == B);
    }
    else
    {
        return (_IQabs(A - B) < _IQ(FLOAT_EQUAL_DEADBAND));
    }
}

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _COMPARE_H_ */
