Blame lib/fpclassify.c

Packit 0b5880
/* Copyright (C) 2017, bel2125
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
/* Note: Within "check", this file uses the LGPL license.
Packit 0b5880
 * If you need only this file, you may find a MIT licensed version in
Packit 0b5880
 * the "floatmagic" repository of the initial author.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "libcompat.h"
Packit 0b5880
Packit 0b5880
double DOUBLE_ZERO = 0.0;
Packit 0b5880
Packit 0b5880
#if defined(NEED_fpclassify)
Packit 0b5880
Packit 0b5880
#if defined(HAVE_STDINT_H)
Packit 0b5880
#include <stdint.h>
Packit 0b5880
typedef uint64_t bitfield64;
Packit 0b5880
#elif defined(_MSC_VER)
Packit 0b5880
typedef unsigned __int64 bitfield64;
Packit 0b5880
#else
Packit 0b5880
typedef unsigned long long bitfield64;
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
static bitfield64 ms = 0x8000000000000000;
Packit 0b5880
static bitfield64 me = 0x7FF0000000000000;
Packit 0b5880
static bitfield64 mf = 0x000FFFFFFFFFFFFF;
Packit 0b5880
Packit 0b5880
int fpclassify(double d) 
Packit 0b5880
{
Packit 0b5880
  bitfield64 *p = (bitfield64 *)&d;
Packit 0b5880
  if ((*p & me) != me) {
Packit 0b5880
    /* finite */
Packit 0b5880
    if (*p & mf) {
Packit 0b5880
      /* finite and not null */
Packit 0b5880
      if (*p & me) {
Packit 0b5880
        return FP_NORMAL;
Packit 0b5880
      }
Packit 0b5880
      return FP_SUBNORMAL;
Packit 0b5880
    }	
Packit 0b5880
    return FP_ZERO;
Packit 0b5880
  }
Packit 0b5880
  if (*p & mf) {
Packit 0b5880
    return FP_NAN;
Packit 0b5880
  }
Packit 0b5880
  return FP_INFINITE;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
#endif
Packit 0b5880