yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
3.1 KiB103 linesraw
1// https://github.com/tobias-loew/constexpr-GUID-cpp-11
2
3//-------------------------------------------------------------------------------------------------------
4// constexpr GUID parsing
5// Written by Alexander Bessonov
6// Written by Tobias Loew
7//
8// Licensed under the MIT license.
9//-------------------------------------------------------------------------------------------------------
10
11#pragma once
12#include <stdexcept>
13#include <string>
14#include <cassert>
15#include <cstdint>
16
17#if !defined(GUID_DEFINED)
18#define GUID_DEFINED
19struct GUID {
20	uint32_t Data1;
21	uint16_t Data2;
22	uint16_t Data3;
23	uint8_t Data4[ 8 ];
24};
25#endif
26
27namespace ComLight
28{
29	namespace details
30	{
31		constexpr const size_t short_guid_form_length = 36;	// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
32		constexpr const size_t long_guid_form_length = 38;	// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
33
34		constexpr uint8_t parse_hex_digit( const char c )
35		{
36			using namespace std::string_literals;
37			return
38				( '0' <= c && c <= '9' )
39				? c - '0'
40				: ( 'a' <= c && c <= 'f' )
41				? 10 + c - 'a'
42				: ( 'A' <= c && c <= 'F' )
43				? 10 + c - 'A'
44				:
45				throw std::domain_error{ "invalid character in GUID"s };
46		}
47
48		constexpr uint8_t parse_hex_uint8_t( const char *ptr )
49		{
50			return ( parse_hex_digit( ptr[ 0 ] ) << 4 ) + parse_hex_digit( ptr[ 1 ] );
51		}
52
53		constexpr uint16_t parse_hex_uint16_t( const char *ptr )
54		{
55			return ( parse_hex_uint8_t( ptr ) << 8 ) + parse_hex_uint8_t( ptr + 2 );
56		}
57
58		constexpr uint32_t parse_hex_uint32_t( const char *ptr )
59		{
60			return ( parse_hex_uint16_t( ptr ) << 16 ) + parse_hex_uint16_t( ptr + 4 );
61		}
62
63		constexpr GUID parse_guid( const char *begin )
64		{
65			return GUID{
66				parse_hex_uint32_t( begin ),
67				parse_hex_uint16_t( begin + 8 + 1 ),
68				parse_hex_uint16_t( begin + 8 + 1 + 4 + 1 ),
69				{
70					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 ),
71					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 ),
72					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 ),
73					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 + 2 ),
74					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 + 2 + 2 ),
75					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 + 2 + 2 + 2 ),
76					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 + 2 + 2 + 2 + 2 ),
77					parse_hex_uint8_t( begin + 8 + 1 + 4 + 1 + 4 + 1 + 2 + 2 + 1 + 2 + 2 + 2 + 2 + 2 )
78				}
79			};
80		}
81
82		constexpr GUID make_guid_helper( const char *str, size_t N )
83		{
84			using namespace std::string_literals;
85			using namespace details;
86
87			return ( !( N == long_guid_form_length || N == short_guid_form_length ) )
88				? throw std::domain_error{ "String GUID of the form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} or XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX is expected"s }
89				: ( N == long_guid_form_length && ( str[ 0 ] != '{' || str[ long_guid_form_length - 1 ] != '}' ) )
90				? throw std::domain_error{ "Missing opening or closing brace"s }
91
92			: parse_guid( str + ( N == long_guid_form_length ? 1 : 0 ) );
93		}
94
95
96		template<size_t N>
97		constexpr GUID make_guid( const char( &str )[ N ] )
98		{
99			return make_guid_helper( str, N - 1 );
100		}
101	}
102	using details::make_guid;
103}