/* Copyright (C) 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA.  */

/*
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement:  ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <ansidecl.h>
#include <float.h>
#include <errno.h>
#include "ieee854.h"
long double __infnanl (int);

long double ldexpl( long double x, int exp);

long double
DEFUN(ldexpl, (x, exp),
      long double x AND int exp)
{
  union ieee854_double *u = (union ieee854_double *) &x;
  unsigned int exponent;

  exponent = u->ieee.exponent;

  /* The order of the tests is carefully chosen to handle
     the usual case first, with no branches taken.  */

  if (exponent != 0)
    {
      /* X is nonzero and not denormalized.  */

      if (exponent <= LDBL_MAX_EXP - LDBL_MIN_EXP + 1)
  	{
	  /* X is finite.  When EXP < 0, overflow is actually underflow.  */

	  exponent += exp;

	  if (exponent != 0)
	    {
	      if (exponent <= LDBL_MAX_EXP - LDBL_MIN_EXP + 1)
		{
		  /* In range.  */
		  u->ieee.exponent = exponent;
		  return x;
		}

	      if (exp >= 0)
	      overflow:
		{
		  CONST int negative = u->ieee.negative;
/*		  x = HUGE_VAL; */
		  x = 1.189731495357231765021263853E4932L;
		  u->ieee.negative = negative;
		  errno = ERANGE;
		  return x;
		}

	      if (exponent <= - (unsigned int) (LDBL_MANT_DIG + 1))
		{
		  /* Underflow.  */
		  CONST int negative = u->ieee.negative;
		  x = 0.0L;
		  u->ieee.negative = negative;
		  errno = ERANGE;
		  return x;
		}
	    }

	  /* Gradual underflow.  */
	  u->ieee.exponent = 1;
	  x *= ldexpl (1.0L, (int) exponent - 1);
	  if (u->ieee.mantissa0 == 0 && u->ieee.mantissa1 == 0)
	    /* Underflow.  */
	    errno = ERANGE;
	  return x;
  	}

      /* X is +-infinity or NaN.  */
      if (u->ieee.mantissa0 == 0 && u->ieee.mantissa1 == 0)
  	{
	  /* X is +-infinity.  */
	  if (exp >= 0)
	    goto overflow;
	  else
	    {
	      /* (infinity * number < 1).  With infinite precision,
		 (infinity / finite) would be infinity, but otherwise it's
		 safest to regard (infinity / 2) as indeterminate.  The
		 infinity might be (2 * finite).  */
	      CONST int negative = u->ieee.negative;
	      /* Create a NaN.  */
	      x = __infnanl(EDOM);
	      u->ieee.negative = negative;
	      errno = EDOM;
	      return x;
	    }
	}

      /* X is NaN.  */
      errno = EDOM;
      return x;
    }

  /* X is zero or denormalized.  */
  if (u->ieee.mantissa0 == 0 && u->ieee.mantissa1 == 0)
    /* X is +-0.0. */
    return x;

  /* X is denormalized.
     Multiplying by 2 ** LDBL_MANT_DIG normalizes it;
     we then subtract the LDBL_MANT_DIG we added to the exponent.  */
  return ldexpl (x * ldexpl (1.0L, LDBL_MANT_DIG), exp - LDBL_MANT_DIG);
}
