git-svn-id: svn://db.shs.com.ru/pip@251 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-09-01 15:15:51 +00:00
parent 414e1b9831
commit e20ed0a3ac
10 changed files with 222 additions and 41 deletions

View File

@@ -1,26 +1,26 @@
/*! \file pibase.h
* \brief Base types and functions
*
*
* This file implements first layer above the system and
* declares some basic useful functions
*/
/*
PIP - Platform Independent Primitives
Base types and functions
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Base types and functions
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIBASE_H
@@ -176,9 +176,9 @@
# pragma warning(disable: 4986)
# pragma warning(disable: 4996)
# ifdef ARCH_BITS_32
typedef long ssize_t;
typedef long ssize_t;
# else
typedef long long ssize_t;
typedef long long ssize_t;
# endif
#endif
@@ -266,7 +266,7 @@ template<typename T> inline void piSwapBinary(T & f, T & s) {
* There are some macros:
* - \c piRoundf for "float"
* - \c piRoundd for "double"
*
*
* Example:
* \snippet piincludes.cpp round */
template<typename T> inline int piRound(const T & v) {return int(v >= T(0.) ? v + T(0.5) : v - T(0.5));}
@@ -276,7 +276,7 @@ template<typename T> inline int piRound(const T & v) {return int(v >= T(0.) ? v
* There are some macros:
* - \c piFloorf for "float"
* - \c piFloord for "double"
*
*
* Example:
* \snippet piincludes.cpp floor */
template<typename T> inline int piFloor(const T & v) {return v < T(0) ? int(v) - 1 : int(v);}
@@ -286,7 +286,7 @@ template<typename T> inline int piFloor(const T & v) {return v < T(0) ? int(v) -
* There are some macros:
* - \c piCeilf for "float"
* - \c piCeild for "double"
*
*
* Example:
* \snippet piincludes.cpp ceil */
template<typename T> inline int piCeil(const T & v) {return v < T(0) ? int(v) : int(v) + 1;}
@@ -300,7 +300,7 @@ template<typename T> inline int piCeil(const T & v) {return v < T(0) ? int(v) :
* - \c piAbsll for "llong"
* - \c piAbsf for "float"
* - \c piAbsd for "double"
*
*
* Example:
* \snippet piincludes.cpp abs */
template<typename T> inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);}
@@ -313,7 +313,7 @@ template<typename T> inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);}
* - \c piMinll for "llong"
* - \c piMinf for "float"
* - \c piMind for "double"
*
*
* Example:
* \snippet piincludes.cpp min2 */
template<typename T> inline T piMin(const T & f, const T & s) {return ((f > s) ? s : f);}
@@ -326,7 +326,7 @@ template<typename T> inline T piMin(const T & f, const T & s) {return ((f > s) ?
* - \c piMinll for "llong"
* - \c piMinf for "float"
* - \c piMind for "double"
*
*
* Example:
* \snippet piincludes.cpp min3 */
template<typename T> inline T piMin(const T & f, const T & s, const T & t) {return ((f < s && f < t) ? f : ((s < t) ? s : t));}
@@ -339,7 +339,7 @@ template<typename T> inline T piMin(const T & f, const T & s, const T & t) {retu
* - \c piMaxll for "llong"
* - \c piMaxf for "float"
* - \c piMaxd for "double"
*
*
* Example:
* \snippet piincludes.cpp max2 */
template<typename T> inline T piMax(const T & f, const T & s) {return ((f < s) ? s : f);}
@@ -352,7 +352,7 @@ template<typename T> inline T piMax(const T & f, const T & s) {return ((f < s) ?
* - \c piMaxll for "llong"
* - \c piMaxf for "float"
* - \c piMaxd for "double"
*
*
* Example:
* \snippet piincludes.cpp max3 */
template<typename T> inline T piMax(const T & f, const T & s, const T & t) {return ((f > s && f > t) ? f : ((s > t) ? s : t));}
@@ -366,7 +366,7 @@ template<typename T> inline T piMax(const T & f, const T & s, const T & t) {retu
* - \c piClampll for "llong"
* - \c piClampf for "float"
* - \c piClampd for "double"
*
*
* Example:
* \snippet piincludes.cpp clamp */
template<typename T> inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));}
@@ -387,7 +387,7 @@ template<typename T> inline void piLetobe(T * v) {piLetobe(v, sizeof(T));}
* - \c piLetobei for "uint"
* - \c piLetobel for "ulong"
* - \c piLetobell for "ullong"
*
*
* Example:
* \snippet piincludes.cpp letobe */
template<typename T> inline T piLetobe(const T & v) {T tv(v); piLetobe(&tv, sizeof(T)); return tv;}
@@ -395,6 +395,11 @@ template<typename T> inline T piLetobe(const T & v) {T tv(v); piLetobe(&tv, size
// specialization
template<> inline ushort piLetobe(const ushort & v) {return (v << 8) | (v >> 8);}
template<> inline uint piLetobe(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);}
template<> inline float piLetobe(const float & f) {
uint v = *((uint *)&f);
v = (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);
return *((float *)&v);
}
DEPRECATED inline ushort letobe_s(const ushort & v) {return (v << 8) | (v >> 8);}
DEPRECATED inline uint letobe_i(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);}
@@ -443,6 +448,7 @@ uint letobe_i(uint v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF
#define piLetobei piLetobe<uint>
#define piLetobel piLetobe<ulong>
#define piLetobell piLetobe<ullong>
#define piLetobef piLetobe<float>
#endif // PIBASE_H