~vladh/peony

peony/src/pack.cpp -rw-r--r-- 828 bytes
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "pack.hpp"


void pack::init(Pack *pack) {
  *pack = 0ULL;
}


void pack::set(Pack *pack, uint8 value, uint8 idx) {
  assert(idx < MAX_LENGTH);
  Pack mask = 0b1111ULL << (idx * ELEMENT_SIZE);
  *pack = (*pack & ~mask) | (((Pack)value << (idx * ELEMENT_SIZE)) & mask);
}


uint8 pack::get(Pack *pack, uint8 idx) {
  Pack mask = 0b1111ULL << (idx * ELEMENT_SIZE);
  return (uint8)((*pack & mask) >> (idx * ELEMENT_SIZE));
}


uint8 pack::get_count(Pack *pack) {
  return get(pack, COUNTER_IDX);
}


void pack::push(Pack *pack, uint8 value) {
  uint8 count = get_count(pack);
  // Last element is reserved for count;
  assert(count < COUNTER_IDX);
  set(pack, value, count);
  set(pack, count + 1, COUNTER_IDX);
}