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
|
|
|
|
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
|
|
|
{
|
2011-05-04 14:10:17 +02:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // 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);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE((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
|
|
|
{
|
2011-05-04 14:10:17 +02:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // 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);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE((unsigned long)l <= 65535u);
|
2004-06-01 19:34:12 +02:00
|
|
|
return (u16)l;
|
|
|
|
}
|