medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 8fbced816bf0d1af2daf93dbcd2310feb4a16798
parent a3209ba9783f625e6325e6f6080d00b28082482f
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Aug 20 20:55:37 +0100

Add CONCAT/clamp/MIN/MAXs to intrinsics.h

Diffstat:
intrinsics.h | 33++++++++++++++++++++++++++++++++-
memory_arena.h | 3+--
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/intrinsics.h b/intrinsics.h @@ -23,6 +23,16 @@ typedef uintptr_t uptr; typedef float f32; typedef double f64; +#define S8_MAX s8( INT8_MAX ) +#define S16_MAX s16( INT16_MAX ) +#define S32_MAX s32( INT32_MAX ) +#define S64_MAX s64( INT64_MAX ) + +#define U8_MAX u8( UINT8_MAX ) +#define U16_MAX u16( UINT16_MAX ) +#define U32_MAX u32( UINT32_MAX ) +#define U64_MAX u64( UINT64_MAX ) + #define array_count( x ) ( sizeof( x ) / sizeof( ( x )[ 0 ] ) ) #define is_power_of_2( n ) ( ( ( n ) & ( ( n ) - 1 ) ) == 0 ) @@ -39,7 +49,7 @@ typedef double f64; #define gigabytes( gb ) ( megabytes( gb ) * size_t( 1024 ) ) #define CACHE_LINE_SIZE 64 -#define CACHE_LINE_PADDING u8 cache_line_spacing##__COUNTER__[ CACHE_LINE_SIZE ] +#define CACHE_LINE_PADDING u8 CONCAT( cache_line_spacing, __COUNTER__ )[ CACHE_LINE_SIZE ] template< typename T > T min( T a, T b ) { @@ -56,6 +66,23 @@ T abs( T x ) { return x > 0 ? x : -x; } +template< typename T > +T clamp( T x, T lo, T hi ) { + if( x < lo ) return lo; + if( x > hi ) return hi; + return x; +} + +template< typename T > +T clamp01( T x ) { + return clamp( x, T( 0 ), T( 1 ) ); +} + +template< typename T > +T clamp11( T x ) { + return clamp( x, T( -1 ), T( 1 ) ); +} + #include "platform_backtrace.h" #if defined( assert ) @@ -74,6 +101,10 @@ inline void mike_assert( const bool predicate, const char * const message ) { #define STRINGIFY_HELPER( x ) #x #define STRINGIFY( x ) STRINGIFY_HELPER( x ) + +#define CONCAT_HELPER( a, b ) a##b +#define CONCAT( a, b ) CONCAT_HELPER( a, b ) + #define assert( predicate ) mike_assert( predicate, "\x1b[1;31massertion failed at " __FILE__ " line " STRINGIFY( __LINE__ ) ": \x1b[0;1m" #predicate "\x1b[0m" ) // TODO: this sucks diff --git a/memory_arena.h b/memory_arena.h @@ -37,7 +37,6 @@ void memarena_clear( MemoryArena * const arena ); MemoryArenaCheckpoint memarena_checkpoint( MemoryArena * const arena ); void memarena_restore( MemoryArena * arena, MemoryArenaCheckpoint * const cp ); -#define MEMARENA_SCOPED_CHECKPOINT( arena ) MemoryArenaAutoCheckpoint mem_cp##__COUNTER__( arena, memarena_checkpoint( arena ) ); - +#define MEMARENA_SCOPED_CHECKPOINT( arena ) MemoryArenaAutoCheckpoint CONCAT( mem_cp, __COUNTER__ )( arena, memarena_checkpoint( arena ) ); #endif // _MEMORY_ARENA_H_