Nugget
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2021 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
31#ifdef __cplusplus
32
33#include <concepts>
34
35namespace Utilities {
36
37template <std::integral T, unsigned size = (sizeof(T) + 7) / 8>
38T loadUnaligned(const uint8_t *ptr) {
39 T ret = 0;
40 for (unsigned i = 0; i < size; i++) {
41 ret |= (ptr[i] << (i * 8));
42 }
43 return ret;
44}
45
46template <std::integral T, unsigned size = (sizeof(T) + 7) / 8>
47void storeUnaligned(uint8_t *ptr, T value) {
48 for (unsigned i = 0; i < size; i++) {
49 ptr[i] = value >> (i * 8);
50 }
51}
52
53} // namespace Utilities
54
55#endif
56
57static __inline__ uint32_t load32Unaligned(const void *in, int pos) {
58 const uint8_t *buffer = (const uint8_t *)in;
59 uint32_t r;
60 __builtin_memcpy(&r, buffer + pos, sizeof(uint32_t));
61 return r;
62}
volatile uint32_t * ptr
Definition cop0.c:80
uint32_t r
Definition cpu.c:222
char in[50]
Definition memcpy.c:74
Definition bitfield.hh:36
static int size
Definition string.h:32
static int value
Definition syscalls.h:534
static void * buffer
Definition syscalls.h:230
static int ret
Definition syscalls.h:72
void uint32_t(classId, spec)