#if !defined HAVE_BITCYCLIC_MINMAX_H__
#define      HAVE_BITCYCLIC_MINMAX_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 "fxttypes.h"
#include "bits/bitsperlong.h"
#include "bits/bitrotate.h"

static inline ulong bit_cyclic_min(ulong x)
// Return minimum of all rotations of x
{
    ulong r = 1;
    ulong m = x;
    do
    {
        x = bit_rotate_right(x, 1);
        if ( x<m )  m = x;
    }
    while ( ++r < BITS_PER_LONG );

    return  m;
}
// -------------------------

static inline ulong bit_cyclic_min(ulong x, ulong ldn)
// Return minimum of all rotations of x
//  using ldn-bit words
{
    ulong r = 1;
    ulong m = x;
    do
    {
        x = bit_rotate_right(x, 1, ldn);
        if ( x<m )  m = x;
    }
    while ( ++r < ldn );

    return  m;
}
// -------------------------


static inline ulong bit_cyclic_max(ulong x)
// Return maximum of all rotations of x
{
    ulong r = 1;
    ulong m = x;
    do
    {
        x = bit_rotate_right(x, 1);
        if ( x>m )  m = x;
    }
    while ( ++r < BITS_PER_LONG );

    return  m;
}
// -------------------------

static inline ulong bit_cyclic_max(ulong x, ulong ldn)
// Return maximum of all rotations of x
//  using ldn-bit words
{
    ulong r = 1;
    ulong m = x;
    do
    {
        x = bit_rotate_right(x, 1, ldn);
        if ( x>m )  m = x;
    }
    while ( ++r < ldn );

    return  m;
}
// -------------------------

#endif  // !defined HAVE_BITCYCLIC_MINMAX_H__

