#if !defined HAVE_BITCYCLIC_MINMAX_H__
#define      HAVE_BITCYCLIC_MINMAX_H__

#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__
