BRL-CAD
test_dvec.cpp
Go to the documentation of this file.
1 /* T E S T _ D V E C . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <iostream>
26 #include <assert.h>
27 
28 #include "vmath.h"
29 #include "brep.h"
30 #include "dvec.h"
31 
32 
33 const int COUNT = 1000000;
34 
35 int
36 main(int argc, char** argv)
37 {
38  if (argc > 1)
39  fprintf(stderr,"Usage: %s\n", argv[0]);
40 
41 #ifdef __x86_vector__
42  printf("using x86 vectorization\n");
43 #elif defined(__fpu_vector__)
44  printf("using fpu vectorization\n");
45 #endif
46 
47  {
48  // test correctness
49  double _a[8] VEC_ALIGN = {0, 1, 2, 3, 4, 5, 6, 7};
50  double _b[8] VEC_ALIGN = {2, 4, 6, 8, 10, 12, 14, 16};
51  dvec<8> a(_a);
52  dvec<8> b(_b);
53 
54  dvec<8> c = a + b;
55  double _c[8] VEC_ALIGN = {2, 5, 8, 11, 14, 17, 20, 23};
56  assert(c == dvec<8>(_c));
57 
58  dvec<8> d = a - b;
59  double _d[8] VEC_ALIGN = {-2, -3, -4, -5, -6, -7, -8, -9};
60  assert(d == dvec<8>(_d));
61 
62  dvec<8> e = a * b;
63  double _e[8] VEC_ALIGN = {0, 4, 12, 24, 40, 60, 84, 112};
64  assert(e == dvec<8>(_e));
65 
66  dvec<8> f = a / b;
67  double _f[8] VEC_ALIGN = {0, 0.25, 0.333333333333, 0.375, 0.40, 0.4166666666, 0.42857142, 0.4375};
68  assert(f == dvec<8>(_f));
69 
70  dvec<8> g = a.madd(c, b);
71  double _g[8] VEC_ALIGN = {2, 9, 22, 41, 66, 97, 134, 177};
72  assert(g == dvec<8>(_g));
73 
74  dvec<8> h = a.madd(2.0, b);
75  double _h[8] VEC_ALIGN = {2, 6, 10, 14, 18, 22, 26, 30};
76  assert(h == dvec<8>(_h));
77 
78  srand(time(NULL));
79  double total = 0.0;
80  double rval[] = {
81  rand()/10000.0,
82  rand()/10000.0,
83  rand()/10000.0,
84  rand()/10000.0,
85  rand()/10000.0,
86  rand()/10000.0,
87  rand()/10000.0,
88  rand()/10000.0,
89  rand()/10000.0,
90  rand()/10000.0,
91  rand()/10000.0,
92  rand()/10000.0,
93  rand()/10000.0,
94  rand()/10000.0,
95  rand()/10000.0,
96  rand()/10000.0,
97  rand()/10000.0 };
98 
99  clock_t start = clock();
100  for (int i = 0; i < COUNT; i++) {
101  double l_a[8] VEC_ALIGN = {rval[i%8], rval[i%8+1], rval[i%8+2], rval[i%8+3],
102  rval[i%8+4], rval[i%8+5], rval[i%8+6], rval[i%8+7]};
103  double l_b[8] VEC_ALIGN = {rval[i%8+1], rval[i%8+2], rval[i%8+3], rval[i%8+4],
104  rval[i%8+5], rval[i%8+6], rval[i%8+7], rval[i%8+8]};
105 
106  dvec<8> la(l_a);
107  dvec<8> lb(l_b);
108 
109  dvec<8> lc = la + lb;
110  dvec<8> ld = lc - la;
111  dvec<8> le = ld * lc;
112  dvec<8> lf = le / la;
113  dvec<8> lg = la.madd(rval[i%16], lf);
114  dvec<8> lh = lg.madd(lf, la+lb*lc-ld/le*lf);
115  total += lh[7];
116  }
117  printf("dvec<8> time: %3.4g\n", (double)(clock()-start)/(double)CLOCKS_PER_SEC);
118  }
119 
120 /* test correctness */
121  vec2d a(100.0, -100.0);
122  vec2d b(200.0, -200.0);
123 
124  vec2d c = a + b;
125  assert(vequals(c, vec2d(300.0, -300.0)));
126 
127  vec2d d = a - b;
128  assert(vequals(d, vec2d(-100.0, 100.0)));
129 
130  vec2d e = a * b;
131  assert(vequals(e, vec2d(20000.0, 20000.0)));
132 
133  vec2d f = b / a;
134  assert(vequals(f, vec2d(2.0, 2.0)));
135 
136  vec2d g = a.madd(20.0, b);
137  assert(vequals(g, vec2d(2200.0, -2200.0)));
138 
139  vec2d h = a.madd(d, b);
140  assert(vequals(h, vec2d(-9800.0, -10200.0)));
141 
142 /* simple performance test */
143 
144  srand(time(NULL));
145  double total = 0.0;
146  double rval[] = {
147  rand()/10000.0,
148  rand()/10000.0,
149  rand()/10000.0,
150  rand()/10000.0,
151  rand()/10000.0,
152  rand()/10000.0,
153  rand()/10000.0,
154  rand()/10000.0,
155  rand()/10000.0,
156  rand()/10000.0,
157  rand()/10000.0,
158  rand()/10000.0,
159  rand()/10000.0,
160  rand()/10000.0,
161  rand()/10000.0,
162  rand()/10000.0 };
163 
164  clock_t start = clock();
165  for (int i = 0; i < COUNT; i++) {
166  vec2d la(rval[i%12], rval[i%12+2]);
167  vec2d lb(rval[i%12+1], rval[i%12+3]);
168 
169  vec2d lc = la + lb;
170  vec2d ld = lc - la;
171  vec2d le = ld * lc;
172  vec2d lf = le / la;
173  vec2d lg = la.madd(rval[i%12], lf);
174  vec2d lh = lg.madd(lg, la + lb - lc + ld - le + lf);
175  total += lh.x() + lh.y();
176  }
177  printf("vec2d time: %3.4g\n", (double)(clock()-start)/(double)CLOCKS_PER_SEC);
178 
179  /* return 'fail' if no time elapsed */
180  return (total > 0) ? 0 : 1;
181 }
182 
183 
184 // Local Variables:
185 // tab-width: 8
186 // mode: C++
187 // c-basic-offset: 4
188 // indent-tabs-mode: t
189 // c-file-style: "stroustrup"
190 // End:
191 // ex: shiftwidth=4 tabstop=8
const int COUNT
Definition: test_dvec.cpp:33
Header file for the BRL-CAD common definitions.
long time(time_t *)
int main(int argc, char **argv)
Definition: test_dvec.cpp:36