/* tag: Tom Lord Tue Dec  4 15:02:16 2001 (gen-alignment.to-c)
 */
/* gen-alignment.c - -*-c-*-
 *
 ****************************************************************
 * Copyright (C) 2000 Tom Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include <stdio.h>

#define TYPES \
  TYPE(char, char) \
  TYPE(short, short) \
  TYPE(int, int) \
  TYPE(long, long) \
  TYPE(long long, long_long) \
  TYPE(void *, pointer) \
  TYPE(float, float) \
  TYPE(double, double) \
  TYPE(long double, long_double)

#undef TYPE
#define TYPE(SCALAR, STRUCT) \
struct flux__align_ ## STRUCT \
{ \
  SCALAR a; \
  char b; \
};

TYPES

static size_t
gcd (size_t a, size_t b)
{
  size_t best;
  size_t guess;
  size_t smaller;

  /* a and b are both small */
  smaller = ((a < b) ? a : b);
  best = 1;

  for (guess = 2; guess <= smaller; ++guess)
    {
      if (!(a % guess) && !(b % guess))
	best = guess;
    }

  return best;
}


static size_t
machine_alignment (void)
{
  size_t best;
  size_t this;

  best = 1;

#undef TYPE
#define TYPE(SCALAR, STRUCT) \
  this = gcd(sizeof (SCALAR), sizeof (struct flux__align_ ## STRUCT)); \
  if (this > best) \
    best = this;

  TYPES
    return best;
}


int
main (int argc, char * argv[])
{
  printf ("/* Automatically generated by gen-alignment. DO NOT EDIT.\n");
  printf ("*/\n");
  printf ("#ifndef INCLUDE__MACHINE__ALIGNMENT_H\n");
  printf ("#define INCLUDE__MACHINE__ALIGNMENT_H\n");
  printf ("\n\n");
  printf ("#define MACHINE_ALIGNMENT %ld\n", (long)machine_alignment ());
  printf ("\n\n");
  printf ("#endif\n");
  return 0;
}
