Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 40342a9e36
Fetching contributors…

Cannot retrieve contributors at this time

file 586 lines (483 sloc) 11.809 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
/*
===========================================================================

Wolfenstein: Enemy Territory GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.

This file is part of the Wolfenstein: Enemy Territory GPL Source Code (“Wolf ET Source Code”).

Wolf ET Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Wolf ET Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Wolf ET Source Code. If not, see <http://www.gnu.org/licenses/>.

In addition, the Wolf: ET Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Wolf ET Source Code. If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/


#include "../game/q_shared.h"
#include "../qcommon/qcommon.h"
#include "win_local.h"
#include <lmerr.h>
#include <lmcons.h>
#include <lmwksta.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <direct.h>
#include <io.h>
#include <conio.h>
#include <math.h>

/*
================
Sys_Milliseconds
================
*/
int sys_timeBase;
int Sys_Milliseconds( void ) {
int sys_curtime;
static qboolean initialized = qfalse;

if ( !initialized ) {
sys_timeBase = timeGetTime();
initialized = qtrue;
}
sys_curtime = timeGetTime() - sys_timeBase;

return sys_curtime;
}

/*
================
Sys_SnapVector
================
*/
long fastftol( float f ) {
#ifndef __GNUC__
static int tmp;

__asm fld f
__asm fistp tmp
// rain - WTF - why do they set the return this way?
__asm mov eax, tmp
#else
// rain - gcc-style inline asm
// zinx - meh, gcc's lrint is sane, so use that. fixed inline asm too, though.
/*
asm(
"fld %1\n\t"
"fistp %0\n"
: "=m" (tmp) // outputs
: "f" (f) // inputs
);
return tmp;
*/
return lrint( f );
#endif
}

void Sys_SnapVector( float *v ) {
#ifndef __GNUC__
int i;
float f;

f = *v;
__asm fld f;
__asm fistp i;
*v = i;
v++;
f = *v;
__asm fld f;
__asm fistp i;
*v = i;
v++;
f = *v;
__asm fld f;
__asm fistp i;
*v = i;
/*
*v = fastftol(*v);
v++;
*v = fastftol(*v);
v++;
*v = fastftol(*v);
*/
#else
// rain - gcc has different inline asm, but I'm not going to emulate
// that here for now...
*v = (float)fastftol( *v );
v++;
*v = (float)fastftol( *v );
v++;
*v = (float)fastftol( *v );
#endif
}

/*
**
** Disable all optimizations temporarily so this code works correctly!
**
*/
#ifdef MSVC // rain - MSVC pragma
#pragma optimize( "", off )
#endif

/*
** --------------------------------------------------------------------------------
**
** PROCESSOR STUFF
**
** --------------------------------------------------------------------------------
*/
static void CPUID( int func, unsigned regs[4] ) {
#ifndef __GNUC__
unsigned regEAX, regEBX, regECX, regEDX;

__asm mov eax, func
__asm cpuid
__asm mov regEAX, eax
__asm mov regEBX, ebx
__asm mov regECX, ecx
__asm mov regEDX, edx

regs[0] = regEAX;
regs[1] = regEBX;
regs[2] = regECX;
regs[3] = regEDX;
#else
// rain - gcc style inline asm
asm (
"cpuid\n"
: "=a" ( regs[0] ), "=b" ( regs[1] ), "=c" ( regs[2] ), "=d" ( regs[3] ) // outputs
: "a" ( func ) // inputs
);
#endif

}

static int IsPentium( void ) {
#ifndef __GNUC__
__asm
{
pushfd // save eflags
pop eax
test eax, 0x00200000 // check ID bit
jz set21 // bit 21 is not set, so jump to set_21
and eax, 0xffdfffff // clear bit 21
push eax // save new value in register
popfd // store new value in flags
pushfd
pop eax
test eax, 0x00200000 // check ID bit
jz good
jmp err // cpuid not supported
set21:
or eax, 0x00200000 // set ID bit
push eax // store new value
popfd // store new value in EFLAGS
pushfd
pop eax
test eax, 0x00200000 // if bit 21 is on
jnz good
jmp err
}

err:
return qfalse;
good:
return qtrue;
#else
// rain - gcc-style inline asm
// rain - FIXME: I'm too tired to do this one right now
return qtrue;
#endif
}

