#if !defined  HAVE_RADIX_M4_H__
#define       HAVE_RADIX_M4_H__
// This file is part of the FXT library.
// Copyright (C) 2011 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"

//: Conversion to and from radix(-4).

static inline ulong bin_to_radm4(ulong x)
// binary --> radix(-4)
{
#if  BITS_PER_LONG >= 64
    // mask in binary is ...110011001100
    // mask in radix 4 is ...30303030
    const ulong m = 0xccccccccccccccccUL;
#else
    const ulong m = 0xccccccccUL;
#endif
    x += m;
    x ^= m;
    return  x;
}
// -------------------------

static inline ulong radm4_to_bin(ulong x)
// radix(-4) --> binary
// inverse of bin_to_radm4()
{
#if  BITS_PER_LONG >= 64
    const ulong m = 0xccccccccccccccccUL;
#else
    const ulong m = 0xccccccccUL;
#endif
    x ^= m;
    x -= m;
    return  x;
}
// -------------------------


static inline ulong next_radm4(ulong x)
// With x the radix(-4) representation of n
// return radix(-4) representation of n+1.
{
#if  BITS_PER_LONG >= 64
    const ulong m = 0xccccccccccccccccUL;
#else
    const ulong m = 0xccccccccUL;
#endif
    x ^= m;
    ++x;
    x ^= m;
    return x;
}
// -------------------------

static inline ulong prev_radm4(ulong x)
// With x the radix(-4) representation of n
// return radix(-4) representation of n-1.
{
#if  BITS_PER_LONG >= 64
    const ulong m = 0xccccccccccccccccUL;
#else
    const ulong m = 0xccccccccUL;
#endif
    x ^= m;
    --x;
    x ^= m;
    return x;
}
// -------------------------


#endif  // !defined HAVE_RADIX_M4_H__
