#!/bin/sh
#*=====================================================================*/
#*    .../prgm/project/bigloo/bigloo/autoconf/enforcedcnstalign        */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Mon Jul 10 14:02:47 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Give the constant alignment on the current architecture          */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$TMP/actest$USER
aout=$TMP/Xactest$USER

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $CFLAGS $cflags $file.c -o $aout >/dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if test -f $file.c; then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <stdlib.h>

struct { char *c; char tc[5]; } __attribute__ ((aligned (8))) str1;
struct { char *c; char tc[6]; } __attribute__ ((aligned (8))) str2;
struct { char *c; char tc[7]; } __attribute__ ((aligned (8))) str3;
struct { char *c; char tc[8]; } __attribute__ ((aligned (8))) str4;

int
get_alignment( long p )
{
   int i = 0;

   while( !(p & (1<<i)) ) i++;

   return i;
}

int main () 
{ 
   if( get_alignment( (long)&str1 ) <= 1 )
   {
      puts( "0" );
      exit( 0 );
   }
   if( get_alignment( (long)&str2 ) <= 1 )
   {
      puts( "0" );
      exit( 0 );
   }
   if( get_alignment( (long)&str3 ) <= 1 )
   {
      puts( "0" );
      exit( 0 );
   }
   if( get_alignment( (long)&str4 ) <= 1 )
   {
      puts( "0" );
      exit( 0 );
   }

   puts( "1" );
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
eval "$BUILDSH $compile"

if [ $? = "0" ]; then
   \rm -f $file.*
   eval "$HOSTSH $aout"
else
   \rm -f $file.*
   exit $?
fi

rm -f $aout
rm -rf $aout*
