/* Copyright (c) 1997-2006
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ifndef _POLYMAKE_INLINES_INT_H
#define _POLYMAKE_INLINES_INT_H "$Project: polymake $$Id: inlines_int.h,v 1.11 2006/03/09 16:21:36 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma interface
#endif

#include <cstdlib>

inline long gcd(long a, long b)
{
   if (a<0) a=-a;
   if (b<0) b=-b;
   if (a==0) return b;
   if (b==0) return a;
   if (a==1 || b==1) return 1;

   int k=0;
   while (!((a|b)&1)) {
      a>>=1;  b>>=1;  k++;
   }

   if (a&1)
      while (!(b&1)) b>>=1;
   else
      while (!(a&1)) a>>=1;

   long d=a-b;
   while (d) {
      while (!(d&1)) d>>=1;
      if (d>0)
	 a=d;
      else
	 b=-d;
      d=a-b;
   }

   return a<<k;
}

inline long lcm(long a, long b)
{
   return (a/gcd(a,b))*b;
}

/// g=gcd(a,b) && g==p*a+q*b
inline void gcd_ext(long a, long b, long& g, long& p, long& q)
{
   long A11= a<0 ? (a=-a, -1L) : 1L,  A12=0,
        A22= b<0 ? (b=-b, -1L) : 1L,  A21=0;
   long k;

   if (a<b) goto STEP2;
   for (;;) {
      k=a/b;
      A11-=k*A12;  A21-=k*A22;
      if (!(a-=k*b)) {
	 g=b;  p=A12;  q=A22;
	 break;
      }
   STEP2:
      k=b/a;
      A12-=k*A11;  A22-=k*A21;
      if (!(b-=k*a)) {
	 g=a;  p=A11;  q=A21;
	 break;
      }
   }
}

inline long div_exact(long x, long y) { return x/y; }

inline int log2_round(unsigned long x, int round) {
   if (x<=1UL) return 0;

   int step=(sizeof(unsigned long)*8)/2, log2=step;
   unsigned long pow2=1<<step;

   while (x != pow2) {
      if (x < pow2) {
	 if (!(step >>= 1)) return log2-1+round;
	 pow2 >>= step;
	 log2 -= step;
      } else {
	 if (!(step >>= 1)) return log2+round;
	 pow2 <<= step;
	 log2 += step;
      }
   }
   return log2;
}

inline int log2_floor(unsigned long x) { return log2_round(x,0); }
inline int log2_ceil(unsigned long x) { return log2_round(x,1); }

inline bool abs_equal(long x, long y) { return x==y || -x==y; }

#endif // _POLYMAKE_INLINES_INT_H

// Local Variables:
// mode:C++
// End:
