Beatmup
fixed_point.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../basic_types.h"
21 #include <cstdint>
22 #include <limits>
23 
24 namespace Beatmup {
25 
26  /**
27  Fixed-point number.
28  \tparam rtype Datatype used to store the values
29  \tparam precision Number of bits to represent the fractional part
30  \tparam optype Datatype used in arithmetic operations
31  */
32  template<typename rtype, const int precision, typename optype>
33  struct Fixed {
34  protected:
35  rtype value;
36  inline static rtype clamp(const float val, const rtype min, const rtype max) {
37  return val <= min ? min : val >= max ? max : (rtype)roundf_fast(val);
38  }
39 
40  static inline rtype fromFloat(const float val) {
42  }
43 
44  Fixed(const rtype value) : value(((optype)value) << precision) {}
45 
46  public:
47  typedef rtype underlying_type;
48 
49  Fixed() : value(0) {}
50 
51  Fixed(const Fixed& another): value(another.value) {}
52 
54 
55  Fixed& operator=(const float value) {
56  this->value = fromFloat(value);
57  return *this;
58  }
59 
60  Fixed& operator=(const rtype value) {
61  this->value = ((optype)value) << precision;
62  return *this;
63  }
64 
65  Fixed operator+(const Fixed& another) const {
66  return Fixed::interpret( static_cast<rtype>(value + another.value) );
67  }
68 
69  Fixed operator-(const Fixed& another) const {
70  return Fixed::interpret( static_cast<rtype>(value - another.value) );
71  }
72 
73  Fixed operator*(const Fixed& another) const {
74  return Fixed::interpret( static_cast<rtype>(((optype)value * another.value) >> precision) );
75  }
76 
77  Fixed operator/(const Fixed& another) const {
78  return Fixed::interpret( static_cast<rtype>(((optype)value << precision) / another.value) );
79  }
80 
81 
82  operator float() const {
83  return (float)value / (float)(1 << precision);
84  }
85 
86 
87  uint8_t asTexture() const {
88  const optype v = (((optype)value) * 255) >> precision;
89  return v <= 0 ? 0 : v >= 255 ? 255 : (uint8_t)v;
90  }
91 
92 
94  Fixed result;
95  result.value = value;
96  return result;
97  }
98 
99 
100  /**
101  \return min representable value.
102  */
103  static float min() {
105  }
106 
107 
108  /**
109  \return max representable value.
110  */
111  static float max() {
113  }
114  };
115 
116 
117  /**
118  Signed 16-bit fixed point
119  With default precision represents [-1, 1) range.
120  */
121  template<const int precision = 15> using Fixed16 = Fixed<int16_t, precision, int32_t>;
122 }
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
Fixed-point number.
Definition: fixed_point.h:33
static float max()
Definition: fixed_point.h:111
Fixed operator/(const Fixed &another) const
Definition: fixed_point.h:77
Fixed operator+(const Fixed &another) const
Definition: fixed_point.h:65
Fixed & operator=(const rtype value)
Definition: fixed_point.h:60
Fixed & operator=(const float value)
Definition: fixed_point.h:55
Fixed(const Fixed &another)
Definition: fixed_point.h:51
Fixed operator-(const Fixed &another) const
Definition: fixed_point.h:69
Fixed operator*(const Fixed &another) const
Definition: fixed_point.h:73
static rtype fromFloat(const float val)
Definition: fixed_point.h:40
static float min()
Definition: fixed_point.h:103
Fixed(const rtype value)
Definition: fixed_point.h:44
static rtype clamp(const float val, const rtype min, const rtype max)
Definition: fixed_point.h:36
static Fixed interpret(const underlying_type value)
Definition: fixed_point.h:93
rtype underlying_type
Definition: fixed_point.h:47
uint8_t asTexture() const
Definition: fixed_point.h:87
Fixed(float value)
Definition: fixed_point.h:53
#define roundf_fast(X)
rounding (nearest integer)
Definition: utils.hpp:22
Beatmup::IntPoint result
return(jlong) new Beatmup jlong jstring jint val
JNIEnv jlong jfloat jfloat jfloat v