LCOV - code coverage report
Current view: top level - source/lib - lib.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 18 32 56.2 %
Date: 2022-03-08 13:03:03 Functions: 6 8 75.0 %

          Line data    Source code
       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             : 
      44           2 : u32 u64_hi(u64 x)
      45             : {
      46           2 :     return (u32)(x >> 32);
      47             : }
      48             : 
      49           2 : u32 u64_lo(u64 x)
      50             : {
      51           2 :     return (u32)(x & 0xFFFFFFFF);
      52             : }
      53             : 
      54           2 : u16 u32_hi(u32 x)
      55             : {
      56           2 :     return (u16)(x >> 16);
      57             : }
      58             : 
      59           2 : u16 u32_lo(u32 x)
      60             : {
      61           2 :     return (u16)(x & 0xFFFF);
      62             : }
      63             : 
      64             : 
      65         121 : u64 u64_from_u32(u32 hi, u32 lo)
      66             : {
      67         121 :     u64 x = (u64)hi;
      68         121 :     x <<= 32;
      69         121 :     x |= lo;
      70         121 :     return x;
      71             : }
      72             : 
      73           1 : u32 u32_from_u16(u16 hi, u16 lo)
      74             : {
      75           1 :     u32 x = (u32)hi;
      76           1 :     x <<= 16;
      77           1 :     x |= lo;
      78           1 :     return x;
      79             : }
      80             : 
      81             : 
      82             : // input in [0, 1); convert to u8 range
      83           0 : u8 u8_from_double(double in)
      84             : {
      85           0 :     if(!(0.0 <= in && in < 1.0))
      86             :     {
      87           0 :         DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
      88           0 :         return 255;
      89             :     }
      90             : 
      91           0 :     int l = (int)(in * 255.0);
      92           0 :     ENSURE((unsigned)l <= 255u);
      93           0 :     return (u8)l;
      94             : }
      95             : 
      96             : // input in [0, 1); convert to u16 range
      97           0 : u16 u16_from_double(double in)
      98             : {
      99           0 :     if(!(0.0 <= in && in < 1.0))
     100             :     {
     101           0 :         DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1)
     102           0 :         return 65535;
     103             :     }
     104             : 
     105           0 :     long l = (long)(in * 65535.0);
     106           0 :     ENSURE((unsigned long)l <= 65535u);
     107           0 :     return (u16)l;
     108             : }

Generated by: LCOV version 1.13