Otclient  14/8/2020
math.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef STDEXT_MATH_H
24 #define STDEXT_MATH_H
25 
26 #include <algorithm>
27 #include "types.h"
28 
29 namespace stdext {
30 
31 inline bool is_power_of_two(size_t v) { return ((v != 0) && !(v & (v - 1))); }
32 inline size_t to_power_of_two(size_t v) { if(v == 0) return 0; size_t r = 1; while(r < v && r != 0xffffffff) r <<= 1; return r; }
33 
34 inline uint16_t readULE16(const uchar *addr) { return (uint16_t)addr[1] << 8 | addr[0]; }
35 inline uint32_t readULE32(const uchar *addr) { return (uint32_t)readULE16(addr + 2) << 16 | readULE16(addr); }
36 inline uint64_t readULE64(const uchar *addr) { return (uint64_t)readULE32(addr + 4) << 32 | readULE32(addr); }
37 
38 inline void writeULE16(uchar *addr, uint16_t value) { addr[1] = value >> 8; addr[0] = (uint8_t)value; }
39 inline void writeULE32(uchar *addr, uint32_t value) { writeULE16(addr + 2, value >> 16); writeULE16(addr, (uint16_t)value); }
40 inline void writeULE64(uchar *addr, uint64_t value) { writeULE32(addr + 4, value >> 32); writeULE32(addr, (uint32_t)value); }
41 
42 inline int16_t readSLE16(const uchar *addr) { return (int16_t)addr[1] << 8 | addr[0]; }
43 inline int32_t readSLE32(const uchar *addr) { return (int32_t)readSLE16(addr + 2) << 16 | readSLE16(addr); }
44 inline int64_t readSLE64(const uchar *addr) { return (int64_t)readSLE32(addr + 4) << 32 | readSLE32(addr); }
45 
46 inline void writeSLE16(uchar *addr, int16_t value) { addr[1] = value >> 8; addr[0] = (int8_t)value; }
47 inline void writeSLE32(uchar *addr, int32_t value) { writeSLE16(addr + 2, value >> 16); writeSLE16(addr, (int16_t)value); }
48 inline void writeSLE64(uchar *addr, int64_t value) { writeSLE32(addr + 4, value >> 32); writeSLE32(addr, (int32_t)value); }
49 
50 uint32_t adler32(const uint8_t *buffer, size_t size);
51 
52 long random_range(long min, long max);
53 float random_range(float min, float max);
54 
55 double round(double r);
56 
57 template<typename T>
58 T clamp(T x, T min, T max) { return std::max<T>(min, std::min<T>(x, max)); }
59 
60 }
61 
62 #endif
stdext::readULE16
uint16_t readULE16(const uchar *addr)
Definition: math.h:34
types.h
stdext::is_power_of_two
bool is_power_of_two(size_t v)
Definition: math.h:31
stdext::writeULE64
void writeULE64(uchar *addr, uint64_t value)
Definition: math.h:40
stdext::round
double round(double r)
Definition: math.cpp:64
stdext::writeULE16
void writeULE16(uchar *addr, uint16_t value)
Definition: math.h:38
stdext::readSLE32
int32_t readSLE32(const uchar *addr)
Definition: math.h:43
stdext::writeULE32
void writeULE32(uchar *addr, uint32_t value)
Definition: math.h:39
stdext::writeSLE32
void writeSLE32(uchar *addr, int32_t value)
Definition: math.h:47
stdext::writeSLE16
void writeSLE16(uchar *addr, int16_t value)
Definition: math.h:46
stdext::adler32
uint32_t adler32(const uint8_t *buffer, size_t size)
Definition: math.cpp:32
stdext::clamp
T clamp(T x, T min, T max)
Definition: math.h:58
stdext::readULE64
uint64_t readULE64(const uchar *addr)
Definition: math.h:36
stdext::writeSLE64
void writeSLE64(uchar *addr, int64_t value)
Definition: math.h:48
uchar
unsigned char uchar
Definition: types.h:29
stdext::readSLE64
int64_t readSLE64(const uchar *addr)
Definition: math.h:44
stdext::random_range
long random_range(long min, long max)
Definition: math.cpp:48
stdext
Definition: any.h:30
stdext::to_power_of_two
size_t to_power_of_two(size_t v)
Definition: math.h:32
stdext::readULE32
uint32_t readULE32(const uchar *addr)
Definition: math.h:35
stdext::readSLE16
int16_t readSLE16(const uchar *addr)
Definition: math.h:42