Imported Upstream version 3.2.2
[debian/gnuradio] / gcell / lib / general / spu / qa_memset.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <gcell/gc_declare_proc.h>
23 #include <spu_intrinsics.h>
24 #include <spu_mfcio.h>
25 #include <string.h>
26 #include <stdio.h>
27
28
29 #define MAX_QA_BYTES  1024
30 #define MAX_OFFSET    32
31 #define ALIGNMENT     16
32 #define K             0xA5
33
34 // FIXME should be passed at gcell init time
35 //static const int TIMEBASE = 79800000; // ps3
36 static const int TIMEBASE = 26666666; // qs21
37
38 typedef void* (*memset_fptr)(void *s, int val, size_t n);
39
40 void *
41 memset_ref(void *sv, int c, size_t n)
42 {
43   unsigned char *s = (unsigned char *) sv;
44   size_t i;
45   for (i = 0; i < n; i++)
46     s[i] = c;
47
48   return sv;
49 }
50
51 static bool
52 check_before(unsigned char *buf, size_t len, size_t offset)
53 {
54   unsigned char *p = buf + sizeof(vector unsigned char) + offset;
55   bool ok = true;
56   int i;
57
58   for (i = -16; i < 0; i++){
59     unsigned char expected = (&p[i] - buf) & 0xff;
60     if (p[i] != expected){
61       printf("b:memset(%p, 0x%x, %zu) <offset %2zd> [%3d] expected %02x, got %02x\n",
62              p, K, len, offset, i, K, p[i]);
63       ok = false;
64     }
65   }
66   return ok;
67 }
68
69 static bool
70 check_middle(unsigned char *buf, size_t len, size_t offset)
71 {
72   unsigned char *p = buf + sizeof(vector unsigned char) + offset;
73   bool ok = true;
74   size_t i;
75
76   for (i = 0; i < len; i++){
77     unsigned char expected = K;
78     if (p[i] != expected){
79       printf("m:memset(%p, 0x%x, %zu) <offset %2zd> [%3zd] expected %02x, got %02x\n",
80              p, K, len, offset, i, expected, p[i]);
81       ok = false;
82     }
83   }
84   return ok;
85 }
86
87 static bool
88 check_after(unsigned char *buf, size_t len, size_t offset)
89 {
90   unsigned char *p = buf + sizeof(vector unsigned char) + offset;
91   bool ok = true;
92   size_t i;
93
94   for (i = len; i < len + 16; i++){
95     unsigned char expected = (&p[i] - buf) & 0xff;
96     if (p[i] != expected){
97       printf("a:memset(%p, 0x%x, %zu) <offset %2zd> [%3zd] expected %02x, got %02x\n",
98              p, K, len, offset, i, expected, p[i]);
99       ok = false;
100     }
101   }
102   return ok;
103 }
104
105
106 static bool
107 test_memset_aux(memset_fptr f,
108                 unsigned char *buf, size_t buflen, size_t len, size_t offset)
109 {
110   size_t i;
111
112   // init buffer to non-zero known state
113   for (i = 0; i < buflen; i++)
114     buf[i] = i;
115   
116   // Our working buffer.  Starts 16 bytes + offset into buf.
117   // We offset by 16 so that we can see if data before is getting damaged.
118   unsigned char *p = buf + sizeof(vector unsigned char) + offset;
119
120   (*f)(p, K, len);
121
122   bool ok = true;
123   ok &= check_before(buf, len, offset);
124   ok &= check_middle(buf, len, offset);
125   ok &= check_after(buf, len, offset);
126
127   return ok;
128 }
129
130 bool
131 test_memset(memset_fptr f)
132 {
133   size_t BUFLEN = MAX_QA_BYTES + 2*sizeof(vector unsigned char) + MAX_OFFSET;
134   unsigned char unaligned_buf[BUFLEN + ALIGNMENT -1];
135   unsigned char *aligned_buf =
136     (unsigned char *)((((intptr_t) unaligned_buf) + ALIGNMENT - 1) & -ALIGNMENT);
137
138   // printf("unaligned = %p\n", unaligned_buf);
139   // printf("aligned   = %p\n", aligned_buf);
140
141   size_t len;
142   size_t offset;
143   bool ok = true;
144
145   for (len = 0; len < MAX_QA_BYTES; len++){
146     for (offset = 0; offset <= MAX_OFFSET; offset++){
147       ok &= test_memset_aux(f, aligned_buf, BUFLEN, len, offset);
148     }
149   }
150
151   return ok;
152 }
153
154 // returns bytes/s
155 float
156 benchmark_memset(memset_fptr f, bool aligned)
157 {
158   static const int SIZE = 32768;
159   unsigned char buf[SIZE];
160   uint32_t      t0, t1;
161   int           nbytes;
162
163   spu_write_decrementer(0xffffffff);
164
165   if (aligned){
166     nbytes = SIZE;
167     t0 = spu_read_decrementer();
168     (*f)(buf, 0x55, nbytes);
169     (*f)(buf, 0x55, nbytes);
170     (*f)(buf, 0x55, nbytes);
171     (*f)(buf, 0x55, nbytes);
172     t1 = spu_read_decrementer();
173   }
174   else {
175     nbytes = SIZE - 2;
176     t0 = spu_read_decrementer();
177     (*f)(buf + 1, 0x55, nbytes);
178     (*f)(buf + 1, 0x55, nbytes);
179     (*f)(buf + 1, 0x55, nbytes);
180     (*f)(buf + 1, 0x55, nbytes);
181     t1 = spu_read_decrementer();
182   }
183
184   //printf("delta ticks: %d\n", t0 - t1);
185   return (float) nbytes * 4 / ((t0 - t1) * 1.0/TIMEBASE);
186 }
187
188 /*
189  * Implement the standard QA stub.
190  * No input arguments, 1 bool output.
191  */
192 static void
193 gcs_qa_memset(const gc_job_direct_args_t *input _UNUSED,
194               gc_job_direct_args_t *output,
195               const gc_job_ea_args_t *eaa _UNUSED)
196 {
197   bool ok = test_memset(memset);
198   output->arg[0].u32 = ok;
199 }
200
201 GC_DECLARE_PROC(gcs_qa_memset, "qa_memset");