Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Utilities.hpp
1/*
2Copyright (C) 2016-2019,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGAMES_UTILITIES_H
17#define AEONGAMES_UTILITIES_H
19#include <string>
20#include <cstdint>
21
22namespace AeonGames
23{
28 DLL const std::string GetFileExtension ( const std::string& aFilePath );
33 DLL bool FileExists ( const std::string& aFilePath );
35 constexpr uint32_t DeBruijnSequence[32] =
36 {
37 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
38 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
39 };
40
44 constexpr uint32_t ffs ( uint32_t value )
45 {
46 return DeBruijnSequence[ ( ( ( value & ( -static_cast<int64_t> ( value ) ) ) * 0x077CB531ULL ) & 0xFFFFFFFF ) >> 27];
47 }
48 static_assert ( ffs ( 0x1 ) == 0, "Find First Bit Set Failure." );
49 static_assert ( ffs ( 0x2 ) == 1, "Find First Bit Set Failure." );
50 static_assert ( ffs ( 0x4 ) == 2, "Find First Bit Set Failure." );
51 static_assert ( ffs ( 0x8 ) == 3, "Find First Bit Set Failure." );
52 static_assert ( ffs ( 0x10 ) == 4, "Find First Bit Set Failure." );
53 static_assert ( ffs ( 0x20 ) == 5, "Find First Bit Set Failure." );
54 static_assert ( ffs ( 0x40 ) == 6, "Find First Bit Set Failure." );
55 static_assert ( ffs ( 0x80 ) == 7, "Find First Bit Set Failure." );
56 static_assert ( ffs ( 0x100 ) == 8, "Find First Bit Set Failure." );
57 static_assert ( ffs ( 0x200 ) == 9, "Find First Bit Set Failure." );
58 static_assert ( ffs ( 0x400 ) == 10, "Find First Bit Set Failure." );
59 static_assert ( ffs ( 0x800 ) == 11, "Find First Bit Set Failure." );
60 static_assert ( ffs ( 0x1000 ) == 12, "Find First Bit Set Failure." );
61 static_assert ( ffs ( 0x2000 ) == 13, "Find First Bit Set Failure." );
62 static_assert ( ffs ( 0x4000 ) == 14, "Find First Bit Set Failure." );
63 static_assert ( ffs ( 0x8000 ) == 15, "Find First Bit Set Failure." );
64 static_assert ( ffs ( 0x10000 ) == 16, "Find First Bit Set Failure." );
65 static_assert ( ffs ( 0x20000 ) == 17, "Find First Bit Set Failure." );
66 static_assert ( ffs ( 0x40000 ) == 18, "Find First Bit Set Failure." );
67 static_assert ( ffs ( 0x80000 ) == 19, "Find First Bit Set Failure." );
68 static_assert ( ffs ( 0x100000 ) == 20, "Find First Bit Set Failure." );
69 static_assert ( ffs ( 0x200000 ) == 21, "Find First Bit Set Failure." );
70 static_assert ( ffs ( 0x400000 ) == 22, "Find First Bit Set Failure." );
71 static_assert ( ffs ( 0x800000 ) == 23, "Find First Bit Set Failure." );
72 static_assert ( ffs ( 0x1000000 ) == 24, "Find First Bit Set Failure." );
73 static_assert ( ffs ( 0x2000000 ) == 25, "Find First Bit Set Failure." );
74 static_assert ( ffs ( 0x4000000 ) == 26, "Find First Bit Set Failure." );
75 static_assert ( ffs ( 0x8000000 ) == 27, "Find First Bit Set Failure." );
76 static_assert ( ffs ( 0x10000000 ) == 28, "Find First Bit Set Failure." );
77 static_assert ( ffs ( 0x20000000 ) == 29, "Find First Bit Set Failure." );
78 static_assert ( ffs ( 0x40000000 ) == 30, "Find First Bit Set Failure." );
79 static_assert ( ffs ( 0x80000000 ) == 31, "Find First Bit Set Failure." );
80 static_assert ( ffs ( ~0x1 ) == 1, "Find First Bit Set Failure." );
81 static_assert ( ffs ( ~0x3 ) == 2, "Find First Bit Set Failure." );
82 static_assert ( ffs ( ~0x7 ) == 3, "Find First Bit Set Failure." );
83 static_assert ( ffs ( ~0xf ) == 4, "Find First Bit Set Failure." );
84 static_assert ( ffs ( ~0x1f ) == 5, "Find First Bit Set Failure." );
85 static_assert ( ffs ( ~0x3f ) == 6, "Find First Bit Set Failure." );
86 static_assert ( ffs ( ~0x7f ) == 7, "Find First Bit Set Failure." );
87 static_assert ( ffs ( ~0xff ) == 8, "Find First Bit Set Failure." );
88 static_assert ( ffs ( ~0x1ff ) == 9, "Find First Bit Set Failure." );
89 static_assert ( ffs ( ~0x3ff ) == 10, "Find First Bit Set Failure." );
90 static_assert ( ffs ( ~0x7ff ) == 11, "Find First Bit Set Failure." );
91 static_assert ( ffs ( ~0xfff ) == 12, "Find First Bit Set Failure." );
92 static_assert ( ffs ( ~0x1fff ) == 13, "Find First Bit Set Failure." );
93 static_assert ( ffs ( ~0x3fff ) == 14, "Find First Bit Set Failure." );
94 static_assert ( ffs ( ~0x7fff ) == 15, "Find First Bit Set Failure." );
95 static_assert ( ffs ( ~0xffff ) == 16, "Find First Bit Set Failure." );
96 static_assert ( ffs ( ~0x1ffff ) == 17, "Find First Bit Set Failure." );
97 static_assert ( ffs ( ~0x3ffff ) == 18, "Find First Bit Set Failure." );
98 static_assert ( ffs ( ~0x7ffff ) == 19, "Find First Bit Set Failure." );
99 static_assert ( ffs ( ~0xfffff ) == 20, "Find First Bit Set Failure." );
100 static_assert ( ffs ( ~0x1fffff ) == 21, "Find First Bit Set Failure." );
101 static_assert ( ffs ( ~0x3fffff ) == 22, "Find First Bit Set Failure." );
102 static_assert ( ffs ( ~0x7fffff ) == 23, "Find First Bit Set Failure." );
103 static_assert ( ffs ( ~0xffffff ) == 24, "Find First Bit Set Failure." );
104 static_assert ( ffs ( ~0x1ffffff ) == 25, "Find First Bit Set Failure." );
105 static_assert ( ffs ( ~0x3ffffff ) == 26, "Find First Bit Set Failure." );
106 static_assert ( ffs ( ~0x7ffffff ) == 27, "Find First Bit Set Failure." );
107 static_assert ( ffs ( ~0xfffffff ) == 28, "Find First Bit Set Failure." );
108 static_assert ( ffs ( ~0x1fffffff ) == 29, "Find First Bit Set Failure." );
109 static_assert ( ffs ( ~0x3fffffff ) == 30, "Find First Bit Set Failure." );
110 static_assert ( ffs ( ~0x7fffffff ) == 31, "Find First Bit Set Failure." );
111
116 constexpr uint32_t popcount ( uint32_t v )
117 {
118 return ( ( ( ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) & 0x33333333 ) +
119 ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) >> 2 ) & 0x33333333 ) ) +
120 ( ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) & 0x33333333 ) +
121 ( ( ( v - ( ( v >> 1 ) & 0x55555555 ) ) >> 2 ) & 0x33333333 ) ) >> 4 ) ) &
122 0xF0F0F0F ) * 0x1010101 ) >> 24;
123 }
124 static_assert ( popcount ( 0x0 ) == 0, "Popcount Failure." );
125 static_assert ( popcount ( 0x1 ) == 1, "Popcount Failure." );
126 static_assert ( popcount ( 0x2 ) == 1, "Popcount Failure." );
127 static_assert ( popcount ( 0x3 ) == 2, "Popcount Failure." );
128 static_assert ( popcount ( 0x4 ) == 1, "Popcount Failure." );
129 static_assert ( popcount ( 0x5 ) == 2, "Popcount Failure." );
130 static_assert ( popcount ( 0x6 ) == 2, "Popcount Failure." );
131 static_assert ( popcount ( 0x7 ) == 3, "Popcount Failure." );
132 static_assert ( popcount ( 0x8 ) == 1, "Popcount Failure." );
133 static_assert ( popcount ( 0x9 ) == 2, "Popcount Failure." );
134 static_assert ( popcount ( 0xa ) == 2, "Popcount Failure." );
135 static_assert ( popcount ( 0xb ) == 3, "Popcount Failure." );
136 static_assert ( popcount ( 0xc ) == 2, "Popcount Failure." );
137 static_assert ( popcount ( 0xd ) == 3, "Popcount Failure." );
138 static_assert ( popcount ( 0xe ) == 3, "Popcount Failure." );
139 static_assert ( popcount ( 0xf ) == 4, "Popcount Failure." );
147 template<class T, class U>
148 const T Concatenate ( const T& aFirst, U& aSecond )
149 {
150 T result ( aFirst.begin(), aFirst.end() );
151 result.insert ( result.end(), aSecond.begin(), aSecond.end() );
152 return result;
153 }
154
157 {
158 public:
165 DLL OptionHandler ( const char aShortOption, const char* aLongOption, void ( *aHandler ) ( const char*, void* ), void* aUserData = nullptr );
166 DLL ~OptionHandler();
168 DLL const char GetShortOption() const;
170 DLL const char* GetLongOption() const;
172 DLL void* GetUserData() const;
177 DLL void operator() ( const char* aArgument, void* aUserData ) const;
178 private:
179 void ( *mHandler ) ( const char*, void* );
180 const char mShortOption{};
181 const char* mLongOption{nullptr};
182 void* mUserData{nullptr};
183 };
184
190 DLL void ProcessOpts ( int argc, char *argv[], const OptionHandler* aOptionHandler, size_t aOptionHandlerCount );
191}
192#endif
Platform-specific macros, includes, and DLL export/import definitions.
Command-line option handler.
DLL void * GetUserData() const
Get the user data pointer.
Definition Utilities.cpp:58
DLL const char * GetLongOption() const
Get the long option string.
Definition Utilities.cpp:54
DLL const char GetShortOption() const
Get the short option character.
Definition Utilities.cpp:50
DLL void operator()(const char *aArgument, void *aUserData) const
Invoke the handler callback.
Definition Utilities.cpp:62
DLL OptionHandler(const char aShortOption, const char *aLongOption, void(*aHandler)(const char *, void *), void *aUserData=nullptr)
Construct an OptionHandler.
Definition Utilities.cpp:44
<- This is here just for the literals
Definition AABB.hpp:31
DLL void ProcessOpts(int argc, char *argv[], const OptionHandler *aOptionHandler, size_t aOptionHandlerCount)
Process command-line options.
Definition Utilities.cpp:67
const T Concatenate(const T &aFirst, U &aSecond)
Concatenate two containers.
DLL bool FileExists(const std::string &aFilePath)
Check whether a file exists at the given path.
Definition Utilities.cpp:38
constexpr uint32_t ffs(uint32_t value)
Find the index of the first set bit using a De Bruijn sequence.
Definition Utilities.hpp:44
DLL const std::string GetFileExtension(const std::string &aFilePath)
Extract the file extension from a file path.
Definition Utilities.cpp:23
constexpr uint32_t DeBruijnSequence[32]
De Bruijn lookup table used by ffs().
Definition Utilities.hpp:35
constexpr uint32_t popcount(uint32_t v)
Count the number of set bits in a 32-bit unsigned integer.