Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lib.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * various utility functions.
25  */
26 
27 #include "precompiled.h"
28 #include "lib/lib.h"
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 
34 #include "lib/app_hooks.h"
35 #include "lib/sysdep/sysdep.h"
36 
37 
38 //-----------------------------------------------------------------------------
39 // type conversion
40 
41 // these avoid a common mistake in using >> (ANSI requires shift count be
42 // less than the bit width of the type).
43 
45 {
46  return (u32)(x >> 32);
47 }
48 
50 {
51  return (u32)(x & 0xFFFFFFFF);
52 }
53 
55 {
56  return (u16)(x >> 16);
57 }
58 
60 {
61  return (u16)(x & 0xFFFF);
62 }
63 
64 
66 {
67  u64 x = (u64)hi;
68  x <<= 32;
69  x |= lo;
70  return x;
71 }
72 
74 {
75  u32 x = (u32)hi;
76  x <<= 16;
77  x |= lo;
78  return x;
79 }
80 
81 
82 // input in [0, 1); convert to u8 range
83 u8 u8_from_double(double in)
84 {
85  if(!(0.0 <= in && in < 1.0))
86  {
87  DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
88  return 255;
89  }
90 
91  int l = (int)(in * 255.0);
92  ENSURE((unsigned)l <= 255u);
93  return (u8)l;
94 }
95 
96 // input in [0, 1); convert to u16 range
97 u16 u16_from_double(double in)
98 {
99  if(!(0.0 <= in && in < 1.0))
100  {
101  DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
102  return 65535;
103  }
104 
105  long l = (long)(in * 65535.0);
106  ENSURE((unsigned long)l <= 65535u);
107  return (u16)l;
108 }
#define u8
Definition: types.h:39
const Status LOGIC
Definition: status.h:409
u8 u8_from_double(double in)
convert double to u8; verifies number is in range.
Definition: lib.cpp:83
u16 u32_lo(u32 x)
return upper 16-bits
Definition: lib.cpp:59
u16 u16_from_double(double in)
convert double to u16; verifies number is in range.
Definition: lib.cpp:97
u32 u64_lo(u64 x)
return upper 32-bits
Definition: lib.cpp:49
u32 u32_from_u16(u16 hi, u16 lo)
assemble u64 from u32
Definition: lib.cpp:73
u16 u32_hi(u32 x)
return lower 32-bits
Definition: lib.cpp:54
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:331
#define u16
Definition: types.h:40
#define u64
Definition: types.h:42
#define u32
Definition: types.h:41
u64 u64_from_u32(u32 hi, u32 lo)
return lower 16-bits
Definition: lib.cpp:65
u32 u64_hi(u64 x)
Definition: lib.cpp:44