OSDN Git Service

[FMGEN] Revert before commit.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmgen / misc.h
1 // ---------------------------------------------------------------------------
2 //      misc.h
3 //      Copyright (C) cisc 1998, 1999.
4 // ---------------------------------------------------------------------------
5 //      $Id: misc.h,v 1.5 2002/05/31 09:45:20 cisc Exp $
6
7 #ifndef __FMGEN_MISC_H
8 #define __FMGEN_MISC_H
9 #include <algorithm>
10
11 inline int Max(int x, int y) { return std::max(x, y); }
12 inline int Min(int x, int y) { return std::min(x, y); }
13
14 inline int Abs(int x) { return x >= 0 ? x : -x; }
15
16 //#if defined(__cplusplus) && (__cplusplus >= 201703L)
17 //#define Limit(foo, max, min) std::clamp((int)foo, (int)min, (int)max)
18 //#else
19 inline int Limit(int v, int max, int min) 
20 {
21         return v > max ? max : (v < min ? min : v); 
22 }
23 //#endif
24
25 #if defined(__has_builtin) && (__has_builtin(__builtin_bswap32))
26 inline unsigned int BSwap(unsigned int a)
27 {
28         return __builtin_bswap32(a);
29 }
30 #else
31 inline unsigned int BSwap(unsigned int a)
32 {
33         return (a >> 24) | ((a >> 8) & 0xff00) | ((a << 8) & 0xff0000) | (a << 24);
34 }
35 #endif
36
37 inline unsigned int NtoBCD(unsigned int a)
38 {
39         return ((a / 10) << 4) + (a % 10);
40 }
41
42 inline unsigned int BCDtoN(unsigned int v)
43 {
44         return (v >> 4) * 10 + (v & 15);
45 }
46
47
48 #if defined(__cplusplus) && (__cplusplus >= 201703L)
49 #include <numeric>
50 using std::gcd;
51 #else
52 template<class T>
53 inline T gcd(T x, T y)
54 {
55         T t;
56         while (y)
57         {
58                 t = x % y;
59                 x = y;
60                 y = t;
61         }
62         return x;
63 }
64
65 #endif
66
67 template<class T>
68 T bessel0(T x)
69 {
70         T p, r, s;
71
72         r = 1.0;
73         s = 1.0;
74         p = (x / 2.0) / s;
75
76         while (p > 1.0E-10)
77         {
78                 r += p * p;
79                 s += 1.0;
80                 p *= (x / 2.0) / s;
81         }
82         return r;
83 }
84
85
86 #endif // MISC_H
87