source: CIVL/examples/compare/sliced_vector/sliced_vector_addin.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.8 KB
Line 
1// program 2:
2#include <stdlib.h>
3#include <stdio.h>
4
5typedef struct {int * a; int n;} vector;
6
7vector *vector_init(vector * v, int n) {
8 v->a = malloc(n*sizeof(int));
9 v->n = n;
10 for (int i = 0; i < n; ++i) v->a[i] = 0;
11 return v;
12}
13
14//////////////////////////////////////////////////////////////////////
15/***
16sliced stuff:
17 Requires vector size is multiple of 64, and u,v are aligned.
18***/
19
20typedef unsigned long int sliced_storage;
21
22typedef struct
23{
24 sliced_storage b0;
25 sliced_storage b1;
26} sliced_word;
27
28typedef struct
29{
30 sliced_word *rep;
31 int n_words; //number of words, not number of entries
32} sliced_vector;
33
34sliced_word *sliced_word_right_shift(sliced_word *lhs, const int n) {
35 lhs->b0 >>= n;
36 lhs->b1 >>= n;
37 return lhs;
38}
39
40sliced_word *sliced_word_addin(sliced_word *lhs, const sliced_word *rhs) {
41 sliced_storage a, b, s, t;
42 a = lhs->b0 ^ rhs->b1;
43 b = lhs->b1 ^ rhs->b0;
44 s = a ^ lhs->b1;
45 t = b ^ rhs->b1;
46 lhs->b1 = a & b;
47 lhs->b0 = s | t;
48 return lhs;
49}
50
51const int *sliced_word_setEntry(sliced_word *lhs, const int i, const int * const x) {
52 if ((*x) == 2) {
53 lhs->b0 |= (sliced_storage)1 << i;
54 lhs->b1 |= (sliced_storage)1 << i;
55 }
56 else if ((*x) == 1) {
57 lhs->b0 |= (sliced_storage)1 << i;
58 lhs->b1 &= ~((sliced_storage)1 << i);
59 }
60 else {
61 lhs->b0 &= ~((sliced_storage)1 << i);
62 lhs->b1 &= ~((sliced_storage)1 << i);
63 }
64 return x;
65}
66
67int *sliced_word_getEntry(const sliced_word *lhs, int *x, const int i) {
68 (*x) = (lhs->b0 & ((sliced_storage)1 << i)) >> i;
69 (*x) += (lhs->b1 & ((sliced_storage)1 << i)) >> i;
70 return x;
71}
72
73sliced_vector *sliced_vector_init(sliced_vector *v, int n) {
74 v->n_words = (n + 8*sizeof(sliced_storage) - 1) / (8*sizeof(sliced_storage));
75 v->rep = calloc(v->n_words, sizeof(sliced_word));
76 return v;
77}
78
79// u <- u+v
80sliced_vector *sliced_vector_addin(sliced_vector *u, const sliced_vector *v) {
81 int i;
82 for (i = 0; i < u->n_words; ++i) {
83 sliced_word_addin(u->rep + i, v->rep + i);
84 }
85 return u;
86}
87
88// u[i] <-- x
89const int *sliced_vector_setEntry(sliced_vector *u, const int i, const int * const x ) {
90 int w, o;
91 w = i / (8*sizeof(sliced_storage));
92 o = i % (8*sizeof(sliced_storage));
93 sliced_word_setEntry(u->rep + w, o, x);
94 return x;
95}
96
97
98// x <-- u[i]
99int *sliced_vector_getEntry(const sliced_vector *const u, int *x, const int i) {
100 int w, o;
101 w = i / (8*sizeof(sliced_storage));
102 o = i % (8*sizeof(sliced_storage));
103 sliced_word_getEntry(u->rep + w, x, o);
104 return x;
105}
106
107sliced_vector* sliced_vector_delete(sliced_vector *u) {
108 free(u->rep);
109 u->rep = NULL;
110 u->n_words = 0;
111 return u;
112}
113//////////////////////////////////////////////////////////////////////
114
115// vector a += b, using conversion to/from sliced form.
116vector *vector_addin_using_sliced(vector *a, const vector *b){
117 sliced_vector x; sliced_vector_init(&x, a->n);
118 sliced_vector y; sliced_vector_init(&y, b->n);
119 for (int i = 0; i < a->n; ++i) {
120 sliced_vector_setEntry(&x, i, &(a->a[i]));
121 sliced_vector_setEntry(&y, i, &(b->a[i]));
122 }
123 sliced_vector_addin(&x, &y);
124 for (int i = 0; i < a->n; ++i) {
125 int temp;
126 sliced_vector_getEntry(&x, &temp, i);
127 a->a[i] = temp;
128 }
129}
130
131int main(){
132 int n = 8;
133 vector x; vector_init(&x, n);
134 vector y; vector_init(&y, n);
135 for (int i = 0; i < x.n; ++i) { x.a[i] = i%3; y.a[i] = (i/3)%3; }
136
137 vector_addin_using_sliced(&x, &y);
138
139 int error = 0;
140 for (int i = 0; i < x.n; ++i) if (x.a[i] != (i+i/3)%3) error += 1;
141 if (error) printf("Sliced Error\n");
142 else printf("Sliced Good\n");
143 return error ? -1 : 0;
144}
Note: See TracBrowser for help on using the repository browser.