1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * Atomic extract 64 from 128-bit, generic version.
4 *
5 * Copyright (C) 2023 Linaro, Ltd.
6 */
7
8#ifndef HOST_LOAD_EXTRACT_AL16_AL8_H
9#define HOST_LOAD_EXTRACT_AL16_AL8_H
10
11/**
12 * load_atom_extract_al16_or_al8:
13 * @pv: host address
14 * @s: object size in bytes, @s <= 8.
15 *
16 * Load @s bytes from @pv, when pv % s != 0.  If [p, p+s-1] does not
17 * cross an 16-byte boundary then the access must be 16-byte atomic,
18 * otherwise the access must be 8-byte atomic.
19 */
20static inline uint64_t ATTRIBUTE_ATOMIC128_OPT
21load_atom_extract_al16_or_al8(void *pv, int s)
22{
23    uintptr_t pi = (uintptr_t)pv;
24    int o = pi & 7;
25    int shr = (HOST_BIG_ENDIAN ? 16 - s - o : o) * 8;
26    Int128 r;
27
28    pv = (void *)(pi & ~7);
29    if (pi & 8) {
30        uint64_t *p8 = __builtin_assume_aligned(pv, 16, 8);
31        uint64_t a = qatomic_read__nocheck(p8);
32        uint64_t b = qatomic_read__nocheck(p8 + 1);
33
34        if (HOST_BIG_ENDIAN) {
35            r = int128_make128(b, a);
36        } else {
37            r = int128_make128(a, b);
38        }
39    } else {
40        r = atomic16_read_ro(pv);
41    }
42    return int128_getlo(int128_urshift(r, shr));
43}
44
45#endif /* HOST_LOAD_EXTRACT_AL16_AL8_H */
46