Blame include/rounding-mode.h

Packit 6c4009
/* Handle floating-point rounding mode within libc.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
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
#ifndef _ROUNDING_MODE_H
Packit 6c4009
#define _ROUNDING_MODE_H	1
Packit 6c4009
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
/* Get the architecture-specific definition of how to determine the
Packit 6c4009
   rounding mode in libc.  This header must also define the FE_*
Packit 6c4009
   macros for any standard rounding modes the architecture does not
Packit 6c4009
   have in <fenv.h>, to arbitrary distinct values.  */
Packit 6c4009
#include <get-rounding-mode.h>
Packit 6c4009
Packit 6c4009
/* Return true if a number should be rounded away from zero in
Packit 6c4009
   rounding mode MODE, false otherwise.  NEGATIVE is true if the
Packit 6c4009
   number is negative, false otherwise.  LAST_DIGIT_ODD is true if the
Packit 6c4009
   last digit of the truncated value (last bit for binary) is odd,
Packit 6c4009
   false otherwise.  HALF_BIT is true if the number is at least half
Packit 6c4009
   way from the truncated value to the next value with the
Packit 6c4009
   least-significant digit in the same place, false otherwise.
Packit 6c4009
   MORE_BITS is true if the number is not exactly equal to the
Packit 6c4009
   truncated value or the half-way value, false otherwise.  */
Packit 6c4009
Packit 6c4009
static bool
Packit 6c4009
round_away (bool negative, bool last_digit_odd, bool half_bit, bool more_bits,
Packit 6c4009
	    int mode)
Packit 6c4009
{
Packit 6c4009
  switch (mode)
Packit 6c4009
    {
Packit 6c4009
    case FE_DOWNWARD:
Packit 6c4009
      return negative && (half_bit || more_bits);
Packit 6c4009
Packit 6c4009
    case FE_TONEAREST:
Packit 6c4009
      return half_bit && (last_digit_odd || more_bits);
Packit 6c4009
Packit 6c4009
    case FE_TOWARDZERO:
Packit 6c4009
      return false;
Packit 6c4009
Packit 6c4009
    case FE_UPWARD:
Packit 6c4009
      return !negative && (half_bit || more_bits);
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* rounding-mode.h */