LCOV - code coverage report
Current view: top level - source/lib - fnv_hash.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 27 27 100.0 %
Date: 2023-01-19 00:18:29 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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             : #include "precompiled.h"
      24             : 
      25             : #include "lib/fnv_hash.h"
      26             : 
      27             : // FNV1-A hash - good for strings.
      28             : // if len = 0 (default), treat buf as a C-string;
      29             : // otherwise, hash <len> bytes of buf.
      30         811 : u32 fnv_hash(const void* buf, size_t len)
      31             : {
      32         811 :     u32 h = 0x811c9dc5u;
      33             :     // give distinct values for different length 0 buffers.
      34             :     // value taken from FNV; it has no special significance.
      35             : 
      36         811 :     const u8* p = (const u8*)buf;
      37             : 
      38             :     // expected case: string
      39         811 :     if(!len)
      40             :     {
      41          17 :         while(*p)
      42             :         {
      43           6 :             h ^= *p++;
      44           6 :             h *= 0x01000193u;
      45             :         }
      46             :     }
      47             :     else
      48             :     {
      49         806 :         size_t bytes_left = len;
      50       15438 :         while(bytes_left != 0)
      51             :         {
      52        7316 :             h ^= *p++;
      53        7316 :             h *= 0x01000193u;
      54             : 
      55        7316 :             bytes_left--;
      56             :         }
      57             :     }
      58             : 
      59         811 :     return h;
      60             : }
      61             : 
      62             : // FNV1-A hash - good for strings.
      63             : // if len = 0 (default), treat buf as a C-string;
      64             : // otherwise, hash <len> bytes of buf.
      65           3 : u64 fnv_hash64(const void* buf, size_t len)
      66             : {
      67           3 :     u64 h = 0xCBF29CE484222325ull;
      68             :     // give distinct values for different length 0 buffers.
      69             :     // value taken from FNV; it has no special significance.
      70             : 
      71           3 :     const u8* p = (const u8*)buf;
      72             : 
      73             :     // expected case: string
      74           3 :     if(!len)
      75             :     {
      76          14 :         while(*p)
      77             :         {
      78           6 :             h ^= *p++;
      79           6 :             h *= 0x100000001B3ull;
      80             :         }
      81             :     }
      82             :     else
      83             :     {
      84           1 :         size_t bytes_left = len;
      85          13 :         while(bytes_left != 0)
      86             :         {
      87           6 :             h ^= *p++;
      88           6 :             h *= 0x100000001B3ull;
      89             : 
      90           6 :             bytes_left--;
      91             :         }
      92             :     }
      93             : 
      94           3 :     return h;
      95           3 : }

Generated by: LCOV version 1.13