OpenJPH
Open-source implementation of JPEG2000 Part-15
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 #include <cmath>
45 
46 #include "ojph_defs.h"
47 
48 namespace ojph {
49 
51  // preprocessor directives for compiler
53  #ifdef _MSC_VER
54  #define OJPH_COMPILER_MSVC
55  #elif (defined __GNUC__)
56  #define OJPH_COMPILER_GNUC
57  #endif
58 
60  // cpu features
62  int cpu_ext_level();
63 
65  #ifdef OJPH_COMPILER_MSVC
66  #include <intrin.h>
67  #endif
68 
70  static inline ui32 population_count(ui32 val)
71  {
72  #ifdef OJPH_COMPILER_MSVC
73  return (ui32)__popcnt(val);
74  #elif (defined OJPH_COMPILER_GNUC)
75  return (ui32)__builtin_popcount(val);
76  #else
77  val -= ((val >> 1) & 0x55555555);
78  val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
79  val = (((val >> 4) + val) & 0x0f0f0f0f);
80  val += (val >> 8);
81  val += (val >> 16);
82  return (int)(val & 0x0000003f);
83  #endif
84  }
85 
87 #ifdef OJPH_COMPILER_MSVC
88  #pragma intrinsic(_BitScanReverse)
89 #endif
90  static inline ui32 count_leading_zeros(ui32 val)
91  {
92  #ifdef OJPH_COMPILER_MSVC
93  unsigned long result = 0;
94  _BitScanReverse(&result, val);
95  return 31 ^ (ui32)result;
96  #elif (defined OJPH_COMPILER_GNUC)
97  return (ui32)__builtin_clz(val);
98  #else
99  val |= (val >> 1);
100  val |= (val >> 2);
101  val |= (val >> 4);
102  val |= (val >> 8);
103  val |= (val >> 16);
104  return 32 - population_count(val);
105  #endif
106  }
107 
109 #ifdef OJPH_COMPILER_MSVC
110  #pragma intrinsic(_BitScanForward)
111 #endif
112  static inline ui32 count_trailing_zeros(ui32 val)
113  {
114  #ifdef OJPH_COMPILER_MSVC
115  unsigned long result = 0;
116  _BitScanForward(&result, val);
117  return (ui32)result;
118  #elif (defined OJPH_COMPILER_GNUC)
119  return (ui32)__builtin_ctz(val);
120  #else
121  val |= (val << 1);
122  val |= (val << 2);
123  val |= (val << 4);
124  val |= (val << 8);
125  val |= (val << 16);
126  return 32 - population_count(val);
127  #endif
128  }
129 
131  static inline si32 ojph_round(float val)
132  {
133  #ifdef OJPH_COMPILER_MSVC
134  return (si32)(val + (val >= 0.0f ? 0.5f : -0.5f));
135  #elif (defined OJPH_COMPILER_GNUC)
136  return (si32)(val + (val >= 0.0f ? 0.5f : -0.5f));
137  #else
138  return (si32)round(val);
139  #endif
140  }
141 
143  static inline si32 ojph_trunc(float val)
144  {
145  #ifdef OJPH_COMPILER_MSVC
146  return (si32)(val);
147  #elif (defined OJPH_COMPILER_GNUC)
148  return (si32)(val);
149  #else
150  return (si32)trunc(val);
151  #endif
152  }
153 
155  // constants
157  const ui32 byte_alignment = 32; //32 bytes == 256 bits
160 
162  // templates for alignment
164 
166  // finds the size such that it is a multiple of byte_alignment
167  template <typename T, int N>
168  size_t calc_aligned_size(size_t size) {
169  size = size * sizeof(T) + N - 1;
170  size &= ~((1ULL << (31 - count_leading_zeros(N))) - 1);
171  size >>= (31 - count_leading_zeros(sizeof(T)));
172  return size;
173  }
174 
176  // moves the pointer to first address that is a multiple of byte_alignment
177  template <typename T, int N>
178  inline T *align_ptr(T *ptr) {
179  intptr_t p = reinterpret_cast<intptr_t>(ptr);
180  p += N - 1;
181  p &= ~((1ULL << (31 - count_leading_zeros(N))) - 1);
182  return reinterpret_cast<T *>(p);
183  }
184 
186  // OS detection definitions
188 #if (defined WIN32) || (defined _WIN32) || (defined _WIN64)
189  #define OJPH_OS_WINDOWS
190 #elif (defined __APPLE__)
191  #define OJPH_OS_APPLE
192 #elif (defined __linux)
193  #define OJPH_OS_LINUX
194 #endif
195 
197  // defines for dll
199 #ifdef OJPH_OS_WINDOWS
200  #define OJPH_EXPORT __declspec(dllexport)
201 #else
202  #define OJPH_EXPORT
203 #endif
204 
205 }
206 
207 #endif // !OJPH_ARCH_H
const ui32 object_alignment
Definition: ojph_arch.h:159
const ui32 byte_alignment
Definition: ojph_arch.h:157
static si32 ojph_round(float val)
Definition: ojph_arch.h:131
size_t calc_aligned_size(size_t size)
Definition: ojph_arch.h:168
static ui32 population_count(ui32 val)
Definition: ojph_arch.h:70
static si32 ojph_trunc(float val)
Definition: ojph_arch.h:143
int cpu_ext_level()
Definition: ojph_arch.cpp:170
T * align_ptr(T *ptr)
Definition: ojph_arch.h:178
static ui32 count_trailing_zeros(ui32 val)
Definition: ojph_arch.h:112
static ui32 count_leading_zeros(ui32 val)
Definition: ojph_arch.h:90
int32_t si32
Definition: ojph_defs.h:55
const ui32 log_byte_alignment
Definition: ojph_arch.h:158
uint32_t ui32
Definition: ojph_defs.h:54