pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plank_StandardHeader.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLANK_STANDARDHEADER_H
00040 #define PLANK_STANDARDHEADER_H
00041 
00042 #define _CRT_SECURE_NO_WARNINGS
00043 
00044 /*
00045  config - define these in preprocessor macros to enable special options
00046  PLANK_FFT_VDSP=1           -   use vDSP on Mac OS X for FFT routines
00047  PLANK_FFT_VDSP_FLIPIMAG=1  -   flip the imag part of the FFT to match FFTReal data closely
00048  PLANK_VEC_VDSP 1           -   use vDSP on Mac OS X for vector ops
00049 */
00050 
00051 #ifndef PLANK_API
00052 #define PLANK_API
00053 #endif
00054 
00055 #define PLANK_CDECL
00056 
00057 #ifdef __cplusplus
00058         #define PLANK_UNUSED(a) (void)a
00059     #define PLANK_BEGIN_C_LINKAGE extern "C" {
00060     #define PLANK_END_C_LINKAGE }
00061 #else
00062     #if (defined (_WIN32) || defined (_WIN64))
00063         #define inline __inline
00064                 #define PLANK_UNUSED(a) (void)a
00065     #else
00066                 #define PLANK_UNUSED(a)
00067         #endif
00068     #define PLANK_BEGIN_C_LINKAGE
00069     #define PLANK_END_C_LINKAGE
00070 #endif        
00071 
00072 #define PLANK_NAMESPACE plank
00073 
00074 #ifdef PLANK_NAMESPACE
00075     #define BEGIN_PLANK_NAMESPACE    namespace PLANK_NAMESPACE {
00076     #define END_PLANK_NAMESPACE      }
00077 #else
00078     #define BEGIN_PLANK_NAMESPACE 
00079     #define END_PLANK_NAMESPACE   
00080 #endif
00081 
00082 #if !defined(_NDEBUG) && !defined(NDEBUG) && !defined(_DEBUG) && !defined(DEBUG)
00083     #warning You should define appropriate preprocessor macros in Debug (_DEBUG=1 DEBUG=1) and Release (_NDEBUG=1 NDEBUG=1)
00084 #endif
00085 
00086 #ifndef PLANK_DEBUG
00087     #if !defined(NDEBUG) || defined(_DEBUG)
00088         #define PLANK_DEBUG 1
00089     #endif
00090 #else
00091     #if !PLANK_DEBUG
00092         #undef PLANK_DEBUG
00093     #endif
00094 #endif
00095 
00096 #include <stdlib.h>
00097 #include <stdio.h>
00098 #include <stdint.h>
00099 #include <string.h>
00100 #include <math.h>
00101 #include <time.h>
00102 #include <limits.h>
00103 
00104 #if (defined (_WIN32) || defined (_WIN64) || defined (WIN64))
00105     #include <windows.h>
00106     #include <process.h>
00107     #include <float.h>
00108     #include <intrin.h>
00109     #include <direct.h>
00110 
00111         #undef min
00112         #undef max
00113 
00114         #pragma warning(disable : 4244) // loss of precision
00115         #pragma warning(disable : 4996) // security of snprintf etc
00116         #pragma warning(disable : 4324) // structure padding / alignment
00117         #pragma warning(disable : 4214) // bit field types other than int.. must revisit this
00118         #pragma warning(disable : 4054) // type cast function pointer to data pointer
00119         #pragma warning(disable : 4055) // type cast data pointer to function pointer
00120         #pragma warning(disable : 4127) // conditional expression is constant (e.g., while(1){...} idiom)
00121         #pragma warning(disable : 4706) // assignment within conditional expression
00122         #pragma warning(disable : 4206) // empty compilation unit
00123 
00124     #define PLANK_WIN 1
00125     #define PLANK_X86 1
00126 
00127         #if defined(_WIN64) || defined(WIN64)
00128         typedef signed __int64 PlankL;
00129         typedef unsigned __int64 PlankUL;
00130         #define PLANK_64BIT 1
00131     #elif defined(_WIN32)
00132         #define PLANK_32BIT 1
00133         typedef signed long PlankL;
00134         typedef unsigned long PlankUL;
00135     #endif
00136 
00137         typedef signed __int64 PlankLL;
00138         typedef unsigned __int64 PlankULL;
00139         typedef PlankULL PlankBits;
00140 
00141     #define PLANK_LL_MAX   _I64_MAX 
00142     #define PLANK_ULL_MAX  _UI64_MAX 
00143 
00144     #define snprintf _snprintf
00145     //#define strncpy _strncpy
00146 
00147     #define PLANK_ALIGN(amount) __declspec(align(amount))
00148 #else
00149     // assume unix and Apple 
00150         #include <unistd.h>
00151     #include <pthread.h>
00152     #include <sys/time.h>
00153 
00154     typedef signed long PlankL;
00155     typedef unsigned long PlankUL;
00156     typedef signed long long PlankLL;
00157     typedef unsigned long long PlankULL;
00158         typedef PlankULL PlankBits;
00159 
00160     #define PLANK_LL_MAX LLONG_MAX
00161     #define PLANK_ULL_MAX ULLONG_MAX
00162 
00163     #if defined (__APPLE_CPP__) || defined(__APPLE_CC__)
00164         #define PLANK_APPLE 1
00165         #include <CoreFoundation/CoreFoundation.h> 
00166         #include <libkern/OSAtomic.h>
00167 
00168         #ifdef __llvm__
00169             #ifdef __clang__
00170                 #define PLANK_APPLE_LLVM 1
00171             #else
00172                 #define PLANK_GCC 1
00173                 #define PLANK_LLVM 1
00174             #endif
00175         #else
00176             #define PLANK_GCC 1
00177         #endif
00178 
00179         // no __has_feature on xcode3.2
00180         #if defined(__has_feature)
00181             #if __has_feature(objc_arc)
00182                 #define PLANK_OBJC_BRIDGE           __bridge
00183                 #define PLANK_OBJC_BRIDGE_RETAINED  __bridge_retained
00184                 #define PLANK_OBJC_BRIDGE_TRANSFER  __bridge_transfer
00185                 #define PLANK_OBJC_STRONG           __strong
00186                 #define PLANK_OBJC_WEAK             __weak
00187                 #define PLANK_OBJC_UNSAFE_RETAINED  __unsafe_unretained
00188                 #define PLANK_OBJC_AUTORELEASING    __autoreleasing
00189             #endif
00190         #endif
00191 
00192         #ifndef PLANK_OBJC_BRIDGE
00193         #define PLANK_OBJC_BRIDGE
00194         #endif
00195 
00196         #ifndef PLANK_OBJC_BRIDGE_RETAINED
00197         #define PLANK_OBJC_BRIDGE_RETAINED
00198         #endif
00199 
00200         #ifndef PLANK_OBJC_BRIDGE_TRANSFER
00201         #define PLANK_OBJC_BRIDGE_TRANSFER
00202         #endif 
00203 
00204         #ifndef PLANK_OBJC_STRONG
00205         #define PLANK_OBJC_STRONG
00206         #endif
00207 
00208         #ifndef PLANK_OBJC_WEAK
00209         #define PLANK_OBJC_WEAK
00210         #endif
00211 
00212         #ifndef PLANK_OBJC_UNSAFE_RETAINED
00213         #define PLANK_OBJC_UNSAFE_RETAINED
00214         #endif
00215 
00216         #ifndef PLANK_OBJC_AUTORELEASING
00217         #define PLANK_OBJC_AUTORELEASING
00218         #endif
00219 
00220         #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
00221             #define PLANK_IOS 1
00222 
00223             #if TARGET_IPHONE_SIMULATOR
00224                 #define PLANK_X86 1
00225             #else
00226                 #define PLANK_ARM 1
00227             #endif
00228         #else
00229             #define PLANK_MAC 1
00230 
00231             #include <mach/mach.h>
00232             #include <mach/mach_init.h>
00233             #include <mach/thread_policy.h>
00234 
00235             #if defined (__ppc__) || defined (__ppc64__)
00236                 #define PLANK_PPC 1
00237             #else
00238                 #define PLANK_X86 1
00239             #endif
00240         #endif
00241     #elif defined(__ANDROID__)
00242         #define PLANK_ANDROID 1
00243 
00244         #define PLANK_GCC 1
00245 
00246         #ifdef __llvm__
00247             #define PLANK_LLVM 1
00248         #endif
00249 
00250         #if defined(__i386__)
00251             #define PLANK_X86 1
00252         #elif defined(__arm__)
00253             #define PLANK_ARM 1
00254         #else
00255             #error Target CPU not yet supported for PLANK_ANDROID
00256         #endif
00257     #elif defined(__linux__)
00258         #define PLANK_LINUX 1
00259 
00260         #define PLANK_GCC 1
00261 
00262         #ifdef __llvm__
00263             #define PLANK_LLVM 1
00264         #endif
00265 
00266         // probably avoid needing to know the CPU for linux...
00267         #if defined(__i386__)
00268             #define PLANK_X86 1
00269         #elif defined(__arm__)
00270             #define PLANK_ARM 1
00271         #elif defined (__ppc__) || defined (__ppc64__)
00272             #define PLANK_PPC 1
00273         #else
00274             #define PLANK_UNKNOWNCPU 1
00275         #endif
00276     #endif
00277 
00278     #if LONG_MAX == 0x7fffffff
00279         #define PLANK_32BIT 1
00280     #else
00281         #define PLANK_64BIT 1
00282     #endif
00283 
00284     #define PLANK_ALIGN(amount)   __attribute__ ((aligned (amount)))
00285 #endif
00286 
00287 #if PLANK_32BIT
00288     #define PLANK_WORDBITS 32
00289     #define PLANK_HALFWORDBITS 16
00290     #define PLANK_WORDSIZE 4
00291     #define PLANK_WIDESIZE 8
00292 #endif
00293 
00294 #if PLANK_64BIT
00295     #define PLANK_WORDBITS 64
00296     #define PLANK_HALFWORDBITS 32
00297     #define PLANK_WORDSIZE 8
00298     #define PLANK_WIDESIZE 16
00299 #endif
00300 
00301 typedef float PlankF;
00302 typedef double PlankD;
00303 
00304 typedef char PlankC;
00305 typedef int PlankI;
00306 typedef short PlankS;
00307 
00308 typedef unsigned char PlankUC;
00309 typedef unsigned int PlankUI;
00310 typedef unsigned short PlankUS;
00311 
00312 typedef PlankUL PlankB;
00313 #define PLANK_FALSE ((PlankB)0)
00314 #define PLANK_TRUE ((PlankB)(!0))
00315 
00316 typedef void* PlankP;
00317 typedef void (*PlankM) (void); // method
00318 typedef const void* PlankConstantP;
00319 #define PLANK_NULL ((PlankP)0)
00320 
00321 typedef PlankUI PlankFourCharCode; // hope this didn't break
00322 
00323 typedef struct PlankFourCharCodeString
00324 {
00325     char string[5];
00326 } PlankFourCharCodeString;
00327 
00328 #ifdef INFINITY
00329     #define PLANK_INFINITY (INFINITY)
00330 #else
00331     union PLANK_MSVC_EVIL_FLOAT_HACK
00332     {
00333         unsigned __int8 Bytes[4];
00334         float Value;
00335     };
00336     static union PLANK_MSVC_EVIL_FLOAT_HACK PLANK_INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
00337     #define PLANK_INFINITY (PLANK_INFINITY_HACK.Value)
00338 #endif
00339 
00340 #define PLANK_FLOAT_ONE         0x3f800000
00341 #define PLANK_FLOAT_ONEMASK     0x007fffff
00342 #define PLANK_DOUBLE_ONE        0x3ff0000000000000ULL
00343 #define PLANK_DOUBLE_ONEMASK    0x000fffffffffffffULL
00344 #define PLANK_INT24_MAX         0x7fffff
00345 #define PLANK_CHARBITS          8
00346 
00347 #include "plank_Result.h"
00348 
00349 #if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
00350     #define PLANK_LITTLEENDIAN  0
00351     #define PLANK_BIGENDIAN     1
00352 #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
00353     #define PLANK_LITTLEENDIAN  1
00354     #define PLANK_BIGENDIAN     0
00355 #elif defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
00356     #define PLANK_LITTLEENDIAN  0
00357     #define PLANK_BIGENDIAN     1
00358 #elif defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
00359     #define PLANK_LITTLEENDIAN  1
00360     #define PLANK_BIGENDIAN     0
00361 #elif defined(__sparc) || defined(__sparc__) \
00362     || defined(_POWER) || defined(__powerpc__) \
00363     || defined(__ppc__) || defined(__hpux) \
00364     || defined(_MIPSEB) || defined(_POWER) \
00365     || defined(__s390__)
00366     #define PLANK_LITTLEENDIAN  0
00367     #define PLANK_BIGENDIAN     1
00368 #elif defined(__i386__) || defined(__alpha__) \
00369     || defined(__ia64) || defined(__ia64__) \
00370     || defined(_M_IX86) || defined(_M_IA64) \
00371     || defined(_M_ALPHA) || defined(__amd64) \
00372     || defined(__amd64__) || defined(_M_AMD64) \
00373     || defined(__x86_64) || defined(__x86_64__) \
00374     || defined(_M_X64) || defined(__bfin__)
00375     #define PLANK_LITTLEENDIAN  1
00376     #define PLANK_BIGENDIAN     0
00377 #elif __BYTE_ORDER == _BIG_ENDIAN
00378     #define PLANK_LITTLEENDIAN  0
00379     #define PLANK_BIGENDIAN     1
00380 #elif __BYTE_ORDER == _LITTLE_ENDIAN
00381     #define PLANK_LITTLEENDIAN  1
00382     #define PLANK_BIGENDIAN     0
00383 #endif
00384 
00385 // 
00386 
00387 #include "plank_Memory.h"
00388 
00389 // this must be after the PLANK_LITTLEENDIAN / PLANK_BIGENDIAN defs
00390 #include "../containers/plank_ContainersMisc.h"
00391 
00392 #define PLANK_NAMEJOININTERNAL2(a,b)        a ## b
00393 #define PLANK_NAMEJOININTERNAL3(a,b,c)      a ## b ## c
00394 #define PLANK_NAMEJOININTERNAL4(a,b,c,d)    a ## b ## c ## d
00395 
00396 #define PLANK_NAMEJOIN2(a,b)       PLANK_NAMEJOININTERNAL2(a,b)
00397 #define PLANK_NAMEJOIN3(a,b,c)     PLANK_NAMEJOININTERNAL3(a,b,c)
00398 #define PLANK_NAMEJOIN4(a,b,c,d)   PLANK_NAMEJOININTERNAL4(a,b,c,d)
00399 
00400 #define PLANK_PRESTRING2(a) #a
00401 #define PLANK_PRESTRING(a) PLANK_PRESTRING2(a)
00402 
00403 #define PLANK_MAJOR_VERSION      0
00404 #define PLANK_MINOR_VERSION      4
00405 #define PLANK_BUILDNUMBER        5
00406 #define PLANK_VERSION           "v" PLANK_PRESTRING(PLANK_MAJOR_VERSION) "." PLANK_PRESTRING(PLANK_MINOR_VERSION) "." PLANK_PRESTRING(PLANK_BUILDNUMBER)
00407 
00408 static inline double pl_TimeNow()
00409 {
00410 #if PLANK_APPLE || PLANK_LINUX || PLANK_ANDROID
00411     struct timeval now;
00412     gettimeofday (&now, 0);
00413     return (double)now.tv_sec + now.tv_usec * 0.000001;
00414 #else
00415     return 0.0;
00416 #endif
00417 }
00418 
00419 #if PLANK_APPLE || PLANK_LINUX || PLANK_ANDROID
00420 static inline void pl_TimeToTimeSpec (struct timespec* time, double seconds)
00421 {
00422     time->tv_sec = (long)seconds;
00423     time->tv_nsec = (long)((seconds - time->tv_sec) * 0.000000001);
00424 }
00425 #endif
00426 
00427 
00428 #endif // PLANK_STANDARDHEADER_H
 All Classes Functions Typedefs Enumerations Enumerator Properties