Blame sysdeps/aarch64/fpu/s_lrint.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <get-rounding-mode.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <libm-alias-double.h>
Packit 6c4009
Packit 6c4009
# define IREG_SIZE 64
Packit 6c4009
Packit 6c4009
# ifdef __ILP32__
Packit 6c4009
#  define OREG_SIZE 32
Packit 6c4009
# else
Packit 6c4009
#  define OREG_SIZE 64
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# define IREGS "d"
Packit 6c4009
Packit 6c4009
#if OREG_SIZE == 32
Packit 6c4009
# define OREGS "w"
Packit 6c4009
#else
Packit 6c4009
# define OREGS "x"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
long int
Packit 6c4009
__lrint (double x)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
#if IREG_SIZE == 64 && OREG_SIZE == 32
Packit 6c4009
  long int result;
Packit 6c4009
Packit 6c4009
  if (__builtin_fabs (x) > INT32_MAX)
Packit 6c4009
    {
Packit 6c4009
      /* Converting large values to a 32 bit int may cause the frintx/fcvtza
Packit 6c4009
	 sequence to set both FE_INVALID and FE_INEXACT.  To avoid this
Packit 6c4009
	 check the rounding mode and do a single instruction with the
Packit 6c4009
	 appropriate rounding mode.  */
Packit 6c4009
Packit 6c4009
      switch (get_rounding_mode ())
Packit 6c4009
	{
Packit 6c4009
	case FE_TONEAREST:
Packit 6c4009
	  asm volatile ("fcvtns" "\t%" OREGS "0, %" IREGS "1"
Packit 6c4009
			: "=r" (result) : "w" (x));
Packit 6c4009
	  break;
Packit 6c4009
	case FE_UPWARD:
Packit 6c4009
	  asm volatile ("fcvtps" "\t%" OREGS "0, %" IREGS "1"
Packit 6c4009
			: "=r" (result) : "w" (x));
Packit 6c4009
	  break;
Packit 6c4009
	case FE_DOWNWARD:
Packit 6c4009
	  asm volatile ("fcvtms" "\t%" OREGS "0, %" IREGS "1"
Packit 6c4009
			: "=r" (result) : "w" (x));
Packit 6c4009
	  break;
Packit 6c4009
	case FE_TOWARDZERO:
Packit 6c4009
	default:
Packit 6c4009
	  asm volatile ("fcvtzs" "\t%" OREGS "0, %" IREGS "1"
Packit 6c4009
			: "=r" (result) : "w" (x));
Packit 6c4009
	}
Packit 6c4009
      return result;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  double r =  __builtin_rint (x);
Packit 6c4009
Packit 6c4009
  /* Prevent gcc from calling lrint directly when compiled with
Packit 6c4009
     -fno-math-errno by inserting a barrier.  */
Packit 6c4009
Packit 6c4009
  math_opt_barrier (r);
Packit 6c4009
  return r;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
libm_alias_double (__lrint, lrint)