aboutsummaryrefslogtreecommitdiff
path: root/src/Platform.cpp
blob: b011d93c572886fd2857096cf0ec5818f8e8666e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "../include/Platform.hpp"
#include <cassert>

namespace sibs
{
    static std::unordered_map<Platform, const char*> platformNames;
    static bool platformNamesSet = false;
    static int countSetBits(u64 value)
    {
        int count = 0;
        while(value)
        {
            count += (value & 1ULL);
            value >>= 1ULL;
        }
        return count;
    }

    // Returns -1 if no bit is set
    static u64 getLeastSignificantBitSetPosition(u64 value)
    {
        for(u64 i = 0; i < sizeof(u64); ++i)
        {
            if(value & 1ULL)
                return i;
            value >>= 1ULL;
        }
        return -1ULL;
    }

    bool containsPlatform(const std::vector<Platform> &platforms, Platform platform)
    {
        for (Platform vecPlatform : platforms)
        {
            if(platform & vecPlatform)
                return true;
        }
        return false;
    }

    const char* asString(Platform platform)
    {
        if(!platformNamesSet)
        {
            platformNamesSet = true;
            for(auto &it : PLATFORM_BY_NAME)
            {
                platformNames[it.second] = it.first.data;
            }
        }

        auto it = platformNames.find(platform);
        if(it != platformNames.end())
            return it->second;
        return nullptr;
    }

    Platform getPlatformByName(StringView name)
    {
        auto it = PLATFORM_BY_NAME.find(name);
        if(it != PLATFORM_BY_NAME.end())
            return it->second;
        return PLATFORM_INVALID;
    }

    bool isSamePlatformFamily(Platform a, Platform b)
    {
        return a & b;
    }

    bool isBaseForPlatform(Platform base, Platform platform)
    {
        return base == PLATFORM_ANY || base == platform || (isSamePlatformFamily(platform, base) && countSetBits(base) < countSetBits(platform));
    }

    Platform getPlatformGenericType(Platform platform)
    {
        if(platform == PLATFORM_INVALID || platform == PLATFORM_ANY)
            return PLATFORM_INVALID;
        return (Platform)(1ULL << (u64)getLeastSignificantBitSetPosition(platform));
    }
}