1 // Copyright (c) Facebook, Inc. and its affiliates. 2 // All rights reserved. 3 // 4 // Copyright 2019 Google LLC 5 // 6 // This source code is licensed under the BSD-style license found in the 7 // LICENSE file in the root directory of this source tree. 8 9 #pragma once 10 11 #if defined(__cplusplus) && (__cplusplus >= 201103L) 12 #include <climits> 13 #include <cstdint> 14 #include <cstdbool> 15 #include <cassert> 16 #else 17 #include <limits.h> 18 #include <stdint.h> 19 #include <stdbool.h> 20 #include <assert.h> 21 #endif 22 23 #include <fp16.h> 24 25 #include <xnnpack/common.h> 26 27 28 #if defined(__clang__) 29 #if __clang_major__ == 3 && __clang_minor__ >= 7 || __clang_major__ > 3 30 #define XNN_IGNORE_SHIFT_BASE_UB __attribute__((__no_sanitize__("shift-base"))) 31 #else 32 #define XNN_IGNORE_SHIFT_BASE_UB 33 #endif 34 #elif defined(__GNUC__) 35 #if __GNUC__ >= 8 36 #define XNN_IGNORE_SHIFT_BASE_UB __attribute__((__no_sanitize__("shift-base"))) 37 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9 || __GNUC__ > 4 38 // 4.9 <= gcc < 8 support ubsan, but doesn't support no_sanitize attribute 39 #define XNN_IGNORE_SHIFT_BASE_UB 40 #ifndef XNN_USE_SHIFT_BASE_UB_WORKAROUND 41 #define XNN_USE_SHIFT_BASE_UB_WORKAROUND 1 42 #endif 43 #else 44 #define XNN_IGNORE_SHIFT_BASE_UB 45 #endif 46 #else 47 #define XNN_IGNORE_SHIFT_BASE_UB 48 #endif 49 50 XNN_IGNORE_SHIFT_BASE_UB asr_s32(int32_t x,uint32_t n)51inline static int32_t asr_s32(int32_t x, uint32_t n) { 52 #ifdef XNN_USE_SHIFT_BASE_UB_WORKAROUND 53 #if XNN_ARCH_X86_64 || XNN_ARCH_ARM64 54 return (int32_t) ((uint64_t) (int64_t) x >> n); 55 #else 56 return x >= 0 ? x >> n : ~(~x >> n); 57 #endif 58 #else 59 return x >> n; 60 #endif 61 } 62 63 XNN_IGNORE_SHIFT_BASE_UB asr_s64(int64_t x,uint32_t n)64inline static int64_t asr_s64(int64_t x, uint32_t n) { 65 #ifdef XNN_USE_SHIFT_BASE_UB_WORKAROUND 66 return x >= 0 ? x >> n : ~(~x >> n); 67 #else 68 return x >> n; 69 #endif 70 } 71