Ejemplo n.º 1
0
static FOUNDATION_CONSTCALL FOUNDATION_FORCEINLINE int unsigned
_memory_get_align(unsigned int align) {
	//All alignment in memory code is built around higher alignments
	//being multiples of lower alignments (powers of two).
	//4, 8, 16, ...
#if FOUNDATION_PLATFORM_ANDROID
	return align > 0 ? FOUNDATION_MAX_ALIGN : 0;
#elif FOUNDATION_PLATFORM_WINDOWS
	if (align < FOUNDATION_SIZE_POINTER)
		return FOUNDATION_SIZE_POINTER;
	align = math_align_poweroftwo(align);
	return (align < FOUNDATION_MAX_ALIGN) ? align : FOUNDATION_MAX_ALIGN;
#else
	if (align < FOUNDATION_SIZE_POINTER)
		return align ? FOUNDATION_SIZE_POINTER : 0;
	align = math_align_poweroftwo(align);
	return (align < FOUNDATION_MAX_ALIGN) ? align : FOUNDATION_MAX_ALIGN;
#endif
}
Ejemplo n.º 2
0
static CONSTCALL FORCEINLINE unsigned int _memory_get_align( unsigned int align )
{
	if( align < FOUNDATION_PLATFORM_POINTER_SIZE )
	{
#if FOUNDATION_PLATFORM_ANDROID
		return sizeof( uint64_t );
#else
		return align ? FOUNDATION_PLATFORM_POINTER_SIZE : 0;
#endif
	}
	align = math_align_poweroftwo( align );
	return ( align < ( FOUNDATION_PLATFORM_POINTER_SIZE * 4 ) ) ? align : ( FOUNDATION_PLATFORM_POINTER_SIZE * 4 );
}
Ejemplo n.º 3
0
DECLARE_TEST( math, utility )
{
	int i;
	
	EXPECT_REALONE( math_abs( REAL_ONE ) );
	EXPECT_REALONE( math_abs( -REAL_ONE ) );
	EXPECT_REALZERO( math_abs( REAL_ZERO ) );
	EXPECT_REALEQ( math_abs( REAL_MAX ), REAL_MAX );
	EXPECT_REALEQ( math_abs( -REAL_MAX ), REAL_MAX );
	EXPECT_REALEQ( math_abs( REAL_MIN ), REAL_MIN );
	EXPECT_REALEQ( math_abs( -REAL_MIN ), REAL_MIN );

	EXPECT_REALZERO( math_mod( REAL_ZERO, REAL_ONE ) );
	EXPECT_REALZERO( math_mod( REAL_ONE, REAL_ONE ) );
	EXPECT_REALZERO( math_mod( REAL_MAX, REAL_ONE ) );
	EXPECT_REALONE( math_mod( REAL_THREE, REAL_TWO ) );
	EXPECT_REALONE( -math_mod( -REAL_THREE, -REAL_TWO ) );

	EXPECT_EQ( math_floor( REAL_ZERO ), 0 );
	EXPECT_EQ( math_floor( REAL_C( 0.999 ) ), 0 );
	EXPECT_EQ( math_floor( REAL_C( -0.1 ) ), -1 );
	EXPECT_EQ( math_floor( REAL_C( 42.5 ) ), 42 );

	EXPECT_EQ( math_ceil( REAL_ZERO ), 0 );
	EXPECT_EQ( math_ceil( REAL_C( 0.999 ) ), 1 );
	EXPECT_EQ( math_ceil( REAL_C( -0.1 ) ), 0 );
	EXPECT_EQ( math_ceil( REAL_C( 42.5 ) ), 43 );
	EXPECT_EQ( math_ceil( REAL_C( 42.45 ) ), 43 );

	EXPECT_EQ( math_floor64( REAL_ZERO ), 0 );
	EXPECT_EQ( math_floor64( REAL_C( 0.999 ) ), 0 );
	EXPECT_EQ( math_floor64( REAL_C( -0.1 ) ), -1 );
	EXPECT_EQ( math_floor64( REAL_C( 42.5 ) ), 42 );

	EXPECT_EQ( math_ceil64( REAL_ZERO ), 0 );
	EXPECT_EQ( math_ceil64( REAL_C( 0.999 ) ), 1 );
	EXPECT_EQ( math_ceil64( REAL_C( -0.1 ) ), 0 );
	EXPECT_EQ( math_ceil64( REAL_C( 42.5 ) ), 43 );
	EXPECT_EQ( math_ceil64( REAL_C( 42.45 ) ), 43 );
	
	EXPECT_EQ( math_round( REAL_ZERO ), 0 );
	EXPECT_EQ( math_round( REAL_C( 0.999 ) ), 1 );
	EXPECT_EQ( math_round( REAL_C( -0.1 ) ), 0 );
	EXPECT_EQ( math_round( REAL_C( 42.5 ) ), 43 );
	EXPECT_EQ( math_round( REAL_C( 42.45 ) ), 42 );
	
	EXPECT_EQ( math_trunc( REAL_ZERO ), 0 );
	EXPECT_EQ( math_trunc( REAL_C( 0.999 ) ), 0 );
	EXPECT_EQ( math_trunc( REAL_C( -0.1 ) ), 0 );
	EXPECT_EQ( math_trunc( REAL_C( 42.5 ) ), 42 );

	EXPECT_EQ( math_align_poweroftwo( 2 ), 2 );
	EXPECT_EQ( math_align_poweroftwo( 3 ), 4 );
	EXPECT_EQ( math_align_poweroftwo( 4 ), 4 );
	EXPECT_EQ( math_align_poweroftwo( 33 ), 64 );
	EXPECT_EQ( math_align_poweroftwo( 134217729 ), 268435456 );

	for( i = 1; i < 31; ++i )
	{
		EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i ) - 1 ) ) );
		EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i )     ) ) );
		EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i ) + 1 ) ) );

		EXPECT_FALSE( math_is_poweroftwo( ( 2 << i ) - 1 ) );
		EXPECT_TRUE( math_is_poweroftwo( ( 2 << i )     ) );
		EXPECT_FALSE( math_is_poweroftwo( ( 2 << i ) + 1 ) );
	}

	EXPECT_EQ( math_align_up( 1, 1 ), 1 );
	EXPECT_EQ( math_align_up( 1, 2 ), 2 );
	EXPECT_EQ( math_align_up( 17, 2 ), 18 );
	EXPECT_EQ( math_align_up( 43, 42 ), 84 );

	EXPECT_REALZERO( math_smoothstep( REAL_ZERO ) );
	EXPECT_REALONE( math_smoothstep( REAL_ONE ) );
	EXPECT_REALEQ( math_smoothstep( REAL_HALF ), REAL_HALF );
	
	EXPECT_REALZERO( math_smootherstep( REAL_ZERO ) );
	EXPECT_REALONE( math_smootherstep( REAL_ONE ) );
	EXPECT_REALEQ( math_smootherstep( REAL_HALF ), REAL_HALF );

	EXPECT_REALZERO( math_lerp( REAL_ZERO, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALZERO( math_lerp( REAL_ONE, REAL_ONE, REAL_ZERO ) );
	EXPECT_REALONE( math_lerp( REAL_ONE, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALONE( math_lerp( REAL_ZERO, REAL_ONE, REAL_ZERO ) );
	EXPECT_REALEQ( math_lerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF );
	EXPECT_REALEQ( math_lerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF );

	EXPECT_REALZERO( math_unlerp( REAL_ZERO, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALZERO( math_unlerp( REAL_ONE, REAL_ONE, REAL_ZERO ) );
	EXPECT_REALONE( math_unlerp( REAL_ONE, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALONE( math_unlerp( REAL_ZERO, REAL_ONE, REAL_ZERO ) );
	EXPECT_REALEQ( math_unlerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF );
	EXPECT_REALEQ( math_unlerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF );

	EXPECT_REALONE( math_linear_remap( REAL_C( 150.0 ), REAL_C( 100.0 ), REAL_C( 200.0 ), REAL_ZERO, REAL_TWO ) );

	EXPECT_REALZERO( math_clamp( -REAL_ONE, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALZERO( math_clamp( REAL_ZERO, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALEQ( math_clamp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF );
	EXPECT_REALONE( math_clamp( REAL_ONE, REAL_ZERO, REAL_ONE ) );
	EXPECT_REALONE( math_clamp( REAL_TWO, REAL_ZERO, REAL_ONE ) );

	return 0;
}