dark-hammer / include / core / numeric.h

/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#ifndef __NUMERIC_H__
#define __NUMERIC_H__

#include "types.h"
#include "core-api.h"

#if defined(PI)
#undef PI
#endif

#if defined(EPSILON)
#undef EPSILON
#endif

#define EPSILON    0.00001f
#define PI         3.14159265f
#define PI_2X      6.2831853f
#define PI_HALF    1.570796325f

#ifdef __cplusplus
extern "C"  {
#endif

/**
 * @defgroup num Numeric: math
 */
 
 /**
 * initializes random seed by system timer 
 * @ingroup num
 */
CORE_API void rand_seed();

/**
 * flips a count given a probability value 
 * @param prob should be between 0~100, it is the chance of heads for the coin (true) -
 * which normally whould be 50 for 50-50 chance
 * @ingroup num
 */
CORE_API bool_t rand_flipcoin(uint32 prob);

/**
 * get a random value between two integer values , range = [min, max]
 * @ingroup num
 */
CORE_API int32 rand_getn(int32 min, int32 max);

/**
 * get a random value between two floating-point values, range = [min, max] 
 * @ingroup num
 */
CORE_API fl32 rand_getf(fl32 min, fl32 max);

/**
 * gets an array of random integer values, between [min, max]
 * @param items integer items  to be filled with random values
 * @ingroup num
 */
CORE_API void rand_getarr(int32* items, uint32 num, int32 min, int32 max);

/**
 * clamp input float value to [min_value, max_value]
 * @ingroup num
 */
INLINE fl32 clampf(fl32 value, fl32 min_value, fl32 max_value)
{
    if (value < min_value)      return min_value;
    else if (value > max_value) return max_value;
    else                        return value;
}

/**
 * clamp input integer value to [min_value, max_value]
 * @ingroup num
 */
INLINE int32 clampn(int32 value, int32 min_value, int32 max_value)
{
    if (value < min_value)      return min_value;
    else if (value > max_value) return max_value;
    else                        return value;
}

/**
 * clamp input unsigned integer value to [min_value, max_value]
 * @ingroup num
 */
INLINE uint32 clampun(uint32 value, uint32 min_value, uint32 max_value)
{
    if (value < min_value)      return min_value;
    else if (value > max_value) return max_value;
    else                        return value;
}

/**
 * return minimum of two float values
 * @ingroup num
 */
INLINE fl32 minf(fl32 n1, fl32 n2)
{
    return (n1 < n2) ? n1 : n2;
}

/**
 * return minimum of two integer values
 * @ingroup num
 */
INLINE int32 minn(int32 n1, int32 n2)
{
    return (n1 < n2) ? n1 : n2;
}

/**
 * return minimum of two unsigned integer values
 * @ingroup num
 */
INLINE uint32 minun(uint32 n1, uint32 n2)
{
    return (n1 < n2) ? n1 : n2;
}

/**
 * return maximum of two float values
 * @ingroup num
 */
INLINE fl32 maxf(fl32 n1, fl32 n2)
{
    return (n1 > n2) ? n1 : n2;
}

/**
 * return maximum of two integer values
 * @ingroup num
 */
INLINE int32 maxn(int32 n1, int32 n2)
{
    return (n1 > n2) ? n1 : n2;
}

/**
 * return maximum of two unsigned integer values
 * @ingroup num
 */
INLINE uint32 maxun(uint32 n1, uint32 n2)
{
    return (n1 > n2) ? n1 : n2;
}

/**
 * swap two float values with each other
 * @ingroup num
 */
INLINE void swapf(fl32* n1, fl32* n2)
{
    register fl32 tmp = *n1;
    *n1 = *n2;
    *n2 = tmp;
}

/**
 * swap two integer values with each other
 * @ingroup num
 */
INLINE void swapn(int32* n1, int32* n2)
{
    register int32 tmp = *n1;
    *n1 = *n2;
    *n2 = tmp;
}

/**
 * swap two unsigned integer values with each other
 * @ingroup num
 */
INLINE void swapun(uint32* n1, uint32* n2)
{
    register uint32 tmp = *n1;
    *n1 = *n2;
    *n2 = tmp;
}

/**
 * swap two pointers with each other
 * @ingroup num
 */
INLINE void swapptr(void** pp1, void** pp2)
{
	register void* tmp = *pp1;
	*pp1 = *pp2;
	*pp2 = tmp;
}

#ifdef __cplusplus
}
#endif

#endif /* __NUMERIC_H__ */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.