xref: /netbsd-src/sys/external/bsd/common/include/linux/compiler.h (revision fb02afa0ae01e4e0c884536a5b4585a5ffea5d1d)
1*fb02afa0Sriastradh /*	$NetBSD: compiler.h,v 1.9 2022/07/17 08:34:00 riastradh Exp $	*/
2704dac09Sriastradh 
3704dac09Sriastradh /*-
4704dac09Sriastradh  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5704dac09Sriastradh  * All rights reserved.
6704dac09Sriastradh  *
7704dac09Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
8704dac09Sriastradh  * by Taylor R. Campbell.
9704dac09Sriastradh  *
10704dac09Sriastradh  * Redistribution and use in source and binary forms, with or without
11704dac09Sriastradh  * modification, are permitted provided that the following conditions
12704dac09Sriastradh  * are met:
13704dac09Sriastradh  * 1. Redistributions of source code must retain the above copyright
14704dac09Sriastradh  *    notice, this list of conditions and the following disclaimer.
15704dac09Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
16704dac09Sriastradh  *    notice, this list of conditions and the following disclaimer in the
17704dac09Sriastradh  *    documentation and/or other materials provided with the distribution.
18704dac09Sriastradh  *
19704dac09Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20704dac09Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21704dac09Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22704dac09Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23704dac09Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24704dac09Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25704dac09Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26704dac09Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27704dac09Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28704dac09Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29704dac09Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
30704dac09Sriastradh  */
31704dac09Sriastradh 
32704dac09Sriastradh #ifndef	_LINUX_COMPILER_H_
33704dac09Sriastradh #define	_LINUX_COMPILER_H_
34704dac09Sriastradh 
3571ee315fSriastradh #include <sys/atomic.h>
364301684eSriastradh #include <sys/cdefs.h>
3771ee315fSriastradh 
389cda6864Sriastradh #include <linux/stddef.h>
399cda6864Sriastradh 
40b641547dSriastradh #include <asm/barrier.h>
41b641547dSriastradh 
424301684eSriastradh #define	__printf	__printflike
434301684eSriastradh #define	__user
444301684eSriastradh #if __GNUC_PREREQ__(4,0)	/* not sure when but this will work */
454301684eSriastradh #define	__must_check	__attribute__((warn_unused_result))
464301684eSriastradh #else
474301684eSriastradh #define	__must_check	/* nothing */
484301684eSriastradh #endif
494301684eSriastradh #define	__always_unused	__unused
504301684eSriastradh #define	__maybe_unused	__unused
514301684eSriastradh #define	noinline	__noinline
524301684eSriastradh #define	__deprecated	/* nothing */
53cae04ea7Sriastradh #define	__acquire(X)	/* nothing */
54cae04ea7Sriastradh #define	__release(X)	/* nothing */
554301684eSriastradh 
564301684eSriastradh #define	barrier()	__insn_barrier()
574301684eSriastradh #define	likely(X)	__predict_true(X)
584301684eSriastradh #define	unlikely(X)	__predict_false(X)
59a508df5bSriastradh #define	__same_type(X,Y)						      \
60a508df5bSriastradh 	__builtin_types_compatible_p(__typeof__(X), __typeof__(Y))
61a508df5bSriastradh #define	__must_be_array(X)						      \
62a508df5bSriastradh 	BUILD_BUG_ON_ZERO(__same_type((X), &(X)[0]))
634301684eSriastradh 
6471ee315fSriastradh #define	READ_ONCE(X)	({						      \
65*fb02afa0Sriastradh 	__insn_barrier();						      \
6671ee315fSriastradh 	typeof(X) __read_once_tmp = (X);				      \
6771ee315fSriastradh 	membar_datadep_consumer();					      \
6871ee315fSriastradh 	__read_once_tmp;						      \
6971ee315fSriastradh })
7071ee315fSriastradh 
7171ee315fSriastradh #define	WRITE_ONCE(X, V)	({					      \
7271ee315fSriastradh 	typeof(X) __write_once_tmp = (V);				      \
73*fb02afa0Sriastradh 	__insn_barrier();						      \
7471ee315fSriastradh 	(X) = __write_once_tmp;						      \
75*fb02afa0Sriastradh 	__insn_barrier();						      \
7671ee315fSriastradh 	__write_once_tmp;						      \
7771ee315fSriastradh })
7871ee315fSriastradh 
79b641547dSriastradh #define	smp_store_mb(X, V)	do {					      \
80b641547dSriastradh 	WRITE_ONCE(X, V);						      \
81b641547dSriastradh 	smp_mb();							      \
82b641547dSriastradh } while (0)
83b641547dSriastradh 
84b641547dSriastradh #define	smp_store_release(X, V)	do {					      \
85b641547dSriastradh 	typeof(X) __smp_store_release_tmp = (V);			      \
8604caf091Sriastradh 	membar_release();						      \
87b641547dSriastradh 	(X) = __write_once_tmp;						      \
88b641547dSriastradh } while (0)
89b641547dSriastradh 
90704dac09Sriastradh #endif	/* _LINUX_COMPILER_H_ */
91