2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* various utility functions.
|
2006-04-12 01:59:08 +02:00
|
|
|
*/
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/lib.h"
|
2005-06-28 06:12:50 +02:00
|
|
|
|
2004-06-09 16:11:35 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-04-19 07:30:02 +02:00
|
|
|
#include <ctype.h>
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-12-10 08:04:31 +01:00
|
|
|
#include "lib/app_hooks.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2005-02-02 18:01:33 +01:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
|
|
|
|
u16 addusw(u16 x, u16 y)
|
|
|
|
{
|
|
|
|
u32 t = x;
|
2007-05-09 23:01:11 +02:00
|
|
|
return (u16)std::min(t+y, 0xFFFFu);
|
2004-06-01 19:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u16 subusw(u16 x, u16 y)
|
|
|
|
{
|
|
|
|
long t = x;
|
2007-05-09 23:01:11 +02:00
|
|
|
return (u16)(std::max(t-y, 0l));
|
2004-06-01 19:34:12 +02:00
|
|
|
}
|
|
|
|
|
2005-05-27 06:40:29 +02:00
|
|
|
|
2006-05-31 06:01:59 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// type conversion
|
|
|
|
|
2005-08-09 18:23:19 +02:00
|
|
|
// these avoid a common mistake in using >> (ANSI requires shift count be
|
|
|
|
// less than the bit width of the type).
|
|
|
|
|
|
|
|
u32 u64_hi(u64 x)
|
|
|
|
{
|
|
|
|
return (u32)(x >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 u64_lo(u64 x)
|
|
|
|
{
|
2006-02-09 06:59:33 +01:00
|
|
|
return (u32)(x & 0xFFFFFFFF);
|
2005-08-09 18:23:19 +02:00
|
|
|
}
|
|
|
|
|
2006-02-09 06:59:33 +01:00
|
|
|
u16 u32_hi(u32 x)
|
|
|
|
{
|
|
|
|
return (u16)(x >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 u32_lo(u32 x)
|
|
|
|
{
|
|
|
|
return (u16)(x & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-12 19:06:53 +02:00
|
|
|
u64 u64_from_u32(u32 hi, u32 lo)
|
|
|
|
{
|
|
|
|
u64 x = (u64)hi;
|
|
|
|
x <<= 32;
|
|
|
|
x |= lo;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2006-01-23 08:59:20 +01:00
|
|
|
u32 u32_from_u16(u16 hi, u16 lo)
|
|
|
|
{
|
|
|
|
u32 x = (u32)hi;
|
|
|
|
x <<= 16;
|
|
|
|
x |= lo;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
// input in [0, 1); convert to u8 range
|
2007-05-09 23:01:11 +02:00
|
|
|
u8 u8_from_double(double in)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2005-01-23 18:54:20 +01:00
|
|
|
if(!(0.0 <= in && in < 1.0))
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2007-12-20 21:09:19 +01:00
|
|
|
debug_assert(0); // clampf not in [0,1)
|
2004-06-01 19:34:12 +02:00
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2004-06-02 18:59:12 +02:00
|
|
|
int l = (int)(in * 255.0);
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
debug_assert((unsigned)l <= 255u);
|
2004-06-01 19:34:12 +02:00
|
|
|
return (u8)l;
|
|
|
|
}
|
|
|
|
|
|
|
|
// input in [0, 1); convert to u16 range
|
2007-05-09 23:01:11 +02:00
|
|
|
u16 u16_from_double(double in)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2005-01-23 18:54:20 +01:00
|
|
|
if(!(0.0 <= in && in < 1.0))
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2007-12-20 21:09:19 +01:00
|
|
|
debug_assert(0); // clampf not in [0,1)
|
2004-06-01 19:34:12 +02:00
|
|
|
return 65535;
|
|
|
|
}
|
|
|
|
|
2004-06-02 18:59:12 +02:00
|
|
|
long l = (long)(in * 65535.0);
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert((unsigned long)l <= 65535u);
|
2004-06-01 19:34:12 +02:00
|
|
|
return (u16)l;
|
|
|
|
}
|