Blame lib/os.h

Packit 06404a
#ifndef _OS_H
Packit 06404a
#define _OS_H
Packit 06404a
/********************************************************************
Packit 06404a
 *                                                                  *
Packit 06404a
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Packit 06404a
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
Packit 06404a
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
Packit 06404a
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Packit 06404a
 *                                                                  *
Packit 06404a
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
Packit 06404a
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
Packit 06404a
 *                                                                  *
Packit 06404a
 ********************************************************************
Packit 06404a
Packit 06404a
 function: #ifdef jail to whip a few platforms into the UNIX ideal.
Packit 06404a
 last mod: $Id: os.h 19457 2015-03-03 00:15:29Z giles $
Packit 06404a
Packit 06404a
 ********************************************************************/
Packit 06404a
Packit 06404a
#ifdef HAVE_CONFIG_H
Packit 06404a
#include "config.h"
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#include <math.h>
Packit 06404a
#include <ogg/os_types.h>
Packit 06404a
Packit 06404a
#include "misc.h"
Packit 06404a
Packit 06404a
#ifndef _V_IFDEFJAIL_H_
Packit 06404a
#  define _V_IFDEFJAIL_H_
Packit 06404a
Packit 06404a
#  ifdef __GNUC__
Packit 06404a
#    define STIN static __inline__
Packit 06404a
#  elif _WIN32
Packit 06404a
#    define STIN static __inline
Packit 06404a
#  else
Packit 06404a
#    define STIN static
Packit 06404a
#  endif
Packit 06404a
Packit 06404a
#ifdef DJGPP
Packit 06404a
#  define rint(x)   (floor((x)+0.5f))
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifndef M_PI
Packit 06404a
#  define M_PI (3.1415926536f)
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#if defined(_WIN32) && !defined(__SYMBIAN32__)
Packit 06404a
#  include <malloc.h>
Packit 06404a
#  define rint(x)   (floor((x)+0.5f))
Packit 06404a
#  define NO_FLOAT_MATH_LIB
Packit 06404a
#  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#if defined(__SYMBIAN32__) && defined(__WINS__)
Packit 06404a
void *_alloca(size_t size);
Packit 06404a
#  define alloca _alloca
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifndef FAST_HYPOT
Packit 06404a
#  define FAST_HYPOT hypot
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifdef HAVE_ALLOCA_H
Packit 06404a
#  include <alloca.h>
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifdef USE_MEMORY_H
Packit 06404a
#  include <memory.h>
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifndef min
Packit 06404a
#  define min(x,y)  ((x)>(y)?(y):(x))
Packit 06404a
#endif
Packit 06404a
Packit 06404a
#ifndef max
Packit 06404a
#  define max(x,y)  ((x)<(y)?(y):(x))
Packit 06404a
#endif
Packit 06404a
Packit 06404a
Packit 06404a
/* Special i386 GCC implementation */
Packit 06404a
#if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
Packit 06404a
#  define VORBIS_FPU_CONTROL
Packit 06404a
/* both GCC and MSVC are kinda stupid about rounding/casting to int.
Packit 06404a
   Because of encapsulation constraints (GCC can't see inside the asm
Packit 06404a
   block and so we end up doing stupid things like a store/load that
Packit 06404a
   is collectively a noop), we do it this way */
Packit 06404a
Packit 06404a
/* we must set up the fpu before this works!! */
Packit 06404a
Packit 06404a
typedef ogg_int16_t vorbis_fpu_control;
Packit 06404a
Packit 06404a
static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
Packit 06404a
  ogg_int16_t ret;
Packit 06404a
  ogg_int16_t temp;
Packit 06404a
  __asm__ __volatile__("fnstcw %0\n\t"
Packit 06404a
          "movw %0,%%dx\n\t"
Packit 06404a
          "andw $62463,%%dx\n\t"
Packit 06404a
          "movw %%dx,%1\n\t"
Packit 06404a
          "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
Packit 06404a
  *fpu=ret;
Packit 06404a
}
Packit 06404a
Packit 06404a
static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
Packit 06404a
  __asm__ __volatile__("fldcw %0":: "m"(fpu));
Packit 06404a
}
Packit 06404a
Packit 06404a
/* assumes the FPU is in round mode! */
Packit 06404a
static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
Packit 06404a
                                             we get extra fst/fld to
Packit 06404a
                                             truncate precision */
Packit 06404a
  int i;
Packit 06404a
  __asm__("fistl %0": "=m"(i) : "t"(f));
Packit 06404a
  return(i);
Packit 06404a
}
Packit 06404a
#endif /* Special i386 GCC implementation */
Packit 06404a
Packit 06404a
Packit 06404a
/* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
Packit 06404a
 * 64 bit compiler and doesn't work on arm. */
Packit 06404a
#if defined(_MSC_VER) && !defined(_WIN64) && \
Packit 06404a
      !defined(_WIN32_WCE) && !defined(_M_ARM)
Packit 06404a
#  define VORBIS_FPU_CONTROL
Packit 06404a
Packit 06404a
typedef ogg_int16_t vorbis_fpu_control;
Packit 06404a
Packit 06404a
static __inline int vorbis_ftoi(double f){
Packit 06404a
        int i;
Packit 06404a
        __asm{
Packit 06404a
                fld f
Packit 06404a
                fistp i
Packit 06404a
        }
Packit 06404a
        return i;
Packit 06404a
}
Packit 06404a
Packit 06404a
static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
Packit 06404a
  (void)fpu;
Packit 06404a
}
Packit 06404a
Packit 06404a
static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
Packit 06404a
  (void)fpu;
Packit 06404a
}
Packit 06404a
Packit 06404a
#endif /* Special MSVC 32 bit implementation */
Packit 06404a
Packit 06404a
Packit 06404a
/* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
Packit 06404a
   done safely because all x86_64 CPUs supports SSE2. */
Packit 06404a
#if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
Packit 06404a
#  define VORBIS_FPU_CONTROL
Packit 06404a
Packit 06404a
typedef ogg_int16_t vorbis_fpu_control;
Packit 06404a
Packit 06404a
#include <emmintrin.h>
Packit 06404a
static __inline int vorbis_ftoi(double f){
Packit 06404a
        return _mm_cvtsd_si32(_mm_load_sd(&f);;
Packit 06404a
}
Packit 06404a
Packit 06404a
static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
Packit 06404a
  (void)fpu;
Packit 06404a
}
Packit 06404a
Packit 06404a
static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
Packit 06404a
  (void)fpu;
Packit 06404a
}
Packit 06404a
Packit 06404a
#endif /* Special MSVC x64 implementation */
Packit 06404a
Packit 06404a
Packit 06404a
/* If no special implementation was found for the current compiler / platform,
Packit 06404a
   use the default implementation here: */
Packit 06404a
#ifndef VORBIS_FPU_CONTROL
Packit 06404a
Packit 06404a
typedef int vorbis_fpu_control;
Packit 06404a
Packit 06404a
static int vorbis_ftoi(double f){
Packit 06404a
        /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
Packit 06404a
           the floor() call is required to ensure correct roudning of
Packit 06404a
           negative numbers */
Packit 06404a
        return (int)floor(f+.5);
Packit 06404a
}
Packit 06404a
Packit 06404a
/* We don't have special code for this compiler/arch, so do it the slow way */
Packit 06404a
#  define vorbis_fpu_setround(vorbis_fpu_control) {}
Packit 06404a
#  define vorbis_fpu_restore(vorbis_fpu_control) {}
Packit 06404a
Packit 06404a
#endif /* default implementation */
Packit 06404a
Packit 06404a
#endif /* _OS_H */