compiler.h revision 7ec681f3
17ec681f3Smrg/*
27ec681f3Smrg * Mesa 3-D graphics library
37ec681f3Smrg *
47ec681f3Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
57ec681f3Smrg * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
67ec681f3Smrg *
77ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
87ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
97ec681f3Smrg * to deal in the Software without restriction, including without limitation
107ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
127ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice shall be included
157ec681f3Smrg * in all copies or substantial portions of the Software.
167ec681f3Smrg *
177ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
187ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
207ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
217ec681f3Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
227ec681f3Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
237ec681f3Smrg * OTHER DEALINGS IN THE SOFTWARE.
247ec681f3Smrg */
257ec681f3Smrg
267ec681f3Smrg
277ec681f3Smrg/**
287ec681f3Smrg * \file compiler.h
297ec681f3Smrg * Compiler-related stuff.
307ec681f3Smrg */
317ec681f3Smrg
327ec681f3Smrg
337ec681f3Smrg#ifndef COMPILER_H
347ec681f3Smrg#define COMPILER_H
357ec681f3Smrg
367ec681f3Smrg
377ec681f3Smrg#include <assert.h>
387ec681f3Smrg
397ec681f3Smrg#include "util/macros.h"
407ec681f3Smrg
417ec681f3Smrg#include "c99_compat.h" /* inline, __func__, etc. */
427ec681f3Smrg
437ec681f3Smrg
447ec681f3Smrg/**
457ec681f3Smrg * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
467ec681f3Smrg * Do not use these unless absolutely necessary!
477ec681f3Smrg * Try to use a runtime test instead.
487ec681f3Smrg * For now, only used by some DRI hardware drivers for color/texel packing.
497ec681f3Smrg */
507ec681f3Smrg#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
517ec681f3Smrg#if defined(__linux__)
527ec681f3Smrg#include <byteswap.h>
537ec681f3Smrg#define CPU_TO_LE32( x )	bswap_32( x )
547ec681f3Smrg#elif defined(__APPLE__)
557ec681f3Smrg#include <CoreFoundation/CFByteOrder.h>
567ec681f3Smrg#define CPU_TO_LE32( x )	CFSwapInt32HostToLittle( x )
577ec681f3Smrg#elif defined(__OpenBSD__)
587ec681f3Smrg#include <sys/types.h>
597ec681f3Smrg#define CPU_TO_LE32( x )	htole32( x )
607ec681f3Smrg#else /*__linux__ */
617ec681f3Smrg#include <sys/endian.h>
627ec681f3Smrg#define CPU_TO_LE32( x )	bswap32( x )
637ec681f3Smrg#endif /*__linux__*/
647ec681f3Smrg#define MESA_BIG_ENDIAN 1
657ec681f3Smrg#else
667ec681f3Smrg#define CPU_TO_LE32( x )	( x )
677ec681f3Smrg#define MESA_LITTLE_ENDIAN 1
687ec681f3Smrg#endif
697ec681f3Smrg#define LE32_TO_CPU( x )	CPU_TO_LE32( x )
707ec681f3Smrg
717ec681f3Smrg
727ec681f3Smrg
737ec681f3Smrg#define IEEE_ONE 0x3f800000
747ec681f3Smrg
757ec681f3Smrg#ifndef __has_attribute
767ec681f3Smrg#  define __has_attribute(x) 0
777ec681f3Smrg#endif
787ec681f3Smrg
797ec681f3Smrg#if defined(__has_cpp_attribute) && defined(__clang__)
807ec681f3Smrg/* We do not do the same trick as __has_attribute because parsing
817ec681f3Smrg * clang::fallthrough in the preprocessor fails in GCC. */
827ec681f3Smrg#  define HAS_CLANG_FALLTHROUGH  __has_cpp_attribute(clang::fallthrough)
837ec681f3Smrg#else
847ec681f3Smrg#  define HAS_CLANG_FALLTHROUGH 0
857ec681f3Smrg#endif
867ec681f3Smrg
877ec681f3Smrg#if __cplusplus >= 201703L || __STDC_VERSION__ > 201710L
887ec681f3Smrg/* Standard C++17/C23 attribute */
897ec681f3Smrg#define FALLTHROUGH [[fallthrough]]
907ec681f3Smrg#elif HAS_CLANG_FALLTHROUGH
917ec681f3Smrg/* Clang++ specific */
927ec681f3Smrg#define FALLTHROUGH [[clang::fallthrough]]
937ec681f3Smrg#elif __has_attribute(fallthrough)
947ec681f3Smrg/* Non-standard but supported by at least gcc and clang */
957ec681f3Smrg#define FALLTHROUGH __attribute__((fallthrough))
967ec681f3Smrg#else
977ec681f3Smrg#define FALLTHROUGH do { } while(0)
987ec681f3Smrg#endif
997ec681f3Smrg
1007ec681f3Smrg#endif /* COMPILER_H */
101