#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/signbit              */
#*    -------------------------------------------------------------    */
#*    Author      :  Jose Romildo                                      */
#*    Creation    :  Sat Nov 11 13:27:23 1995                          */
#*    Last change :  Mon Jul 10 13:57:46 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Detect the mask for the sign bit of an integer type.             */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=
type=long
alignment=0

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

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

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

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

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

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

#*---------------------------------------------------------------------*/
#*    Default setting                                                  */
#*---------------------------------------------------------------------*/
if [ "$type " = " " ]; then
  type="int"
fi

unit=""

case $type in
  "int") unit="";;
  "long") unit="L";;
  "long long") unit="LL";;
  *) echo "Unknown type $type assumed to be without suffix" >&2;;
esac

#*---------------------------------------------------------------------*/
#*    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 <limits.h>
int main( int argc, char *argv[] ) {
   int n;
   unsigned long long x = 1;
   for (n = 8 * sizeof($type) - 1 - ($alignment); n > 0; n--)
      x <<= 1;
   printf("%#llx$unit\n", x);   
   return 0;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout"
   ret_code=$?
   \rm -f $file.*
   rm -f $aout
   rm -rf $aout*
   exit $ret_code
else
   \rm -f $file.*
   echo ""
fi
