source: XMLIO_V2/external/include/blitz/vecpick.cc @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

File size: 18.4 KB
Line 
1/*
2 * $Id: vecpick.cc,v 1.4 2005/10/06 23:29:34 julianc Exp $
3 *
4 * Copyright (C) 1997 Todd Veldhuizen <tveldhui@oonumerics.org>
5 * All rights reserved.  Please see <blitz/blitz.h> for terms and
6 * conditions of use.
7 *
8 */
9
10#ifndef BZ_VECPICK_CC
11#define BZ_VECPICK_CC
12
13#include <blitz/vecpick.h>
14#include <blitz/update.h>
15#include <blitz/random.h>
16#include <blitz/vecexpr.h>
17
18BZ_NAMESPACE(blitz)
19
20/*****************************************************************************
21 * Assignment operators with vector expression operand
22 */
23
24template<typename P_numtype> template<typename P_expr, typename P_updater>
25inline
26void VectorPick<P_numtype>::_bz_assign(P_expr expr, P_updater)
27{
28    BZPRECONDITION(expr.length(length()) == length());
29
30    // If all vectors in the expression, plus the vector to which the
31    // result is being assigned have unit stride, then avoid stride
32    // calculations.
33    if (_bz_hasFastAccess() && expr._bz_hasFastAccess())
34    {
35#ifndef BZ_PARTIAL_LOOP_UNROLL
36        for (int i=0; i < length(); ++i)
37            P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
38#else
39        // Unwind the inner loop, five elements at a time.
40        int leftover = length() % 5;
41
42        int i=0;
43        for (; i < leftover; ++i)
44            P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
45
46        for (; i < length(); i += 5)
47        {
48            P_updater::update(vector_(index_(i)), expr._bz_fastAccess(i));
49            P_updater::update(vector_(index_(i+1)), expr._bz_fastAccess(i+1));
50            P_updater::update(vector_(index_(i+2)), expr._bz_fastAccess(i+2));
51            P_updater::update(vector_(index_(i+3)), expr._bz_fastAccess(i+3));
52            P_updater::update(vector_(index_(i+4)), expr._bz_fastAccess(i+4));
53        }
54#endif
55    }
56    else {
57        // Not all unit strides -- have to access all the vector elements
58        // as data_[i*stride_], which is slower.
59        for (int i=0; i < length(); ++i)
60            P_updater::update(vector_[index_[i]], expr[i]);
61    }
62}
63
64template<typename P_numtype> template<typename P_expr>
65inline VectorPick<P_numtype>& 
66VectorPick<P_numtype>::operator=(_bz_VecExpr<P_expr> expr)
67{
68    BZPRECONDITION(expr.length(length()) == length());
69
70    // If all vectors in the expression, plus the vector to which the
71    // result is being assigned have unit stride, then avoid stride
72    // calculations.
73    if (_bz_hasFastAccess() && expr._bz_hasFastAccess())
74    {
75#ifndef BZ_PARTIAL_LOOP_UNROLL
76        for (int i=0; i < length(); ++i)
77            (*this)(i) = expr._bz_fastAccess(i);
78#else
79        // Unwind the inner loop, five elements at a time.
80        int leftover = length() % 5;
81
82        int i=0;
83        for (; i < leftover; ++i)
84            (*this)(i) = expr._bz_fastAccess(i);
85
86        for (; i < length(); i += 5)
87        {
88            (*this)(i) = expr._bz_fastAccess(i);
89            (*this)(i+1) = expr._bz_fastAccess(i+1);
90            (*this)(i+2) = expr._bz_fastAccess(i+2);
91            (*this)(i+3) = expr._bz_fastAccess(i+3);
92            (*this)(i+4) = expr._bz_fastAccess(i+4);
93        }
94#endif
95    }
96    else {
97        // Not all unit strides -- have to access all the vector elements
98        // as data_[i*stride_], which is slower.
99        for (int i=0; i < length(); ++i)
100            (*this)[i] = expr[i];
101    }
102    return *this;
103}
104
105
106template<typename P_numtype> template<typename P_expr>
107inline VectorPick<P_numtype>& 
108VectorPick<P_numtype>::operator+=(_bz_VecExpr<P_expr> expr)
109{
110    _bz_assign(expr, _bz_plus_update<P_numtype,
111        _bz_typename P_expr::T_numtype>());
112    return *this;
113}
114
115template<typename P_numtype> template<typename P_expr>
116inline VectorPick<P_numtype>& 
117VectorPick<P_numtype>::operator-=(_bz_VecExpr<P_expr> expr)
118{
119    _bz_assign(expr, _bz_minus_update<P_numtype,
120        _bz_typename P_expr::T_numtype>());
121    return *this;
122}
123
124template<typename P_numtype> template<typename P_expr>
125inline VectorPick<P_numtype>& 
126VectorPick<P_numtype>::operator*=(_bz_VecExpr<P_expr> expr)
127{
128    _bz_assign(expr, _bz_multiply_update<P_numtype,
129        _bz_typename P_expr::T_numtype>());
130    return *this;
131}
132
133template<typename P_numtype> template<typename P_expr>
134inline VectorPick<P_numtype>& 
135VectorPick<P_numtype>::operator/=(_bz_VecExpr<P_expr> expr)
136{
137    _bz_assign(expr, _bz_divide_update<P_numtype,
138        _bz_typename P_expr::T_numtype>());
139    return *this;
140}
141
142template<typename P_numtype> template<typename P_expr>
143inline VectorPick<P_numtype>& 
144VectorPick<P_numtype>::operator%=(_bz_VecExpr<P_expr> expr)
145{
146    _bz_assign(expr, _bz_mod_update<P_numtype,
147        _bz_typename P_expr::T_numtype>());
148    return *this;
149}
150
151template<typename P_numtype> template<typename P_expr>
152inline VectorPick<P_numtype>& 
153VectorPick<P_numtype>::operator^=(_bz_VecExpr<P_expr> expr)
154{
155    _bz_assign(expr, _bz_xor_update<P_numtype,
156        _bz_typename P_expr::T_numtype>());
157    return *this;
158}
159
160template<typename P_numtype> template<typename P_expr>
161inline VectorPick<P_numtype>& 
162VectorPick<P_numtype>::operator&=(_bz_VecExpr<P_expr> expr)
163{
164    _bz_assign(expr, _bz_bitand_update<P_numtype,
165        _bz_typename P_expr::T_numtype>());
166    return *this;
167}
168
169template<typename P_numtype> template<typename P_expr>
170inline VectorPick<P_numtype>& 
171VectorPick<P_numtype>::operator|=(_bz_VecExpr<P_expr> expr)
172{
173    _bz_assign(expr, _bz_bitor_update<P_numtype,
174        _bz_typename P_expr::T_numtype>());
175    return *this;
176}
177
178template<typename P_numtype> template<typename P_expr>
179inline VectorPick<P_numtype>& 
180VectorPick<P_numtype>::operator>>=(_bz_VecExpr<P_expr> expr)
181{
182    _bz_assign(expr, _bz_shiftr_update<P_numtype,
183        _bz_typename P_expr::T_numtype>());
184    return *this;
185}
186
187template<typename P_numtype> template<typename P_expr>
188inline VectorPick<P_numtype>& 
189VectorPick<P_numtype>::operator<<=(_bz_VecExpr<P_expr> expr)
190{
191    _bz_assign(expr, _bz_shiftl_update<P_numtype,
192        _bz_typename P_expr::T_numtype>());
193    return *this;
194}
195
196/*****************************************************************************
197 * Assignment operators with scalar operand
198 */
199
200template<typename P_numtype>
201inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(P_numtype x)
202{
203    typedef _bz_VecExprConstant<P_numtype> T_expr;
204    (*this) = _bz_VecExpr<T_expr>(T_expr(x));
205    return *this;
206}
207
208template<typename P_numtype>
209inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(P_numtype x)
210{
211    typedef _bz_VecExprConstant<P_numtype> T_expr;
212    (*this) += _bz_VecExpr<T_expr>(T_expr(x));
213    return *this;
214}
215
216template<typename P_numtype>
217inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(P_numtype x)
218{
219    typedef _bz_VecExprConstant<P_numtype> T_expr;
220    (*this) -= _bz_VecExpr<T_expr>(T_expr(x));
221    return *this;
222}
223
224template<typename P_numtype>
225inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(P_numtype x)
226{
227    typedef _bz_VecExprConstant<P_numtype> T_expr;
228    (*this) *= _bz_VecExpr<T_expr>(T_expr(x));
229    return *this;
230}
231
232template<typename P_numtype>
233inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(P_numtype x)
234{
235    typedef _bz_VecExprConstant<P_numtype> T_expr;
236    (*this) /= _bz_VecExpr<T_expr>(T_expr(x));
237    return *this;
238}
239
240template<typename P_numtype>
241inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(P_numtype x)
242{
243    typedef _bz_VecExprConstant<P_numtype> T_expr;
244    (*this) %= _bz_VecExpr<T_expr>(T_expr(x));
245    return *this;
246}
247
248template<typename P_numtype>
249inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(P_numtype x)
250{
251    typedef _bz_VecExprConstant<P_numtype> T_expr;
252    (*this) ^= _bz_VecExpr<T_expr>(T_expr(x));
253    return *this;
254}
255
256template<typename P_numtype>
257inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(P_numtype x)
258{
259    typedef _bz_VecExprConstant<P_numtype> T_expr;
260    (*this) &= _bz_VecExpr<T_expr>(T_expr(x));
261    return *this;
262}
263
264template<typename P_numtype>
265inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(P_numtype x)
266{
267    typedef _bz_VecExprConstant<P_numtype> T_expr;
268    (*this) |= _bz_VecExpr<T_expr>(T_expr(x));
269    return *this;
270}
271
272template<typename P_numtype>
273inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator>>=(int x)
274{
275    typedef _bz_VecExprConstant<int> T_expr;
276    (*this) >>= _bz_VecExpr<T_expr>(T_expr(x));
277    return *this;
278}
279
280template<typename P_numtype>
281inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator<<=(int x)
282{
283    typedef _bz_VecExprConstant<P_numtype> T_expr;
284    (*this) <<= _bz_VecExpr<T_expr>(T_expr(x));
285    return *this;
286}
287
288/*****************************************************************************
289 * Assignment operators with vector operand
290 */
291
292template<typename P_numtype> template<typename P_numtype2>
293inline VectorPick<P_numtype>& 
294VectorPick<P_numtype>::operator=(const Vector<P_numtype2>& x)
295{
296    (*this) = _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
297    return *this;
298}
299
300template<typename P_numtype> template<typename P_numtype2>
301inline VectorPick<P_numtype>&
302VectorPick<P_numtype>::operator+=(const Vector<P_numtype2>& x)
303{
304    (*this) += _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
305    return *this;
306}
307
308template<typename P_numtype> template<typename P_numtype2>
309inline VectorPick<P_numtype>&
310VectorPick<P_numtype>::operator-=(const Vector<P_numtype2>& x)
311{
312    (*this) -= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
313    return *this;
314}
315
316template<typename P_numtype> template<typename P_numtype2>
317inline VectorPick<P_numtype>&
318VectorPick<P_numtype>::operator*=(const Vector<P_numtype2>& x)
319{
320    (*this) *= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
321    return *this;
322}
323
324template<typename P_numtype> template<typename P_numtype2>
325inline VectorPick<P_numtype>&
326VectorPick<P_numtype>::operator/=(const Vector<P_numtype2>& x)
327{
328    (*this) /= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
329    return *this;
330}
331
332template<typename P_numtype> template<typename P_numtype2>
333inline VectorPick<P_numtype>&
334VectorPick<P_numtype>::operator%=(const Vector<P_numtype2>& x)
335{
336    (*this) %= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
337    return *this;
338}
339
340template<typename P_numtype> template<typename P_numtype2>
341inline VectorPick<P_numtype>&
342VectorPick<P_numtype>::operator^=(const Vector<P_numtype2>& x)
343{
344    (*this) ^= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
345    return *this;
346}
347
348template<typename P_numtype> template<typename P_numtype2>
349inline VectorPick<P_numtype>&
350VectorPick<P_numtype>::operator&=(const Vector<P_numtype2>& x)
351{
352    (*this) &= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
353    return *this;
354}
355
356template<typename P_numtype> template<typename P_numtype2>
357inline VectorPick<P_numtype>&
358VectorPick<P_numtype>::operator|=(const Vector<P_numtype2>& x)
359{
360    (*this) |= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
361    return *this;
362}
363
364template<typename P_numtype> template<typename P_numtype2>
365inline VectorPick<P_numtype>&
366VectorPick<P_numtype>::operator<<=(const Vector<P_numtype2>& x)
367{
368    (*this) <<= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
369    return *this;
370}
371
372template<typename P_numtype> template<typename P_numtype2>
373inline VectorPick<P_numtype>&
374VectorPick<P_numtype>::operator>>=(const Vector<P_numtype2>& x)
375{
376    (*this) >>= _bz_VecExpr<VectorIterConst<P_numtype2> >(x.beginFast());
377    return *this;
378}
379
380/*****************************************************************************
381 * Assignment operators with Range operand
382 */
383
384template<typename P_numtype>
385inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(Range r)
386{
387    (*this) = _bz_VecExpr<Range>(r);
388    return *this;
389}
390
391template<typename P_numtype>
392inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(Range r)
393{
394    (*this) += _bz_VecExpr<Range>(r);
395    return *this;
396}
397
398template<typename P_numtype>
399inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(Range r)
400{
401    (*this) -= _bz_VecExpr<Range>(r);
402    return *this;
403}
404
405template<typename P_numtype>
406inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(Range r)
407{
408    (*this) *= _bz_VecExpr<Range>(r);
409    return *this;
410}
411
412template<typename P_numtype>
413inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(Range r)
414{
415    (*this) /= _bz_VecExpr<Range>(r);
416    return *this;
417}
418
419template<typename P_numtype>
420inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(Range r)
421{
422    (*this) %= _bz_VecExpr<Range>(r);
423    return *this;
424}
425
426template<typename P_numtype>
427inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(Range r)
428{
429    (*this) ^= _bz_VecExpr<Range>(r);
430    return *this;
431}
432
433template<typename P_numtype>
434inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(Range r)
435{
436    (*this) &= _bz_VecExpr<Range>(r);
437    return *this;
438}
439
440template<typename P_numtype>
441inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(Range r)
442{
443    (*this) |= _bz_VecExpr<Range>(r);
444    return *this;
445}
446
447template<typename P_numtype>
448inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator>>=(Range r)
449{
450    (*this) >>= _bz_VecExpr<Range>(r);
451    return *this;
452}
453
454template<typename P_numtype>
455inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator<<=(Range r)
456{
457    (*this) <<= _bz_VecExpr<Range>(r);
458    return *this;
459}
460
461/*****************************************************************************
462 * Assignment operators with VectorPick operand
463 */
464
465template<typename P_numtype> template<typename P_numtype2>
466inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator=(const 
467    VectorPick<P_numtype2>& x)
468{
469    typedef VectorPickIterConst<P_numtype2> T_expr;
470    (*this) = _bz_VecExpr<T_expr>(x.beginFast());
471    return *this;
472}
473
474template<typename P_numtype> template<typename P_numtype2>
475inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator+=(const
476    VectorPick<P_numtype2>& x)
477{
478    typedef VectorPickIterConst<P_numtype2> T_expr;
479    (*this) += _bz_VecExpr<T_expr>(x.beginFast());
480    return *this;
481}
482
483
484template<typename P_numtype> template<typename P_numtype2>
485inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator-=(const
486    VectorPick<P_numtype2>& x)
487{
488    typedef VectorPickIterConst<P_numtype2> T_expr;
489    (*this) -= _bz_VecExpr<T_expr>(x.beginFast());
490    return *this;
491}
492
493template<typename P_numtype> template<typename P_numtype2>
494inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator*=(const
495    VectorPick<P_numtype2>& x)
496{
497    typedef VectorPickIterConst<P_numtype2> T_expr;
498    (*this) *= _bz_VecExpr<T_expr>(x.beginFast());
499    return *this;
500}
501
502template<typename P_numtype> template<typename P_numtype2>
503inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator/=(const
504    VectorPick<P_numtype2>& x)
505{
506    typedef VectorPickIterConst<P_numtype2> T_expr;
507    (*this) /= _bz_VecExpr<T_expr>(x.beginFast());
508    return *this;
509}
510
511template<typename P_numtype> template<typename P_numtype2>
512inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator%=(const
513    VectorPick<P_numtype2>& x)
514{
515    typedef VectorPickIterConst<P_numtype2> T_expr;
516    (*this) %= _bz_VecExpr<T_expr>(x.beginFast());
517    return *this;
518}
519
520template<typename P_numtype> template<typename P_numtype2>
521inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator^=(const
522    VectorPick<P_numtype2>& x)
523{
524    typedef VectorPickIterConst<P_numtype2> T_expr;
525    (*this) ^= _bz_VecExpr<T_expr>(x.beginFast());
526    return *this;
527}
528
529template<typename P_numtype> template<typename P_numtype2>
530inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator&=(const
531    VectorPick<P_numtype2>& x)
532{
533    typedef VectorPickIterConst<P_numtype2> T_expr;
534    (*this) &= _bz_VecExpr<T_expr>(x.beginFast());
535    return *this;
536}
537
538template<typename P_numtype> template<typename P_numtype2>
539inline VectorPick<P_numtype>& VectorPick<P_numtype>::operator|=(const
540    VectorPick<P_numtype2>& x)
541{
542    typedef VectorPickIterConst<P_numtype2> T_expr;
543    (*this) |= _bz_VecExpr<T_expr>(x.beginFast());
544    return *this;
545}
546
547/*****************************************************************************
548 * Assignment operators with Random operand
549 */
550
551template<typename P_numtype> template<typename P_distribution>
552VectorPick<P_numtype>& 
553VectorPick<P_numtype>::operator=(Random<P_distribution>& rand)
554{
555    (*this) = _bz_VecExpr<_bz_VecExprRandom<P_distribution> > 
556        (_bz_VecExprRandom<P_distribution>(rand));
557    return *this;
558}
559
560template<typename P_numtype> template<typename P_distribution>
561VectorPick<P_numtype>& 
562VectorPick<P_numtype>::operator+=(Random<P_distribution>& rand)
563{
564    (*this) += _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
565        (_bz_VecExprRandom<P_distribution>(rand));
566    return *this;
567}
568
569template<typename P_numtype> template<typename P_distribution>
570VectorPick<P_numtype>& 
571VectorPick<P_numtype>::operator-=(Random<P_distribution>& rand)
572{
573    (*this) -= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
574        (_bz_VecExprRandom<P_distribution>(rand));
575    return *this;
576}
577
578template<typename P_numtype> template<typename P_distribution>
579VectorPick<P_numtype>& 
580VectorPick<P_numtype>::operator*=(Random<P_distribution>& rand)
581{
582    (*this) *= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
583        (_bz_VecExprRandom<P_distribution>(rand));
584    return *this;
585}
586
587template<typename P_numtype> template<typename P_distribution>
588VectorPick<P_numtype>& 
589VectorPick<P_numtype>::operator/=(Random<P_distribution>& rand)
590{
591    (*this) /= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
592        (_bz_VecExprRandom<P_distribution>(rand));
593    return *this;
594}
595
596template<typename P_numtype> template<typename P_distribution>
597VectorPick<P_numtype>& 
598VectorPick<P_numtype>::operator%=(Random<P_distribution>& rand)
599{
600    (*this) %= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
601        (_bz_VecExprRandom<P_distribution>(rand));
602    return *this;
603}
604
605template<typename P_numtype> template<typename P_distribution>
606VectorPick<P_numtype>& 
607VectorPick<P_numtype>::operator^=(Random<P_distribution>& rand)
608{
609    (*this) ^= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
610        (_bz_VecExprRandom<P_distribution>(rand));
611    return *this;
612}
613
614template<typename P_numtype> template<typename P_distribution>
615VectorPick<P_numtype>& 
616VectorPick<P_numtype>::operator&=(Random<P_distribution>& rand)
617{
618    (*this) &= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
619        (_bz_VecExprRandom<P_distribution>(rand));
620    return *this;
621}
622
623template<typename P_numtype> template<typename P_distribution>
624VectorPick<P_numtype>& 
625VectorPick<P_numtype>::operator|=(Random<P_distribution>& rand)
626{
627    (*this) |= _bz_VecExpr<_bz_VecExprRandom<P_distribution> >
628        (_bz_VecExprRandom<P_distribution>(rand));
629    return *this;
630}
631
632BZ_NAMESPACE_END
633
634#endif // BZ_VECPICK_CC
Note: See TracBrowser for help on using the repository browser.