rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

math.h (2517B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef MATH_H
     17 #define MATH_H
     18 
     19 #include "rsys.h"
     20 #include <float.h>
     21 #include <math.h>
     22 
     23 #define MMAX(A, B) ((A) > (B) ? (A) : (B))
     24 #define MMIN(A, B) ((A) < (B) ? (A) : (B))
     25 #define CLAMP(A, Min, Max) MMIN(MMAX(Min, A), Max)
     26 #define IS_POW2(A) (((A) & ((A)-1)) == 0 && (A) > 0)
     27 #define INF (DBL_MAX + DBL_MAX)
     28 #define NaN (-(INF*0))
     29 #define IS_INF(X) ((X==INF) || (X==-INF))
     30 #define IS_NaN(X) (!((X)==(X)))
     31 #define PI 3.14159265358979323846
     32 #define RCP_PI 0.31830988618379067154	/* 1/pi */
     33 #define SQRT2 1.41421356237309504880 /* sqrt(2) */
     34 #define MDEG2RAD(Deg) ((Deg)*PI/180.0)
     35 #define MRAD2DEG(Rad) ((Rad)*180.0/PI)
     36 
     37 static FINLINE size_t
     38 round_up_pow2(const size_t i)
     39 {
     40   if(IS_POW2(i)) {
     41     return i;
     42   } else if(!i) {
     43     return 1;
     44   } else {
     45     size_t j = i - 1;
     46     unsigned k;
     47     for(k = 1; k < sizeof(int)*8; k <<= 1)
     48       j |= j >> k;
     49     return j + 1;
     50   }
     51 }
     52 
     53 static FINLINE int
     54 log2i(const int i)
     55 {
     56   union { float f; int32_t i; } ucast;
     57   ASSERT(i != 0);
     58   ucast.f = (float)i;
     59   return ((ucast.i>>23/*#bits mantissa*/) & ((1<<8/*#bits exponent*/)-1)) - 127;
     60 }
     61 
     62 static NOINLINE float
     63 absf(const float flt)
     64 {
     65   union { float f; int32_t i; } ucast;
     66   ucast.f = flt;
     67   ucast.i &= 0x7FFFFFFF;
     68   return ucast.f;
     69 }
     70 
     71 static FINLINE float
     72 signf(const float flt)
     73 {
     74   return flt < 0.f ? -1.f : 1.f;
     75 }
     76 
     77 static FINLINE double
     78 sign(const double dbl)
     79 {
     80   return dbl < 0.0 ? -1.0 : 1.0;
     81 }
     82 
     83 static FINLINE char
     84 eq_eps(const double a, const double b, const double eps)
     85 {
     86   return fabs(a - b) <= eps;
     87 }
     88 
     89 static FINLINE char
     90 eq_epsf(const float a, const float b, const float eps)
     91 {
     92   return absf(a - b) <= eps;
     93 }
     94 
     95 static FINLINE double
     96 sin2cos(const double d)
     97 {
     98   return sqrt(MMAX(0.0, 1.0 - d*d));
     99 }
    100 
    101 static FINLINE double
    102 cos2sin(const double d)
    103 {
    104   return sin2cos(d);
    105 }
    106 
    107 #endif /* MATH_H */