int rand_shuffle(rand_t *r, void *base, size_t nmemb, size_t size) { u_char *save, *src, *dst, *start = (u_char *)base; u_int i, j; if (nmemb < 2) return (0); if ((u_int)r->tmplen < size) { if (r->tmp == NULL) { if ((save = malloc(size)) == NULL) return (-1); } else if ((save = realloc(r->tmp, size)) == NULL) return (-1); r->tmp = save; r->tmplen = size; } else save = r->tmp; for (i = 0; i < nmemb; i++) { if ((j = rand_uint32(r) % (nmemb - 1)) != i) { src = start + (size * i); dst = start + (size * j); memcpy(save, dst, size); memcpy(dst, src, size); memcpy(src, save, size); } } return (0); }
//----[ compileDropSequence ]------------------------------------------------ void GlobalTreasure::compileDropSequence( TreasureDropSequenceElement* elements, size_t number_of_elements) { unsigned int common_threshold = (UINT_MAX-2) * (double)(percent_equipment_.getValue() / 100.0); unsigned int uncommon_threshold = common_threshold * (double)(percent_uncommon_.getValue() / 100.0); unsigned int rare_threshold = uncommon_threshold * (double)(percent_uncommons_rare_.getValue() / 100.0); rare_threshold = uncommon_threshold - rare_threshold; uncommon_threshold = common_threshold - uncommon_threshold; common_threshold = (UINT_MAX-2) - common_threshold; uncommon_threshold += common_threshold; rare_threshold += uncommon_threshold; assert(common_threshold < uncommon_threshold); assert(uncommon_threshold < rare_threshold); unsigned int gold_threshold = (UINT_MAX-2) * (double)(1.0 - percent_gold_.getValue() / 100.0); unsigned int consumable_threshold = (UINT_MAX-2) * (double)(1.0 - percent_consumables_.getValue() / 100.0); for (int i = 0; i < number_of_elements; ++i) { // use exclusive comparisons (>) instead of inclusive (>=) so that // setting something to a drop % of 0 makes it never drop unsigned int gold = rand_uint32(); unsigned int consumable = rand_uint32(); unsigned int equipment = rand_uint32(); if (equipment > rare_threshold) { elements[i].equipment = TreasureDropSequenceElement::RARE; } else if (equipment > uncommon_threshold) { elements[i].equipment = TreasureDropSequenceElement::UNCOMMON; } else if (equipment > common_threshold) { elements[i].equipment = TreasureDropSequenceElement::COMMON; } else { elements[i].equipment = TreasureDropSequenceElement::NONE; } elements[i].gold = gold > gold_threshold; elements[i].consumable = consumable > consumable_threshold; } }
int ConnClient::prepare_req_conn(const sockaddr* addr, uv_udp_t* handle) { _addr = *addr; _udp = handle; _status = CONV_REQ_CONN; _next_req_conn_tick = 0; _req_conn_times = 10; _req_conn.conv = CONV_REQ_CONN; _req_conn.n = rand_uint32(); return 0; }
unsigned int random_u32(unsigned int seed) { rand_t *r; int rv; unsigned int rand; r = rand_open(); if (r == NULL) return -1; rv = rand_set(r,&seed,4); if (r == NULL) return -1; rand = rand_uint32(r); r = rand_close(r); return rand; }
static struct hostent *lumberjack_choose_address(const char *host) { strlist_t *hostlist; split(&hostlist, host, ","); insist(hostlist->nitems > 0, "host string must not be empty"); struct backoff sleeper; backoff_init(&sleeper, &MIN_SLEEP, &MAX_SLEEP); struct hostent *hostinfo = NULL; while (hostinfo == NULL) { int item = rand_uint32() % hostlist->nitems; char *chosen = hostlist->items[item]; flog_if_slow(stdout, 0.200, { hostinfo = gethostbyname(chosen); }, "gethostbyname('%s')", chosen);
//----[ AvatarMeleeDamage ]-------------------------------------------------- int AvatarMeleeDamage(Avatar::AvatarLevel avatar_level, int min_damage, int max_damage, int strength, Avatar::AvatarClass avatar_class) { float expected_str = 15.0 + avatar_level / 3.5; float base_multiplier = 0.4 / (expected_str - 10); float multiplier = 0.25 + ((BoostedAvatarStat(strength)-10) * base_multiplier); // I removed this 9/30/2009 because players were complaining that strength // didn't do anything useful. //if (multiplier > 1.22) multiplier = 1.22; if (Avatar::AvatarClassIsMage(avatar_class)) multiplier /= 2.0; int damage_range = (max_damage - min_damage); // ensure it's at least 1 damage_range = damage_range < 1 ? 1 : damage_range; return Time::Milliseconds(multiplier * (min_damage + (rand_uint32()%damage_range))); }
struct personality * personality_random(void) { extern rand_t *honeyd_rand; struct personality *pers; int i; if (!npersons) return (NULL); i = rand_uint32(honeyd_rand) % npersons; pers = SPLAY_MIN(perstree, &personalities); while (i--) { pers = SPLAY_NEXT(perstree, &personalities, pers); } return (pers); }
void tlb_commands_till_exc_init(tst_t *tst, shared_mem_t *shared_ptr) { for(int i=1; i<32; i++) shared_ptr->initial.reg[i-1] = rand_uint32(); shared_ptr->initial.pc = 0xA0001000; for(int i=0; i<64; i++) shared_ptr->initial.tlb[i].vpn = rand() & 0xFFFFF; shared_ptr->initial.index_p = rand() & 0x1; shared_ptr->initial.index_index = rand() & 0x3F; shared_ptr->initial.random = rand() & 0x3F; shared_ptr->initial.entrylo_pfn = rand() & 0xFFFFF; shared_ptr->initial.entrylo_n = rand() & 0x1; shared_ptr->initial.entrylo_d = rand() & 0x1; shared_ptr->initial.entrylo_v = rand() & 0x1; shared_ptr->initial.entrylo_g = rand() & 0x1; shared_ptr->initial.context_ptebase = rand() & 0x7FF; shared_ptr->initial.context_badvpn = rand() & 0x7FFFF; shared_ptr->initial.bad_vaddr = rand_uint32(); shared_ptr->initial.entryhi_vpn = rand() & 0xFFFFF; shared_ptr->initial.entryhi_asid = rand() & 0x3F; shared_ptr->initial.sr_cp_usable = rand() & 0xF; shared_ptr->initial.sr_rev_endian = rand() & 0x1; shared_ptr->initial.sr_bootstrap_vec = rand() & 0x1; shared_ptr->initial.sr_tlb_shutdown = rand() & 0x1; shared_ptr->initial.sr_parity_err = rand() & 0x1; shared_ptr->initial.sr_cache_miss = rand() & 0x1; shared_ptr->initial.sr_parity_zero = rand() & 0x1; shared_ptr->initial.sr_switch_cache = rand() & 0x1; shared_ptr->initial.sr_isolate_cache = rand() & 0x1; shared_ptr->initial.sr_irq_mask = rand() & 0xFF; shared_ptr->initial.sr_ku_ie = rand() & 0x3F; shared_ptr->initial.cause_branch_delay = rand() & 0x1; shared_ptr->initial.cause_cp_error = rand() & 0x3; shared_ptr->initial.cause_irq_pending = 0; shared_ptr->initial.cause_exc_code = rand() & 0x1F; shared_ptr->initial.epc = rand_uint32(); // shared_ptr->irq2_at_event = 0xFFFFFFFF; shared_ptr->irq3_at_event = 0xFFFFFFFF; // uint32 *ptr = &shared_ptr->mem.ints[(shared_ptr->initial.pc & 0x1FFFFFFF) / 4]; for(int i=0; i<100; i++) { uint32 type = rand() % 5; if(type == 0) { //RFE (*ptr) = rand() & 0x01FFFFC0; (*ptr) |= (0b0100 << 28) | (0b00 << 26) | (0b1 << 25) | (0b010000 << 0); } else if(type == 1) { //TLBWR (*ptr) = rand() & 0x01FFFFC0; (*ptr) |= (0b0100 << 28) | (0b00 << 26) | (0b1 << 25) | (0b000110 << 0); ptr++; (*ptr) = rand() & 0x03FFFFC0; (*ptr) |= 0b001100; //SYSCALL } else if(type == 2) { //TLBWI (*ptr) = rand() & 0x01FFFFC0; (*ptr) |= (0b0100 << 28) | (0b00 << 26) | (0b1 << 25) | (0b000010 << 0); ptr++; (*ptr) = rand() & 0x03FFFFC0; (*ptr) |= 0b001100; //SYSCALL } else if(type == 3) { //TLBR (*ptr) = rand() & 0x01FFFFC0; (*ptr) |= (0b0100 << 28) | (0b00 << 26) | (0b1 << 25) | (0b000001 << 0); } else if(type == 4) { //TLBP (*ptr) = rand() & 0x01FFFFC0; (*ptr) |= (0b0100 << 28) | (0b00 << 26) | (0b1 << 25) | (0b001000 << 0); } ptr++; } for(int i=0; i<5; i++) { (*ptr) = rand() & 0x03FFFFC0; (*ptr) |= 0b001100; //SYSCALL ptr++; } }
//----[ MonsterGold ]-------------------------------------------------------- unsigned int MonsterGold(Avatar::AvatarLevel intended_level) { if (intended_level > Avatar::HIGHEST_AVATAR_LEVEL) { intended_level = Avatar::HIGHEST_AVATAR_LEVEL; } return 2 * intended_level + 10 + rand_uint32()%(1+intended_level); }
void interrupt_till_exc_init(tst_t *tst, shared_mem_t *shared_ptr) { for(int i=1; i<32; i++) shared_ptr->initial.reg[i-1] = rand_uint32(); shared_ptr->initial.pc = 0xA0001000; //tlb left zero shared_ptr->initial.index_p = rand() & 0x1; shared_ptr->initial.index_index = rand() & 0x3F; shared_ptr->initial.random = rand() & 0x3F; shared_ptr->initial.entrylo_pfn = rand() & 0xFFFFF; shared_ptr->initial.entrylo_n = rand() & 0x1; shared_ptr->initial.entrylo_d = rand() & 0x1; shared_ptr->initial.entrylo_v = rand() & 0x1; shared_ptr->initial.entrylo_g = rand() & 0x1; shared_ptr->initial.context_ptebase = rand() & 0x7FF; shared_ptr->initial.context_badvpn = rand() & 0x7FFFF; shared_ptr->initial.bad_vaddr = rand_uint32(); shared_ptr->initial.entryhi_vpn = rand() & 0xFFFFF; shared_ptr->initial.entryhi_asid = rand() & 0x3F; shared_ptr->initial.sr_cp_usable = rand() & 0xF; shared_ptr->initial.sr_rev_endian = rand() & 0x1; shared_ptr->initial.sr_bootstrap_vec = rand() & 0x1; shared_ptr->initial.sr_tlb_shutdown = rand() & 0x1; shared_ptr->initial.sr_parity_err = rand() & 0x1; shared_ptr->initial.sr_cache_miss = rand() & 0x1; shared_ptr->initial.sr_parity_zero = rand() & 0x1; shared_ptr->initial.sr_switch_cache = rand() & 0x1; shared_ptr->initial.sr_isolate_cache = rand() & 0x1; shared_ptr->initial.sr_irq_mask = rand() & 0xFF; shared_ptr->initial.sr_ku_ie = rand() & 0x3F; shared_ptr->initial.cause_branch_delay = rand() & 0x1; shared_ptr->initial.cause_cp_error = rand() & 0x3; shared_ptr->initial.cause_irq_pending = 0; shared_ptr->initial.cause_exc_code = rand() & 0x1F; shared_ptr->initial.epc = rand_uint32(); // uint32 *ptr = &shared_ptr->mem.ints[(shared_ptr->initial.pc & 0x1FFFFFFF) / 4]; (*ptr) = 0; ptr++; (*ptr) = 0; ptr++; (*ptr) = 0; ptr++; (*ptr) = rand() & 0x03FFFFC0; (*ptr) |= 0b001100; //SYSCALL ptr++; // bool irq3 = (rand() % 2) == 0; shared_ptr->irq2_at_event = (irq3)? 0xFFFFFFFF : 1; shared_ptr->irq3_at_event = (irq3)? 1 : 0xFFFFFFFF; }
//----[ WinMain ]------------------------------------------------------------ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { sqlite3* db; sqlite3_open(":memory:", &db); char* errmsg; // create the data structure char sql[2048]; sqlite3_snprintf(2048, sql, "CREATE VIRTUAL TABLE entity_tree USING rtree(id, min_x, max_x, min_z, max_z)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); return 0; } sqlite3_snprintf(2048, sql, "CREATE TABLE entities (id INTEGER PRIMARY KEY, x REAL, z REAL, entity_ptr BLOB)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); return 0; } sqlite3_create_function(db, "collision", 2, SQLITE_ANY, NULL, // user data ptr collisionFunctionForSqlite, NULL, NULL); // not an aggregating function static const size_t ENTITIES = 1024; static const size_t SIZE = 4096; enum Statement { STATEMENT_UPDATE_BOUNDARIES, STATEMENT_INSERT_ENTITY, STATEMENT_UPDATE_ENTITY, STATEMENT_QUERY_OVERLAP, STATEMENT_QUERY_COLLISION, NUMBER_OF_STATEMENTS, }; struct Entity { int id; Number x, z; Number collision_rad_sq; Number visible_rad_sq; Number radius; }; const char* statements_text[NUMBER_OF_STATEMENTS] = { "REPLACE INTO entity_tree VALUES (?,?,?,?,?)", "INSERT INTO entities VALUES (?,?,?,?)", // id, x, z, entity_ptr (blob) "UPDATE entities SET x=?, z=? WHERE id=?", // x, z, id "SELECT id FROM entity_tree WHERE id>? AND max_x>=? AND min_x<=? AND max_z>=? AND min_z<=?", // min/max x then min/max z // searcher's id, min x, max x, min z, max z "SELECT entities.id, entity_ptr FROM entities,entity_tree WHERE entities.id=entity_tree.id AND entity_tree.id>? AND collision(entities.x, entities.z) AND max_x>=? AND min_x<=? AND max_z>=? AND min_z<=?" }; sqlite3_stmt* statements[NUMBER_OF_STATEMENTS]; for (int i = 0; i <NUMBER_OF_STATEMENTS; ++i) { int rc; if (SQLITE_OK != (rc=sqlite3_prepare_v2(db, statements_text[i], strlen(statements_text[i]), &statements[i], NULL))) { DEBUG_INFO("%i - %s", i, sqlite3_errmsg(db)); sqlite3_close(db); return false; } } Entity* entities = new Entity[ENTITIES]; for (int i = 0; i < ENTITIES; ++i) { entities[i].id = i; entities[i].x = randf() * SIZE; entities[i].z = randf() * SIZE; float f = 0.5 + randf() * 1.0; entities[i].radius = f; entities[i].collision_rad_sq = f*f; f = 1.5f*randf() + f; entities[i].visible_rad_sq = f*f; sqlite3_bind_double(statements[STATEMENT_INSERT_ENTITY], 1, i); sqlite3_bind_double(statements[STATEMENT_INSERT_ENTITY], 2, entities[i].x.toFloat()); sqlite3_bind_double(statements[STATEMENT_INSERT_ENTITY], 3, entities[i].z.toFloat()); sqlite3_bind_blob(statements[STATEMENT_INSERT_ENTITY], 4, &entities[i], 4, SQLITE_TRANSIENT); sqlite3_step(statements[STATEMENT_INSERT_ENTITY]); sqlite3_reset(statements[STATEMENT_INSERT_ENTITY]); sqlite3_bind_int(statements[STATEMENT_UPDATE_BOUNDARIES], 1, entities[i].id); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 2, (entities[i].x - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 3, (entities[i].x + entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 4, (entities[i].z - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 5, (entities[i].z + entities[i].radius).toFloat()); sqlite3_step(statements[STATEMENT_UPDATE_BOUNDARIES]); sqlite3_reset(statements[STATEMENT_UPDATE_BOUNDARIES]); } dcx::dcxWin32Clock clock; unsigned int counter = 0; double starter = clock.getAccurateSystemTime(); double ender = starter + 5.0; double time=ender; unsigned int visible_tally = 0; collider_comparisons = 0; #define TIMETRIAL #ifdef TIMETRIAL while ((time = clock.getAccurateSystemTime()) < ender) { #else while (counter < 1) { #endif for (int i = 0; i < ENTITIES; ++i) { collider_x = entities[i].x = randf()*SIZE; collider_z = entities[i].z = randf()*SIZE; collider_rsq = entities[i].collision_rad_sq; sqlite3_bind_int(statements[STATEMENT_UPDATE_ENTITY], 3, entities[i].id); sqlite3_bind_double(statements[STATEMENT_UPDATE_ENTITY], 1, collider_x.toFloat()); sqlite3_bind_double(statements[STATEMENT_UPDATE_ENTITY], 2, collider_z.toFloat()); sqlite3_step(statements[STATEMENT_UPDATE_ENTITY]); sqlite3_reset(statements[STATEMENT_UPDATE_ENTITY]); float minx = (entities[i].x - entities[i].radius).toFloat(); float maxx = (entities[i].x + entities[i].radius).toFloat(); float minz = (entities[i].x - entities[i].radius).toFloat(); float maxz = (entities[i].x + entities[i].radius).toFloat(); sqlite3_bind_int(statements[STATEMENT_UPDATE_BOUNDARIES], 1, entities[i].id); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 2, minx); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 3, maxx); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 4, minz); sqlite3_bind_double(statements[STATEMENT_UPDATE_BOUNDARIES], 5, maxz); sqlite3_step(statements[STATEMENT_UPDATE_BOUNDARIES]); sqlite3_reset(statements[STATEMENT_UPDATE_BOUNDARIES]); #if 0 sqlite3_bind_int(statements[STATEMENT_QUERY_OVERLAP], 1, entities[i].id); sqlite3_bind_double(statements[STATEMENT_QUERY_OVERLAP], 2, (collider_x - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_OVERLAP], 3, (collider_x + entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_OVERLAP], 4, (collider_z - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_OVERLAP], 5, (collider_z + entities[i].radius).toFloat()); while (SQLITE_ROW == sqlite3_step(statements[STATEMENT_QUERY_OVERLAP])) { Entity* entity = (Entity*)sqlite3_column_blob(statements[STATEMENT_QUERY_OVERLAP], 1); ++visible_tally; #ifndef TIMETRIAL DEBUG_INFO("collision = %i with %i @ %.02f, %.02f", entities[i].id, sqlite3_column_int(statements[STATEMENT_QUERY_OVERLAP], 0), entity->x.toFloat(), entity->z.toFloat()); #endif } sqlite3_reset(statements[STATEMENT_QUERY_OVERLAP]); #endif sqlite3_bind_int(statements[STATEMENT_QUERY_COLLISION], 1, entities[i].id); sqlite3_bind_double(statements[STATEMENT_QUERY_COLLISION], 2, (collider_x - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_COLLISION], 3, (collider_x + entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_COLLISION], 4, (collider_z - entities[i].radius).toFloat()); sqlite3_bind_double(statements[STATEMENT_QUERY_COLLISION], 5, (collider_z + entities[i].radius).toFloat()); while (SQLITE_ROW == sqlite3_step(statements[STATEMENT_QUERY_COLLISION])) { Entity* entity = (Entity*)sqlite3_column_blob(statements[STATEMENT_QUERY_COLLISION], 1); ++visible_tally; #ifndef TIMETRIAL DEBUG_INFO("collision = %i with %i @ %.02f, %.02f", entities[i].id, sqlite3_column_int(statements[STATEMENT_QUERY_COLLISION], 0), entity->x.toFloat(), entity->z.toFloat()); #endif } sqlite3_reset(statements[STATEMENT_QUERY_COLLISION]); #ifndef TIMETRIAL DEBUG_INFO("comparisons = %i", collider_comparisons); #endif } ++counter; } for (int i = 0; i <NUMBER_OF_STATEMENTS; ++i) { sqlite3_finalize(statements[i]); } sqlite3_close(db); delete[] entities; DEBUG_INFO("%f queries/second; %f visible/entity (comparisons=%i)", counter / (time-starter), visible_tally/double(counter), collider_comparisons); #if 0 char sql[2048]; // create the data structure sqlite3_snprintf(2048, sql, "CREATE TABLE IF NOT EXISTS objects ("\ "id INTEGER PRIMARY KEY,"\ "x INTEGER,"\ "z INTEGER"\ ")"); char* errmsg; if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); return 0; } sqlite3_snprintf(2048, sql, "CREATE INDEX IF NOT EXISTS "\ "index_x "\ "ON objects (x)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } sqlite3_snprintf(2048, sql, "CREATE INDEX IF NOT EXISTS "\ "index_z "\ "ON objects (z)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } sqlite3_snprintf(2048, sql, "CREATE TABLE IF NOT EXISTS has_seen ("\ "a INTEGER,"\ "b INTEGER"\ ")"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } sqlite3_snprintf(2048, sql, "CREATE INDEX IF NOT EXISTS "\ "index_a "\ "ON has_seen (a)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } sqlite3_snprintf(2048, sql, "CREATE INDEX IF NOT EXISTS "\ "index_b "\ "ON has_seen (b)"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } sqlite3_snprintf(2048, sql, "CREATE TABLE IF NOT EXISTS results ("\ "id INTEGER PRIMARY KEY,"\ "x INTEGER,"\ "z INTEGER"\ ")"); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); return 0; } sqlite3_exec(db, "PRAGMA case_sensitive_like=ON;", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA synchronous=OFF;", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA count_changes=OFF;", NULL, NULL, NULL); static const int COUNT = 256; static const int SIZE = 64; // insert a bunch of random objects for (int i = 0; i < COUNT; ++i) { sqlite3_snprintf(2048, sql, "INSERT INTO objects (id, x, z) VALUES "\ "(%i, %i, %i)", i, rand_uint32()%SIZE, rand_uint32()%SIZE); if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { DEBUG_INFO("%s", errmsg); sqlite3_close(db); return false; } } enum Statement { STATEMENT_CURRENT_VISIBILITY_QUERY, STATEMENT_NEW_VISIBILITY_QUERY, STATEMENT_INSERT_HAS_SEEN, NUMBER_OF_STATEMENTS, }; const char* statements_text[NUMBER_OF_STATEMENTS] = { "SELECT id, x, z FROM objects WHERE (x BETWEEN ? AND ?) AND (z BETWEEN ? AND ?)", "SELECT id FROM objects WHERE (x BETWEEN ? AND ?) AND (z BETWEEN ? AND ?) EXCEPT SELECT (a) FROM has_seen WHERE (b=?)", "INSERT INTO has_seen (a,b) VALUES (?,?)" }; sqlite3_stmt* statements[NUMBER_OF_STATEMENTS]; for (int i = 0; i <NUMBER_OF_STATEMENTS; ++i) { int rc; if (SQLITE_OK != (rc=sqlite3_prepare_v2(db, statements_text[i], strlen(statements_text[i]), &statements[i], NULL))) { DEBUG_INFO("%i-%s", i, sqlite3_errmsg(db)); sqlite3_close(db); return false; } } dcx::dcxWin32Clock clock; unsigned int counter = 0; double starter = clock.getAccurateSystemTime(); double ender = starter + 1.0; double time=ender; unsigned int visible_tally = 0; #define TIMETRIAL #ifdef TIMETRIAL while ((time = clock.getAccurateSystemTime()) < ender) { #else while (counter < 64) { #endif double x = counter; double z = counter; sqlite3_bind_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 1, x -8); sqlite3_bind_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 2, x +8); sqlite3_bind_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 3, z - 8); sqlite3_bind_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 4, z + 8); sqlite3_bind_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 5, COUNT); //sqlite3_bind_int(statements[STATEMENT_CURRENT_VISIBILITY_QUERY], 1, x -8); //sqlite3_bind_int(statements[STATEMENT_CURRENT_VISIBILITY_QUERY], 2, x +8); //sqlite3_bind_int(statements[STATEMENT_CURRENT_VISIBILITY_QUERY], 3, z - 8); //sqlite3_bind_int(statements[STATEMENT_CURRENT_VISIBILITY_QUERY], 4, z + 8); #ifndef TIMETRIAL char buffer[512]; sprintf_s(buffer, 512, "\n==== %f, %f ============\n", x, z); OutputDebugString(buffer); #endif while (sqlite3_step(statements[STATEMENT_NEW_VISIBILITY_QUERY]) == SQLITE_ROW) { #ifndef TIMETRIAL char buffer[512]; sprintf_s(buffer, 512, " %lu %8f, %8f\n", sqlite3_column_int(statements[STATEMENT_NEW_VISIBILITY_QUERY], 0), sqlite3_column_double(statements[STATEMENT_NEW_VISIBILITY_QUERY], 1), sqlite3_column_double(statements[STATEMENT_NEW_VISIBILITY_QUERY], 2)); OutputDebugString(buffer); #endif //sqlite3_bind_int(statements[STATEMENT_INSERT_HAS_SEEN], 1, sqlite3_column_int(statements[STATEMENT_NEW_VISIBILITY_QUERY],0)); //sqlite3_bind_int(statements[STATEMENT_INSERT_HAS_SEEN], 2, COUNT); //sqlite3_step(statements[STATEMENT_INSERT_HAS_SEEN]); //sqlite3_reset(statements[STATEMENT_INSERT_HAS_SEEN]); visible_tally++; } sqlite3_reset(statements[STATEMENT_NEW_VISIBILITY_QUERY]); sqlite3_reset(statements[STATEMENT_CURRENT_VISIBILITY_QUERY]); ++counter; } for (int i = 0; i <NUMBER_OF_STATEMENTS; ++i) { sqlite3_finalize(statements[i]); } sqlite3_close(db); DEBUG_INFO("%f queries/second; %f visible/entity (tally=%i)", counter / (time-starter), visible_tally/double(counter), visible_tally); #endif // sqlite3* db; // sqlite3_open(":memory:", &db); // // char sql[2048]; // // // create the data structure // sqlite3_snprintf(2048, // sql, // "CREATE TABLE IF NOT EXISTS objects ("\ // "id INTEGER PRIMARY KEY,"\ // "x REAL,"\ // "z REAL"\ // ")"); // char* errmsg; // if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { // DEBUG_INFO("%s", errmsg); // return 0; // } // sqlite3_snprintf(2048, // sql, // "CREATE INDEX IF NOT EXISTS "\ // "index_x "\ // "ON objects (x)"); // if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { // DEBUG_INFO("%s", errmsg); // sqlite3_close(db); // return false; // } // sqlite3_snprintf(2048, // sql, // "CREATE INDEX IF NOT EXISTS "\ // "index_z "\ // "ON objects (z)"); // if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { // DEBUG_INFO("%s", errmsg); // sqlite3_close(db); // return false; // } // sqlite3_exec(db, "PRAGMA case_sensitive_like=ON;", NULL, NULL, NULL); // sqlite3_exec(db, "PRAGMA synchronous=OFF;", NULL, NULL, NULL); // sqlite3_exec(db, "PRAGMA count_changes=OFF;", NULL, NULL, NULL); // // static const int COUNT = 16000; // static const float SIZE = 2048; // // insert a bunch of random objects // for (int i = 0; i < COUNT; ++i) { // sqlite3_snprintf(2048, // sql, // "INSERT INTO objects (id, x, z) VALUES "\ // "(%i, %f, %f)", // i, // randf()*SIZE, // randf()*SIZE); // if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { // DEBUG_INFO("%s", errmsg); // sqlite3_close(db); // return false; // } // } // // // create a query // sqlite3_snprintf(2048, // sql, // "SELECT id, x, z FROM objects "\ // "WHERE x>=? AND x<=? AND z>=? AND z<=?"); // sqlite3_stmt* stmt = NULL; // if (SQLITE_OK != sqlite3_prepare_v2(db, // sql, // sizeof(sql), // &stmt, // NULL)) { // return false; // } // // dcx::dcxWin32Clock clock; // unsigned int counter = 0; // double starter = clock.getAccurateSystemTime(); // double ender = starter + 1; // double time; // unsigned int visible_tally = 0; // while ((time = clock.getAccurateSystemTime()) < ender) { // //while (counter < 64) { // double x = 32.0f; // double z = randf()*SIZE; // sqlite3_bind_double(stmt, 1, x - 8.0); // sqlite3_bind_double(stmt, 2, x + 8.0); // sqlite3_bind_double(stmt, 3, z - 8.0); // sqlite3_bind_double(stmt, 4, z + 8.0); // //char buffer[512]; // //sprintf_s(buffer, 512, "\n==== %f, %f ============\n", x, z); // //OutputDebugString(buffer); // while (sqlite3_step(stmt) == SQLITE_ROW) { // //char buffer[512]; // //sprintf_s(buffer, 512, " %lu %8f, %8f\n", sqlite3_column_int(stmt, 0), sqlite3_column_double(stmt, 1), sqlite3_column_double(stmt, 2)); // //OutputDebugString(buffer); // visible_tally++; // } // sqlite3_reset(stmt); // ++counter; // } // sqlite3_finalize(stmt); // sqlite3_close(db); // DEBUG_INFO("%f queries/second; %f visible/entity", counter / (time-starter), visible_tally/double(counter)); }
//----[ randfExclusive ]----------------------------------------------------- float randfExclusive() { return float(double(rand_uint32()) / double(0x100000000)); }
//----[ randfSymmetric ]----------------------------------------------------- float randfSymmetric() { return float((double(rand_uint32()) / double(0x7FFFFFFF)) - 1.0); }
//----[ randf64 ]------------------------------------------------------------ double randf64() { return double(rand_uint32()) / double(0xFFFFFFFF); }
//----[ randf32 ]------------------------------------------------------------ float randf32() { return float(double(rand_uint32()) / double(0xFFFFFFFF)); }