summaryrefslogtreecommitdiff
path: root/envytools/rnn/dedma_cache.c
blob: bf357a26ae2fcb020281344bf83b65d1b4dd4671 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2010 Francisco Jerez.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "dedma.h"

#define CACHE_ENT(s, i)							\
	(&(s)->cache.ent[((i) - (s)->cache.i0 + (s)->cache.k) % MAX_CACHE])

struct ent *
get_ent(struct state *s, int i)
{
	struct cache *c = &s->cache;

	if (i < c->i0 || i > c->i1)
		return NULL;

	if (i == c->i1 && !s->op.parse(s))
		return NULL;

	return CACHE_ENT(s, i);
}

void
rotate_ent(struct state *s, int i, int j)
{
	const int d = sizeof(struct ent);
	struct cache *c = &s->cache;
	int ki = CACHE_ENT(s, i) - c->ent;
	int kj = CACHE_ENT(s, j) - c->ent;
	struct ent tmp;

	assert(j >= i && j >= c->i0 && j < c->i1 &&
	       i >= c->i0 && i < c->i1);

	memmove(&tmp, c->ent + kj, d);

	if (ki >= c->k && kj < c->k) {
		/* the region wraps around */
		memmove(c->ent + 1, c->ent, d * kj);
		memmove(c->ent, c->ent + MAX_CACHE - 1, d);
		memmove(c->ent + ki + 1, c->ent + ki, d * (MAX_CACHE - ki - 1));

	} else {
		/* easy case */
		memmove(c->ent + ki + 1, c->ent + ki, d * (kj - ki));
	}

	memmove(c->ent + ki, &tmp, d);
}

void
add_ent(struct state *s, struct ent *e)
{
	struct cache *c = &s->cache;

	if (c->i1 - c->i0 == MAX_CACHE) {
		s->op.flush(s, CACHE_ENT(s, c->i0));
		c->k = (c->k + 1) % MAX_CACHE;
		c->i0++;
	}

	*CACHE_ENT(s, c->i1) = *e;
	c->i1++;
}

void
drop_ent(struct state *s, int i)
{
	struct cache *c = &s->cache;

	rotate_ent(s, c->i0, i);
	c->k = (c->k + 1) % MAX_CACHE;
	c->i1--;
}

void
flush_cache(struct state *s)
{
	struct cache *c = &s->cache;
	int i;

	for (i = c->i0; i < c->i1; i++)
		s->op.flush(s, CACHE_ENT(s, i));

	c->i0 = c->i1 = 0;
}