sign.h 1.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2018   Matthieu Schaller (matthieu.schaller@durham.ac.uk).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/
#ifndef SWIFT_SIGN_H
#define SWIFT_SIGN_H

/**
 * @brief Return the sign of a floating point number
 *
 * @param x The number of interest.
 * @return >0 if positive, 0 if negative.
 */
28
__attribute__((always_inline, const)) INLINE static int signf(float x) {
29 30 31 32 33 34 35 36 37 38 39 40 41
#ifdef __GNUC__
  return !signbit(x);
#else
  return (0.f < val) - (val < 0.f);
#endif
}

/**
 * @brief Return 1 if two numbers have the same sign, 0 otherwise
 *
 * @param x The first number
 * @param y The second number
 */
42 43
__attribute__((always_inline, const)) INLINE static int same_signf(float x,
                                                                   float y) {
44 45 46 47
  return signf(x) == signf(y);
}

#endif