#if !defined HAVE_REVERSE_H__
#define      HAVE_REVERSE_H__
// This file is part of the FXT library.
// Copyright (C) 2010 Joerg Arndt
// License: GNU General Public License version 3 or later,
// see the file COPYING.txt in the main directory.


#include "aux0/swap.h"
#include "fxttypes.h"

#define  USE_PTRS   0 // default is 0
#define  USE_UNROLL 0 // default is 0

template <typename Type>
inline void reverse(Type *f, ulong n)
// Reverse order of array f.
{
    if ( n<2 )  return;
#if  USE_PTRS == 1

    Type *g = f + n - 1;
    do  { swap2(*f, *g); }  while ( ++f < --g );

#else // USE_PTRS


#if  USE_UNROLL == 0
    for (ulong k=0, i=n-1;  k<i;  ++k, --i)  swap2(f[k], f[i]);
#else
    if ( n<32 ) for (ulong k=0, i=n-1;  k<i;  ++k, --i)  swap2(f[k], f[i]);
    else
    {
        ulong k = 0,  i = n - 1;
        n >>= 1;
        if ( n&1 )  { swap2(f[k++], f[i--]);  }
        n >>= 1;
        if ( n&1 )  { swap2(f[k++], f[i--]); swap2(f[k++], f[i--]); }
        n >>= 1;
        do
        {
            swap2(f[k], f[i]);
            swap2(f[k+1], f[i-1]);
            swap2(f[k+2], f[i-2]);
            swap2(f[k+3], f[i-3]);
            k += 4;  i -= 4;
        }
        while ( --n );
    }
#endif // USE_UNROLL

#endif // USE_PTRS
}
// -------------------------


template <typename Type>
inline void reverse_0(Type *f, ulong n)
// Reverse array around index zero:
{
    if ( n>2 )  reverse(f+1, n-1);
}
// -------------------------

#undef  USE_PTRS
//#undef  USE_UNROLL


#endif // !defined HAVE_REVERSE_H__

