2017-07-10 07:26:24 -07:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08: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:
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* 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 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* various utility functions.
|
2006-04-11 16:59:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-05-07 18:11:51 -07:00
|
|
|
#include "precompiled.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/lib.h"
|
2005-06-27 21:12:50 -07:00
|
|
|
|
2004-06-09 07:11:35 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2006-04-18 22:30:02 -07:00
|
|
|
#include <ctype.h>
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2005-12-09 23:04:31 -08:00
|
|
|
#include "lib/app_hooks.h"
|
2006-06-01 19:10:27 -07:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2005-02-02 09:01:33 -08:00
|
|
|
|
2004-06-01 10:34:12 -07:00
|
|
|
|
2006-05-30 21:01:59 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// type conversion
|
|
|
|
|
|
2005-08-09 09:23:19 -07: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-08 21:59:33 -08:00
|
|
|
return (u32)(x & 0xFFFFFFFF);
|
2005-08-09 09:23:19 -07:00
|
|
|
}
|
|
|
|
|
|
2006-02-08 21:59:33 -08:00
|
|
|
u16 u32_hi(u32 x)
|
|
|
|
|
{
|
|
|
|
|
return (u16)(x >> 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u16 u32_lo(u32 x)
|
|
|
|
|
{
|
|
|
|
|
return (u16)(x & 0xFFFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-08-12 10:06:53 -07:00
|
|
|
u64 u64_from_u32(u32 hi, u32 lo)
|
|
|
|
|
{
|
|
|
|
|
u64 x = (u64)hi;
|
|
|
|
|
x <<= 32;
|
|
|
|
|
x |= lo;
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 23:59:20 -08:00
|
|
|
u32 u32_from_u16(u16 hi, u16 lo)
|
|
|
|
|
{
|
|
|
|
|
u32 x = (u32)hi;
|
|
|
|
|
x <<= 16;
|
|
|
|
|
x |= lo;
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-06-01 10:34:12 -07:00
|
|
|
// input in [0, 1); convert to u8 range
|
2007-05-09 14:01:11 -07:00
|
|
|
u8 u8_from_double(double in)
|
2004-06-01 10:34:12 -07:00
|
|
|
{
|
2005-01-23 09:54:20 -08:00
|
|
|
if(!(0.0 <= in && in < 1.0))
|
2004-06-01 10:34:12 -07:00
|
|
|
{
|
2011-05-04 05:10:17 -07:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
|
2004-06-01 10:34:12 -07:00
|
|
|
return 255;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-02 09:59:12 -07:00
|
|
|
int l = (int)(in * 255.0);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE((unsigned)l <= 255u);
|
2004-06-01 10:34:12 -07:00
|
|
|
return (u8)l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// input in [0, 1); convert to u16 range
|
2007-05-09 14:01:11 -07:00
|
|
|
u16 u16_from_double(double in)
|
2004-06-01 10:34:12 -07:00
|
|
|
{
|
2005-01-23 09:54:20 -08:00
|
|
|
if(!(0.0 <= in && in < 1.0))
|
2004-06-01 10:34:12 -07:00
|
|
|
{
|
2011-05-04 05:10:17 -07:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
|
2004-06-01 10:34:12 -07:00
|
|
|
return 65535;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-02 09:59:12 -07:00
|
|
|
long l = (long)(in * 65535.0);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE((unsigned long)l <= 65535u);
|
2004-06-01 10:34:12 -07:00
|
|
|
return (u16)l;
|
|
|
|
|
}
|