int main (int argc, char *argv[]) { double result; sqlite3_int64 result64; if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ result = math_round (3.4); if (abs (result - 3) > double_eps) { fprintf (stderr, "bad math_round() result for 3.4: %f\n", result); return -1; } result = math_round (3.6); if (abs (result - 4) > double_eps) { fprintf (stderr, "bad math_round() result for 3.6: %f\n", result); return -2; } result = math_round (-3.4); if (abs (result + 3) > double_eps) { fprintf (stderr, "bad math_round() result for -3.4: %f\n", result); return -3; } result = math_round (-3.6); if (abs (result + 4) > double_eps) { fprintf (stderr, "bad math_round() result for -3.6: %f\n", result); return -4; } result64 = math_llabs ((sqlite3_int64) 26); if (result64 != 26) { fprintf (stderr, "bad math_llabs() result for 26"); return -5; } result64 = math_llabs ((sqlite3_int64) - 26); if (result64 != 26) { fprintf (stderr, "bad math_llabs() result for -26"); return -6; } return 0; }
objectmap_t* objectmap_allocate( unsigned int size ) { uint64_t bits; unsigned int ip; uintptr_t next_indexshift; objectmap_t* map; void** slot; FOUNDATION_ASSERT_MSG( size > 2, "Invalid objectmap size" ); if( size <= 2 ) size = 2; bits = math_round( math_log2( (real)size ) ); //Number of bits needed FOUNDATION_ASSERT_MSGFORMAT( bits < 50, "Invalid objectmap size %d", size ); //Top two bits unused for Lua compatibility map = memory_allocate_zero( sizeof( objectmap_t ) + ( sizeof( void* ) * size ), 16, MEMORY_PERSISTENT ); map->size_bits = bits; map->id_max = ((1ULL<<(62ULL-bits))-1); map->size = size; map->mask_index = ((1ULL<<bits)-1ULL); map->mask_id = ( 0x3FFFFFFFFFFFFFFFULL & ~map->mask_index ); atomic_store64( &map->free, 0 ); atomic_store64( &map->id, 1 ); slot = map->map; for( ip = 0, next_indexshift = 3; ip < ( size - 1 ); ++ip, next_indexshift += 2, ++slot ) *slot = (void*)next_indexshift; *slot = (void*)((uintptr_t)-1); return map; }
void fzyNeuCtrl(int en_l, int en_r, float set_l, float set_r){ /* encoder value ----> rotation (rad/s) */ float vl, vr; vl = (float)en_l * 0.2586f; vr = (float)en_r * 0.2586f; set_l = set_l * 0.2586f; set_r = set_r * 0.2586f; /* update x(m) with x(m+1) computed last time */ xl = xl_1; xr = xr_1; /* 1.reference model */ xl_1 = referModel(xl, set_l); xr_1 = referModel(xr, set_r); /* 2.reference state */ referState(xl_1, xr_1, xl, xr); /* 3.error */ float e1, e2; e1 = (xl - vl); e2 = (xr - vr); /* 4.fuzzy neural controller */ fzyNeu_update(e1, e2); /* 5.switching control */ switching(e1, e2); //ys[0] = 0; //ys[1] = 0; /* 6.control signal */ yt[0] = yfnc[0] + ys[0] + yr[0]; yt[1] = yfnc[1] + ys[1] + yr[1]; /* control signal mapping */ if(yt[0] > 0) fnn_l = math_round(k_lf * yt[0] + lf); else if(yt[0] == 0) fnn_l = 0.0f; else if(yt[0] < 0) fnn_l = math_round(k_lb * yt[0] + lb); if(yt[1] > 0) fnn_r = math_round(k_rf * yt[1] + rf); else if(yt[1] == 0) fnn_r = 0.0f; else if(yt[1] < 0) fnn_r = math_round(k_rb * yt[1] + rb); recFzyNeuData((int)(e1*10000), (int)(e2*10000), (int)(yfnc[0]*10000), (int)(yfnc[1]*10000), (int)(ys[0]*10000), (int)(ys[1]*10000), (int)(yr[0]*10000), (int)(yr[1]*10000)); }
/* reference: shaderX6 - cascade shadow stabilization (micheal villant) */ struct mat4f* csm_round_mat(struct mat4f* r, const struct mat4f* m, float shadow_size) { struct vec3f origin; vec3_transformsrt_m4(&origin, &g_vec3_zero, m); float texcoord_x = origin.x*shadow_size*0.5f; float texcoord_y = origin.y*shadow_size*0.5f; float texcoord_x_round = math_round(texcoord_x); float texcoord_y_round = math_round(texcoord_y); float dx = texcoord_x_round - texcoord_x; float dy = texcoord_y_round - texcoord_y; dx /= shadow_size*0.5f; dy /= shadow_size*0.5f; struct mat4f round_mat; mat4_set_ident(&round_mat); round_mat.m41 = dx; round_mat.m42 = dy; round_mat.m43 = 0.0f; return mat4_mul(r, m, &round_mat); }
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; }