Grok  7.6.6
ojph_arch.h
Go to the documentation of this file.
1 //***************************************************************************/
2 // This software is released under the 2-Clause BSD license, included
3 // below.
4 //
5 // Copyright (c) 2019, Aous Naman
6 // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7 // Copyright (c) 2019, The University of New South Wales, Australia
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //***************************************************************************/
32 // This file is part of the OpenJPH software implementation.
33 // File: ojph_arch.h
34 // Author: Aous Naman
35 // Date: 28 August 2019
36 //***************************************************************************/
37 
38 
39 #ifndef OJPH_ARCH_H
40 #define OJPH_ARCH_H
41 
42 #include <cstdio>
43 #include <cstdint>
44 #ifdef _MSC_VER
45 #define _USE_MATH_DEFINES // for C++
46 #endif
47 #include <cmath>
48 
49 #include "ojph_defs.h"
50 
51 namespace ojph {
52 
54  // preprocessor directives for compiler
56  #ifdef _MSC_VER
57  #define OJPH_COMPILER_MSVC
58  #elif (defined __GNUC__)
59  #define OJPH_COMPILER_GNUC
60  #endif
61 
63  // cpu features
65  int cpu_ext_level();
66 
68  #ifdef OJPH_COMPILER_MSVC
69  #include <intrin.h>
70  #endif
71 
73  static inline ui32 population_count(ui32 val)
74  {
75  #ifdef OJPH_COMPILER_MSVC
76  return (ui32)__popcnt(val);
77  #elif (defined OJPH_COMPILER_GNUC)
78  return (ui32)__builtin_popcount(val);
79  #else
80  val -= ((val >> 1) & 0x55555555);
81  val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
82  val = (((val >> 4) + val) & 0x0f0f0f0f);
83  val += (val >> 8);
84  val += (val >> 16);
85  return (int)(val & 0x0000003f);
86  #endif
87  }
88 
90 #ifdef OJPH_COMPILER_MSVC
91  #pragma intrinsic(_BitScanReverse)
92 #endif
93  static inline ui32 count_leading_zeros(ui32 val)
94  {
95  #ifdef OJPH_COMPILER_MSVC
96  unsigned long result = 0;
97  _BitScanReverse(&result, val);
98  return 31 ^ (ui32)result;
99  #elif (defined OJPH_COMPILER_GNUC)
100  return (ui32)__builtin_clz(val);
101  #else
102  val |= (val >> 1);
103  val |= (val >> 2);
104  val |= (val >> 4);
105  val |= (val >> 8);
106  val |= (val >> 16);
107  return 32 - population_count(val);
108  #endif
109  }
110 
112 #ifdef OJPH_COMPILER_MSVC
113  #pragma intrinsic(_BitScanForward)
114 #endif
115  static inline ui32 count_trailing_zeros(ui32 val)
116  {
117  #ifdef OJPH_COMPILER_MSVC
118  unsigned long result = 0;
119  _BitScanForward(&result, val);
120  return (ui32)result;
121  #elif (defined OJPH_COMPILER_GNUC)
122  return (ui32)__builtin_ctz(val);
123  #else
124  val |= (val << 1);
125  val |= (val << 2);
126  val |= (val << 4);
127  val |= (val << 8);
128  val |= (val << 16);
129  return 32 - population_count(val);
130  #endif
131  }
132 
134  static inline si32 ojph_round(float val)
135  {
136  #ifdef OJPH_COMPILER_MSVC
137  return (si32)(val + (val >= 0.0f ? 0.5f : -0.5f));
138  #elif (defined OJPH_COMPILER_GNUC)
139  return (si32)(val + (val >= 0.0f ? 0.5f : -0.5f));
140  #else
141  return (si32)round(val);
142  #endif
143  }
144 
146  static inline si32 ojph_trunc(float val)
147  {
148  #ifdef OJPH_COMPILER_MSVC
149  return (si32)(val);
150  #elif (defined OJPH_COMPILER_GNUC)
151  return (si32)(val);
152  #else
153  return (si32)trunc(val);
154  #endif
155  }
156 
158  // constants
160  const ui32 byte_alignment = 32; //32 bytes == 256 bits
163 
165  // templates for alignment
167 
169  // finds the size such that it is a multiple of byte_alignment
170  template <typename T, int N>
171  size_t calc_aligned_size(size_t size) {
172  size = size * sizeof(T) + N - 1;
173  size &= ~((1ULL << (31 - count_leading_zeros(N))) - 1);
174  size >>= (31 - count_leading_zeros(sizeof(T)));
175  return size;
176  }
177 
179  // moves the pointer to first address that is a multiple of byte_alignment
180  template <typename T, int N>
181  inline T *align_ptr(T *ptr) {
182  intptr_t p = reinterpret_cast<intptr_t>(ptr);
183  p += N - 1;
184  p &= ~((intptr_t)(1ULL << (31 - count_leading_zeros(N))) - 1);
185  return reinterpret_cast<T *>(p);
186  }
187 
189  // OS detection definitions
191 #if (defined WIN32) || (defined _WIN32) || (defined _WIN64)
192  #define OJPH_OS_WINDOWS
193 #elif (defined __APPLE__)
194  #define OJPH_OS_APPLE
195 #elif (defined __linux)
196  #define OJPH_OS_LINUX
197 #endif
198 
200  // defines for dll
202 #ifdef OJPH_OS_WINDOWS
203  #define OJPH_EXPORT __declspec(dllexport)
204 #else
205  #define OJPH_EXPORT
206 #endif
207 
208 }
209 
210 #endif // !OJPH_ARCH_H
Definition: ojph_block_decoder.cpp:49
const ui32 object_alignment
Definition: ojph_arch.h:162
const ui32 byte_alignment
Definition: ojph_arch.h:160
static si32 ojph_round(float val)
Definition: ojph_arch.h:134
size_t calc_aligned_size(size_t size)
Definition: ojph_arch.h:171
static ui32 population_count(ui32 val)
Definition: ojph_arch.h:73
static si32 ojph_trunc(float val)
Definition: ojph_arch.h:146
int cpu_ext_level()
Definition: ojph_arch.cpp:170
T * align_ptr(T *ptr)
Definition: ojph_arch.h:181
static ui32 count_trailing_zeros(ui32 val)
Definition: ojph_arch.h:115
static ui32 count_leading_zeros(ui32 val)
Definition: ojph_arch.h:93
int32_t si32
Definition: ojph_defs.h:55
const ui32 log_byte_alignment
Definition: ojph_arch.h:161
uint32_t ui32
Definition: ojph_defs.h:54
Definition: ojph_base.h:48