static int Is3DNOW( void ) {
unsigned regs[4];
char pstring[16];
char processorString[13];

// get name of processor
CPUID( 0, ( unsigned int * ) pstring );
processorString[0] = pstring[4];
processorString[1] = pstring[5];
processorString[2] = pstring[6];
processorString[3] = pstring[7];
processorString[4] = pstring[12];
processorString[5] = pstring[13];
processorString[6] = pstring[14];
processorString[7] = pstring[15];
processorString[8] = pstring[8];
processorString[9] = pstring[9];
processorString[10] = pstring[10];
processorString[11] = pstring[11];
processorString[12] = 0;

// REMOVED because you can have 3DNow! on non-AMD systems
// if ( strcmp( processorString, "AuthenticAMD" ) )
// return qfalse;

// check AMD-specific functions
CPUID( 0x80000000, regs );
if ( regs[0] < 0x80000000 ) {
return qfalse;
}

// bit 31 of EDX denotes 3DNOW! support
CPUID( 0x80000001, regs );
if ( regs[3] & ( 1 << 31 ) ) {
return qtrue;
}

return qfalse;
}

static int IsKNI( void ) {
unsigned regs[4];

// get CPU feature bits
CPUID( 1, regs );

// bit 25 of EDX denotes KNI existence
if ( regs[3] & ( 1 << 25 ) ) {
return qtrue;
}

return qfalse;
}

static int IsMMX( void ) {
unsigned regs[4];

// get CPU feature bits
CPUID( 1, regs );

// bit 23 of EDX denotes MMX existence
if ( regs[3] & ( 1 << 23 ) ) {
return qtrue;
}
return qfalse;
}

static int IsP3() {
unsigned regs[4];

// get CPU feature bits
CPUID( 1, regs );
if ( regs[0] < 6 ) {
return qfalse;
}

if ( !( regs[3] & 0x1 ) ) {
return qfalse; // fp
}

if ( !( regs[3] & 0x8000 ) ) { // cmov
return qfalse;
}

if ( !( regs[3] & 0x800000 ) ) { // mmx
return qfalse;
}

if ( !( regs[3] & 0x2000000 ) ) { // simd
return qfalse;
}

return qtrue;
}

static int IsAthlon() {
unsigned regs[4];
char pstring[16];
char processorString[13];

// get name of processor
CPUID( 0, ( unsigned int * ) pstring );
processorString[0] = pstring[4];
processorString[1] = pstring[5];
processorString[2] = pstring[6];
processorString[3] = pstring[7];
processorString[4] = pstring[12];
processorString[5] = pstring[13];
processorString[6] = pstring[14];
processorString[7] = pstring[15];
processorString[8] = pstring[8];
processorString[9] = pstring[9];
processorString[10] = pstring[10];
processorString[11] = pstring[11];
processorString[12] = 0;

if ( strcmp( processorString, "AuthenticAMD" ) ) {
return qfalse;
}

CPUID( 0x80000000, regs );

if ( regs[0] < 0x80000001 ) {
return qfalse;
}

// get CPU feature bits
CPUID( 1, regs );
if ( regs[0] < 6 ) {
return qfalse;
}

CPUID( 0x80000001, regs );

if ( !( regs[3] & 0x1 ) ) {
return qfalse; // fp
}

if ( !( regs[3] & 0x8000 ) ) { // cmov
return qfalse;
}

if ( !( regs[3] & 0x800000 ) ) { // mmx
return qfalse;
}

if ( !( regs[3] & 0x400000 ) ) { // k7 mmx
return qfalse;
}

if ( !( regs[3] & 0x80000000 ) ) { // 3dnow
return qfalse;
}

if ( !( regs[3] & 0x40000000 ) ) { // advanced 3dnow
return qfalse;
}

return qtrue;
}

int Sys_GetProcessorId( void ) {
#if defined _M_ALPHA
return CPUID_AXP;
#elif !defined _M_IX86
return CPUID_GENERIC;
#else

// verify we're at least a Pentium or 486 w/ CPUID support
if ( !IsPentium() ) {
return CPUID_INTEL_UNSUPPORTED;
}

// check for MMX
if ( !IsMMX() ) {
// Pentium or PPro
return CPUID_INTEL_PENTIUM;
}

// see if we're an AMD 3DNOW! processor
if ( Is3DNOW() ) {
return CPUID_AMD_3DNOW;
}

// see if we're an Intel Katmai
if ( IsKNI() ) {
return CPUID_INTEL_KATMAI;
}

// by default we're functionally a vanilla Pentium/MMX or P2/MMX
return CPUID_INTEL_MMX;

#endif
}

