The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
functions.c
Go to the documentation of this file.
1 
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 uint32_t crc_update(const uint64_t *buf, size_t n, uint32_t crc);
15 
16 buffer* get_buffer(buffer *head, unsigned i)
17 {
18  while(i--) {
19  head = head->next;
20  }
21  return head;
22 }
23 
24 uint32_t read_buffer(buffer *head, unsigned i, uint32_t old_crc)
25 {
26  head = get_buffer(head, i);
27  return crc_update(head->data, head->count, old_crc);
28 }
29 
30 buffer* allocate_buffer(lp_state *state, const unsigned *data, unsigned count)
31 {
32  buffer *new = malloc(sizeof(buffer) + count * sizeof(uint64_t));
33  new->next = state->head;
34  new->count = count;
35 
36  if (data != NULL) {
37  memcpy(new->data, data, count * sizeof(uint64_t));
38  } else {
39  for(unsigned i = 0; i < count; i++) {
40  new->data[i] = lcg_random_u(state->rng_state);
41  }
42  }
43 
44  return new;
45 }
46 
47 buffer* deallocate_buffer(buffer *head, unsigned i)
48 {
49  buffer *prev = NULL;
50  buffer *to_free = head;
51 
52  for (unsigned j = 0; j < i; j++) {
53  prev = to_free;
54  to_free = to_free->next;
55  }
56 
57  if (prev != NULL) {
58  prev->next = to_free->next;
59  free(to_free);
60  return head;
61  }
62 
63  prev = head->next;
64  free(head);
65  return prev;
66 }
67 
68 static uint32_t crc_table[256];
69 
70 void crc_table_init(void)
71 {
72  uint32_t n = 256;
73  while(n--) {
74  uint32_t c = n;
75  int k = 8;
76  while(k--) {
77  if (c & 1) {
78  c = 0xedb88320UL ^ (c >> 1);
79  } else {
80  c = c >> 1;
81  }
82  }
83  crc_table[n] = c;
84  }
85 }
86 
87 uint32_t crc_update(const uint64_t *buf, size_t n, uint32_t crc)
88 {
89  uint32_t c = crc ^ 0xffffffffUL;
90  while (n--) {
91  unsigned k = 64;
92  do {
93  k -= 8;
94  c = crc_table[(c ^ (buf[n] >> k)) & 0xff] ^ (c >> 8);
95  } while(k);
96  }
97  return c ^ 0xffffffffUL;
98 }
lcg_random_u
#define lcg_random_u(rng_state)
Computes a pseudo random 64 bit number.
Definition: test_rng.h:37
_lp_state_type
Definition: application.h:34
_buffer
Definition: application.h:28
application.h
Header of the model used to verify the runtime correctness.