blob: 341a4ae5a1759fc0b5afb680cac1da6278fe9702 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// 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
Marat Dukhanc72fa1e2019-11-27 11:54:03 -080025#include <xnnpack/common.h>
26
27
Marat Dukhanf42facc2020-03-08 15:14:53 -070028#if defined(__clang__)
XNNPACK Teamb455b122019-09-27 18:10:33 -070029 #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
Marat Dukhan80fc9322019-09-29 21:06:36 -070038 // 4.9 <= gcc < 8 support ubsan, but doesn't support no_sanitize attribute
XNNPACK Teamb455b122019-09-27 18:10:33 -070039 #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
50XNN_IGNORE_SHIFT_BASE_UB
51inline static int32_t asr_s32(int32_t x, uint32_t n) {
52 #ifdef XNN_USE_SHIFT_BASE_UB_WORKAROUND
Marat Dukhanc72fa1e2019-11-27 11:54:03 -080053 #if XNN_ARCH_X86_64 || XNN_ARCH_ARM64
XNNPACK Teamb455b122019-09-27 18:10:33 -070054 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
63XNN_IGNORE_SHIFT_BASE_UB
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}