int Sys_GetHighQualityCPU() {
return ( !IsP3() && !IsAthlon() ) ? 0 : 1;
}

//bani - defined but not used
#if 0
static void GetClockTicks( double *t ) {
unsigned long lo, hi;

#ifndef __GNUC__
_asm
{
rdtsc
mov lo, eax
mov hi, edx
}
#else
// rain - gcc-style inline asm
asm (
"rdtsc\n"
: "=a" ( lo ), "=d" ( hi ) // outputs
);
#endif

// *t = (double) lo + (double) 0xFFFFFFFF * hi ;
// zinx - 0x100000000
*t = (double) lo + (double) 4294967296.0 * hi ;
}
#endif

float Sys_RealGetCPUSpeed( void ) {
LARGE_INTEGER c0, c1, freq;
unsigned int stamp0, stamp1, cycles, ticks, tries;
unsigned int freq1, freq2, freq3, total;
unsigned int priorityClass;
int threadPriority;

freq1 = freq2 = freq3 = tries = 0;

if ( !QueryPerformanceFrequency( &freq ) ) {
return .0f;
}

priorityClass = GetPriorityClass( GetCurrentProcess() );
threadPriority = GetThreadPriority( GetCurrentThread() );

SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );

do {
tries++;

freq3 = freq2;
freq2 = freq1;

QueryPerformanceCounter( &c0 );

c1.LowPart = c0.LowPart;
c1.HighPart = c0.HighPart;

while ( (int)c1.LowPart - (int)c0.LowPart < 50 ) {
QueryPerformanceCounter( &c1 );
}

#ifndef __GNUC__
_asm
{
rdtsc
mov stamp0, eax
}
#else
// rain - gcc-style inline asm
asm (
"rdtsc\n"
: "=a" ( stamp0 ) // outputs
:
: "edx"
);
#endif

c0.LowPart = c1.LowPart;
c0.HighPart = c1.HighPart;

while ( (int)c1.LowPart - (int)c0.LowPart < 1000 ) {
QueryPerformanceCounter( &c1 );
}

#ifndef __GNUC__
_asm
{
rdtsc
mov stamp1, eax
}
#else
// rain - gcc-style inline asm
asm (
"rdtsc\n"
: "=a" ( stamp1 ) // outputs
:
: "edx"
);
#endif

cycles = stamp1 - stamp0;
ticks = (int)c1.LowPart - (int)c0.LowPart;

ticks *= 100000;
ticks /= ( freq.LowPart / 10 );

if ( ticks % freq.LowPart > freq.LowPart * .5f ) {
ticks++;
}

freq1 = cycles / ticks;

if ( cycles % ticks > ticks * .5f ) {
freq1++;
}

total = freq1 + freq2 + freq3;

} while ( ( tries < 3 || tries < 20 ) &&
( ( abs( 3 * freq1 - total ) > 3 ) || ( abs( 3 * freq2 - total ) > 3 ) || ( abs( 3 * freq3 - total ) > 3 ) ) );

if ( total / 3 != ( total + 1 ) / 3 ) {
total++;
}

SetPriorityClass( GetCurrentProcess(), priorityClass );
SetThreadPriority( GetCurrentThread(), threadPriority );

return( (float)total / 3.f );
}

float Sys_GetCPUSpeed( void ) {
float cpuSpeed;

#ifndef __GNUC__
__try
#else
if ( 1 )
#endif
{
cpuSpeed = Sys_RealGetCPUSpeed();
}
#ifndef __GNUC__
__except( EXCEPTION_EXECUTE_HANDLER )
#else
if ( 0 )
#endif
{
cpuSpeed = 100.f;
}

return cpuSpeed;
}

/*
**
** Re-enable optimizations back to what they were
**
*/
#ifdef MSVC // rain - MSVC pragma
#pragma optimize( "", on )
#endif

//============================================

char *Sys_GetCurrentUser( void ) {
static char s_userName[1024];
unsigned long size = sizeof( s_userName );


if ( !GetUserName( s_userName, &size ) ) {
strcpy( s_userName, "player" );
}

if ( !s_userName[0] ) {
strcpy( s_userName, "player" );
}

return s_userName;
}
Something went wrong with that request. Please try again.