Grok  9.7.5
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  #ifdef _MSC_VER
53  #define OJPH_COMPILER_MSVC
54  #elif (defined __GNUC__)
55  #define OJPH_COMPILER_GNUC
56  #endif
57 
58  #ifdef OJPH_COMPILER_MSVC
59  #include <intrin.h>
60  #endif
61 
62  static inline ui32 population_count(ui32 val)
63  {
64  #ifdef OJPH_COMPILER_MSVC
65  return (ui32)__popcnt(val);
66  #elif (defined OJPH_COMPILER_GNUC)
67  return (ui32)__builtin_popcount(val);
68  #else
69  val -= ((val >> 1) & 0x55555555);
70  val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
71  val = (((val >> 4) + val) & 0x0f0f0f0f);
72  val += (val >> 8);
73  val += (val >> 16);
74  return (int)(val & 0x0000003f);
75  #endif
76  }
77 
79 #ifdef OJPH_COMPILER_MSVC
80  #pragma intrinsic(_BitScanReverse)
81 #endif
82  static inline ui32 count_leading_zeros(ui32 val)
83  {
84  #ifdef OJPH_COMPILER_MSVC
85  unsigned long result = 0;
86  _BitScanReverse(&result, val);
87  return 31 ^ (ui32)result;
88  #elif (defined OJPH_COMPILER_GNUC)
89  return (ui32)__builtin_clz(val);
90  #else
91  val |= (val >> 1);
92  val |= (val >> 2);
93  val |= (val >> 4);
94  val |= (val >> 8);
95  val |= (val >> 16);
96  return 32 - population_count(val);
97  #endif
98  }
99 
101  // constants
103  const ui32 byte_alignment = 32; //32 bytes == 256 bits
106 
108  // finds the size such that it is a multiple of byte_alignment
109  template <typename T, int N>
110  size_t calc_aligned_size(size_t size) {
111  size = size * sizeof(T) + N - 1;
112  size &= ~((1ULL << (31 - count_leading_zeros(N))) - 1);
113  size >>= (31 - count_leading_zeros(sizeof(T)));
114  return size;
115  }
116 
118  // moves the pointer to first address that is a multiple of byte_alignment
119  template <typename T, int N>
120  inline T *align_ptr(T *ptr) {
121  intptr_t p = reinterpret_cast<intptr_t>(ptr);
122  p += N - 1;
123  p &= ~((intptr_t)(1ULL << (31 - count_leading_zeros(N))) - 1);
124  return reinterpret_cast<T *>(p);
125  }
126 
127 }
128 
129 #endif // !OJPH_ARCH_H
N
Definition: rvv-inl.h:1656
Definition: ojph_block_decoder.cpp:50
const ui32 object_alignment
Definition: ojph_arch.h:105
const ui32 byte_alignment
Definition: ojph_arch.h:103
size_t calc_aligned_size(size_t size)
Definition: ojph_arch.h:110
static ui32 population_count(ui32 val)
Definition: ojph_arch.h:62
T * align_ptr(T *ptr)
Definition: ojph_arch.h:120
static ui32 count_leading_zeros(ui32 val)
Definition: ojph_arch.h:82
const ui32 log_byte_alignment
Definition: ojph_arch.h:104
uint32_t ui32
Definition: ojph_defs.h:50