Path: blob/main/sys/contrib/ck/include/gcc/aarch64/ck_pr_lse.h
48422 views
/*1* Copyright 2009-2016 Samy Al Bahra.2* Copyright 2013-2016 Olivier Houchard.3* Copyright 2016 Alexey Kopytov.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef CK_PR_AARCH64_LSE_H29#define CK_PR_AARCH64_LSE_H3031#error bite32#ifndef CK_PR_H33#error Do not include this file directly, use ck_pr.h34#endif3536CK_CC_INLINE static bool37ck_pr_cas_64_2_value(uint64_t target[2], uint64_t compare[2], uint64_t set[2], uint64_t value[2])38{39uint64_t tmp1;40uint64_t tmp2;41register uint64_t x0 __asm__ ("x0") = compare[0];42register uint64_t x1 __asm__ ("x1") = compare[1];43register uint64_t x2 __asm__ ("x2") = set[0];44register uint64_t x3 __asm__ ("x3") = set[1];4546__asm__ __volatile__("casp %0, %1, %4, %5, [%6]\n"47"eor %2, %0, %7\n"48"eor %3, %1, %8\n"49"orr %2, %2, %3\n"50: "+&r" (x0), "+&r" (x1), "=&r" (tmp1), "=&r" (tmp2)51: "r" (x2), "r" (x3), "r" (target), "r" (compare[0]), "r" (compare[1])52: "memory");5354value[0] = x0;55value[1] = x1;5657return (!!tmp1);58}5960CK_CC_INLINE static bool61ck_pr_cas_ptr_2_value(void *target, void *compare, void *set, void *value)62{63return (ck_pr_cas_64_2_value(CK_CPP_CAST(uint64_t *, target),64CK_CPP_CAST(uint64_t *, compare),65CK_CPP_CAST(uint64_t *, set),66CK_CPP_CAST(uint64_t *, value)));67}6869CK_CC_INLINE static bool70ck_pr_cas_64_2(uint64_t target[2], uint64_t compare[2], uint64_t set[2])71{72register uint64_t x0 __asm__ ("x0") = compare[0];73register uint64_t x1 __asm__ ("x1") = compare[1];74register uint64_t x2 __asm__ ("x2") = set[0];75register uint64_t x3 __asm__ ("x3") = set[1];7677__asm__ __volatile__("casp %0, %1, %2, %3, [%4]\n"78"eor %0, %0, %5\n"79"eor %1, %1, %6\n"80"orr %0, %0, %1\n"81: "+&r" (x0), "+&r" (x1)82: "r" (x2), "r" (x3), "r" (target), "r" (compare[0]), "r" (compare[1])83: "memory");8485return (!!x0);86}87CK_CC_INLINE static bool88ck_pr_cas_ptr_2(void *target, void *compare, void *set)89{90return (ck_pr_cas_64_2(CK_CPP_CAST(uint64_t *, target),91CK_CPP_CAST(uint64_t *, compare),92CK_CPP_CAST(uint64_t *, set)));93}949596#define CK_PR_CAS(N, M, T, W, R) \97CK_CC_INLINE static bool \98ck_pr_cas_##N##_value(M *target, T compare, T set, M *value) \99{ \100*(T *)value = compare; \101__asm__ __volatile__( \102"cas" W " %" R "0, %" R "2, [%1]\n"\103: "+&r" (*(T *)value) \104: "r" (target), \105"r" (set) \106: "memory"); \107return (*(T *)value == compare); \108} \109CK_CC_INLINE static bool \110ck_pr_cas_##N(M *target, T compare, T set) \111{ \112T previous = compare; \113__asm__ __volatile__( \114"cas" W " %" R "0, %" R "2, [%1]\n"\115: "+&r" (previous) \116: "r" (target), \117"r" (set) \118: "memory"); \119return (previous == compare); \120}121122CK_PR_CAS(ptr, void, void *, "", "")123124#define CK_PR_CAS_S(N, M, W, R) CK_PR_CAS(N, M, M, W, R)125CK_PR_CAS_S(64, uint64_t, "", "")126#ifndef CK_PR_DISABLE_DOUBLE127CK_PR_CAS_S(double, double, "", "")128#endif129CK_PR_CAS_S(32, uint32_t, "", "w")130CK_PR_CAS_S(uint, unsigned int, "", "w")131CK_PR_CAS_S(int, int, "", "w")132CK_PR_CAS_S(16, uint16_t, "h", "w")133CK_PR_CAS_S(8, uint8_t, "b", "w")134CK_PR_CAS_S(short, short, "h", "w")135CK_PR_CAS_S(char, char, "b", "w")136137138#undef CK_PR_CAS_S139#undef CK_PR_CAS140141#define CK_PR_FAS(N, M, T, W, R) \142CK_CC_INLINE static T \143ck_pr_fas_##N(M *target, T v) \144{ \145T previous; \146__asm__ __volatile__( \147"swp" W " %" R "2, %" R "0, [%1]\n"\148: "=&r" (previous) \149: "r" (target), \150"r" (v) \151: "memory"); \152return (previous); \153}154155CK_PR_FAS(64, uint64_t, uint64_t, "", "")156CK_PR_FAS(32, uint32_t, uint32_t, "", "w")157CK_PR_FAS(ptr, void, void *, "", "")158CK_PR_FAS(int, int, int, "", "w")159CK_PR_FAS(uint, unsigned int, unsigned int, "", "w")160CK_PR_FAS(16, uint16_t, uint16_t, "h", "w")161CK_PR_FAS(8, uint8_t, uint8_t, "b", "w")162CK_PR_FAS(short, short, short, "h", "w")163CK_PR_FAS(char, char, char, "b", "w")164165166#undef CK_PR_FAS167168#define CK_PR_UNARY(O, N, M, T, I, W, R, S) \169CK_CC_INLINE static void \170ck_pr_##O##_##N(M *target) \171{ \172__asm__ __volatile__(I "\n" \173"st" S W " " R "0, [%0]\n" \174: \175: "r" (target) \176: "x0", "memory"); \177return; \178}179180CK_PR_UNARY(inc, ptr, void, void *, "mov x0, 1", "", "x", "add")181CK_PR_UNARY(dec, ptr, void, void *, "mov x0, -1", "", "x", "add")182CK_PR_UNARY(not, ptr, void, void *, "mov x0, -1", "", "x", "eor")183CK_PR_UNARY(inc, 64, uint64_t, uint64_t, "mov x0, 1", "", "x", "add")184CK_PR_UNARY(dec, 64, uint64_t, uint64_t, "mov x0, -1", "", "x", "add")185CK_PR_UNARY(not, 64, uint64_t, uint64_t, "mov x0, -1", "", "x", "eor")186187#define CK_PR_UNARY_S(S, T, W) \188CK_PR_UNARY(inc, S, T, T, "mov w0, 1", W, "w", "add") \189CK_PR_UNARY(dec, S, T, T, "mov w0, -1", W, "w", "add") \190CK_PR_UNARY(not, S, T, T, "mov w0, -1", W, "w", "eor") \191192CK_PR_UNARY_S(32, uint32_t, "")193CK_PR_UNARY_S(uint, unsigned int, "")194CK_PR_UNARY_S(int, int, "")195CK_PR_UNARY_S(16, uint16_t, "h")196CK_PR_UNARY_S(8, uint8_t, "b")197CK_PR_UNARY_S(short, short, "h")198CK_PR_UNARY_S(char, char, "b")199200#undef CK_PR_UNARY_S201#undef CK_PR_UNARY202203#define CK_PR_BINARY(O, N, M, T, S, W, R, I) \204CK_CC_INLINE static void \205ck_pr_##O##_##N(M *target, T delta) \206{ \207__asm__ __volatile__(I "\n" \208"st" S W " %" R "0, [%1]\n"\209: "+&r" (delta) \210: "r" (target) \211: "memory"); \212return; \213}214215CK_PR_BINARY(and, ptr, void, uintptr_t, "clr", "", "", "mvn %0, %0")216CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "", "", "")217CK_PR_BINARY(or, ptr, void, uintptr_t, "set", "", "", "")218CK_PR_BINARY(sub, ptr, void, uintptr_t, "add", "", "", "neg %0, %0")219CK_PR_BINARY(xor, ptr, void, uintptr_t, "eor", "", "", "")220CK_PR_BINARY(and, 64, uint64_t, uint64_t, "clr", "", "", "mvn %0, %0")221CK_PR_BINARY(add, 64, uint64_t, uint64_t, "add", "", "", "")222CK_PR_BINARY(or, 64, uint64_t, uint64_t, "set", "", "", "")223CK_PR_BINARY(sub, 64, uint64_t, uint64_t, "add", "", "", "neg %0, %0")224CK_PR_BINARY(xor, 64, uint64_t, uint64_t, "eor", "", "", "")225226#define CK_PR_BINARY_S(S, T, W) \227CK_PR_BINARY(and, S, T, T, "clr", W, "w", "mvn %w0, %w0") \228CK_PR_BINARY(add, S, T, T, "add", W, "w", "") \229CK_PR_BINARY(or, S, T, T, "set", W, "w", "") \230CK_PR_BINARY(sub, S, T, T, "add", W, "w", "neg %w0, %w0") \231CK_PR_BINARY(xor, S, T, T, "eor", W, "w", "")232233CK_PR_BINARY_S(32, uint32_t, "")234CK_PR_BINARY_S(uint, unsigned int, "")235CK_PR_BINARY_S(int, int, "")236CK_PR_BINARY_S(16, uint16_t, "h")237CK_PR_BINARY_S(8, uint8_t, "b")238CK_PR_BINARY_S(short, short, "h")239CK_PR_BINARY_S(char, char, "b")240241#undef CK_PR_BINARY_S242#undef CK_PR_BINARY243244CK_CC_INLINE static void *245ck_pr_faa_ptr(void *target, uintptr_t delta)246{247uintptr_t previous;248249__asm__ __volatile__(250"ldadd %2, %0, [%1]\n"251: "=r" (previous)252: "r" (target),253"r" (delta)254: "memory");255256return (void *)(previous);257}258259CK_CC_INLINE static uint64_t260ck_pr_faa_64(uint64_t *target, uint64_t delta)261{262uint64_t previous;263264__asm__ __volatile__(265"ldadd %2, %0, [%1]\n"266: "=r" (previous)267: "r" (target),268"r" (delta)269: "memory");270271return (previous);272}273274#define CK_PR_FAA(S, T, W) \275CK_CC_INLINE static T \276ck_pr_faa_##S(T *target, T delta) \277{ \278T previous; \279__asm__ __volatile__( \280"ldadd" W " %w2, %w0, [%1]\n" \281: "=r" (previous) \282: "r" (target), \283"r" (delta) \284: "memory"); \285return (previous); \286}287288CK_PR_FAA(32, uint32_t, "")289CK_PR_FAA(uint, unsigned int, "")290CK_PR_FAA(int, int, "")291CK_PR_FAA(16, uint16_t, "h")292CK_PR_FAA(8, uint8_t, "b")293CK_PR_FAA(short, short, "h")294CK_PR_FAA(char, char, "b")295296#undef CK_PR_FAA297298#endif /* CK_PR_AARCH64_LSE_H */299300301