cgv
Loading...
Searching...
No Matches
packing_info.cxx
1#include <cgv/utils/bit_operations.h>
2#include "packing_info.h"
3
4using namespace cgv::utils;
5
6namespace cgv {
7 namespace data {
8size_t packing_info::align(size_t v, unsigned a)
9{
10 return a*(v/a) == v ? v : a*((v/a)+1);
11}
12packing_info::packing_info(unsigned align, unsigned d0, unsigned d1, unsigned d2, unsigned d3)
13 : is_packed(true), bd0(d0), bd1(d1), bd2(d2), bd3(d3), ca(align)
14{
15 if (d0 == 0)
16 is_packed = false;
17 else if (d1 == 0)
18 bd1 = bd2 = bd3 = d0;
19}
21{
22 is_packed = false;
23 ca = 1;
24 bd0 = bd1 = bd2 = bd3 = 0;
25}
26unsigned packing_info::get_bit_depth(unsigned ci) const
27{
28 return ci<2 ? (ci==0?bd0:bd1) : (ci==2?bd2:bd3);
29}
30void packing_info::set_bit_depth(unsigned ci, unsigned depth)
31{
32 if (ci<2) if (ci == 0) bd0 = depth; else bd1 = depth;
33 else if (ci == 2) bd2 = depth; else bd3 = depth;
34}
36{
37 return is_packed;
38}
39void packing_info::set_packing(bool enable)
40{
41 is_packed = enable;
42}
44{
45 return ca;
46}
48{
49 ca = a;
50}
51bool packing_info::prepare_bit_operation(unsigned ci, void* ptr, unsigned &off, unsigned &bd, unsigned* &iptr) const
52{
53 off = get_bit_offset(ci);
54 bd = get_bit_depth(ci);
55 ptr = static_cast<unsigned char*>(ptr)+off/8;
56 off -= 8*(off/8);
57 iptr = static_cast<unsigned*>(ptr);
58 return true;
59};
60bool packing_info::prepare_bit_operation(unsigned ci, const void* ptr, unsigned &off, unsigned &bd, const unsigned* &iptr) const
61{
62 off = get_bit_offset(ci);
63 bd = get_bit_depth(ci);
64 ptr = static_cast<const unsigned char*>(ptr)+off/8;
65 off -= 8*(off/8);
66 iptr = static_cast<const unsigned*>(ptr);
67 return true;
68};
69unsigned packing_info::get_bit_offset(unsigned ci) const
70{
71 unsigned off = 0;
72 for (unsigned i = 0; i < ci; ++i)
73 off += unsigned(align(get_bit_depth(i),get_component_alignment()));
74 return off;
75}
76int packing_info::get_signed(unsigned ci, const void* ptr) const
77{
78 unsigned off, bd;
79 const unsigned *iptr;
80 prepare_bit_operation(ci, ptr, off, bd, iptr);
81 unsigned int i = *iptr >> off;
82 if (is_bit_set(bd-1,i))
83 enable_upper_bits(i, bd);
84 else
85 disable_upper_bits(i, bd);
86 return (int) i;
87}
88unsigned packing_info::get_unsigned(unsigned ci, const void* ptr) const
89{
90 unsigned off, bd;
91 const unsigned *iptr;
92 prepare_bit_operation(ci, ptr, off, bd, iptr);
93 unsigned i = *iptr >> off;
94 disable_upper_bits(i, bd);
95 return i;
96}
97bool packing_info::set_signed(unsigned ci, void* ptr, int v) const
98{
99 unsigned off, bd, *iptr;
100 prepare_bit_operation(ci, ptr, off, bd, iptr);
101 set_bits(*iptr, off, bd, (const unsigned&) v);
102 return true;
103}
104bool packing_info::set_unsigned(unsigned ci, void* ptr, unsigned v) const
105{
106 unsigned off, bd, *iptr;
107 prepare_bit_operation(ci, ptr, off, bd, iptr);
108 set_bits(*iptr, off, bd, v);
109 return true;
110}
112{
113 if (is_packing() != pi.is_packing())
114 return false;
116 return false;
117 for (unsigned int i=0; i<4; ++i)
118 if (get_bit_depth(i) != pi.get_bit_depth(i))
119 return false;
120 return true;
121}
123{
124 return !(*this == pi);
125}
126 }
127}
the packing_info class stores information about packed integers structures.
static size_t align(size_t v, unsigned a)
return the next integer larger or equal to v which is dividable by a
bool is_packing() const
return whether packing is enabled
void set_bit_depth(unsigned ci, unsigned depth)
set the bit depth of the ci-th component
int get_signed(unsigned ci, const void *ptr) const
return the ci-th component of the data entry pointed to by the given pointer of a signed packed compo...
bool operator!=(const packing_info &pi) const
unequal comparison
packing_info(unsigned align=1, unsigned d0=0, unsigned d1=0, unsigned d2=0, unsigned d3=0)
construct packing information from alignment and bit depths.
unsigned get_bit_depth(unsigned ci) const
return the bit depth of the ci-th component
unsigned int get_unsigned(unsigned ci, const void *ptr) const
return the ci-th component of the data entry pointed to by the given pointer of an unsigned packed co...
unsigned int get_component_alignment() const
return the component alignment in bits in the packed case and in bytes in the unpacked case
void set_packing(bool enable=true)
set the packing flag
void set_component_alignment(unsigned a)
set the component alignment in bits in the packed case and in bytes in the unpacked case
bool set_unsigned(unsigned ci, void *ptr, unsigned v) const
set the ci-th component of the data entry pointed to by the given pointer of an unsigned packed compo...
bool set_signed(unsigned ci, void *ptr, int v) const
set the ci-th component of the data entry pointed to by the given pointer of a signed packed componen...
bool operator==(const packing_info &pi) const
equal comparison
void clear()
clear packing info information
namespace that holds tools that dont fit any other namespace
bool is_bit_set(unsigned int bit_idx, unsigned int bit_field)
check if a bit of a bit field is set
void enable_upper_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
set all the bits of bit_field with index equal or larger than fst_bit_idx
void disable_upper_bits(unsigned int &bit_field, unsigned int fst_bit_idx)
clear all the bits of bit_field with index equal or larger than fst_bit_idx
void set_bits(unsigned int &bit_field, unsigned int off, unsigned int n, unsigned int value)
set n bits starting with index off of the given bit field from the first bits of the given integer va...
the cgv namespace
Definition print.h:11