blob: c5c650c5eb3034cbd012ebf15a28a5bfea0a8399 [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#include <stddef.h>
Marat Dukhan69722492019-11-11 19:55:50 -080012#include <stdint.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070013#include <assert.h>
14
Marat Dukhan69722492019-11-11 19:55:50 -080015#include <xnnpack/common.h>
16
Marat Dukhan72b32502019-11-12 17:10:50 -080017
Marat Dukhan9fe932e2020-04-11 17:14:15 -070018// stdlib.h from Windows 10 SDK defines min & max macros.
19// Undefine them before defining the corresponding functions.
20#ifdef min
21 #undef min
22#endif
23#ifdef max
24 #undef max
25#endif
26
27
XNNPACK Teamb455b122019-09-27 18:10:33 -070028inline static size_t min(size_t a, size_t b) {
Marat Dukhan72b32502019-11-12 17:10:50 -080029 return XNN_UNPREDICTABLE(b < a) ? b : a;
XNNPACK Teamb455b122019-09-27 18:10:33 -070030}
31
32inline static size_t max(size_t a, size_t b) {
Marat Dukhan72b32502019-11-12 17:10:50 -080033 return XNN_UNPREDICTABLE(b < a) ? a : b;
XNNPACK Teamb455b122019-09-27 18:10:33 -070034}
35
36inline static size_t doz(size_t a, size_t b) {
Marat Dukhan72b32502019-11-12 17:10:50 -080037 return XNN_UNPREDICTABLE(b < a) ? a - b : 0;
XNNPACK Teamb455b122019-09-27 18:10:33 -070038}
39
40inline static size_t divide_round_up(size_t n, size_t q) {
Marat Dukhan72b32502019-11-12 17:10:50 -080041 return XNN_UNPREDICTABLE(n % q == 0) ? n / q : n / q + 1;
XNNPACK Teamb455b122019-09-27 18:10:33 -070042}
43
44inline static size_t round_up(size_t n, size_t q) {
45 return divide_round_up(n, q) * q;
46}
47
48inline static size_t round_down_po2(size_t n, size_t q) {
49 assert(q != 0);
50 assert((q & (q - 1)) == 0);
51 return n & -q;
52}
53
54inline static size_t round_up_po2(size_t n, size_t q) {
55 return round_down_po2(n + q - 1, q);
56}
57
58inline static size_t subtract_modulo(size_t a, size_t b, size_t m) {
59 assert(a < m);
60 assert(b < m);
Marat Dukhan72b32502019-11-12 17:10:50 -080061 return XNN_UNPREDICTABLE(a >= b) ? a - b : a - b + m;
XNNPACK Teamb455b122019-09-27 18:10:33 -070062}
63
Marat Dukhan69722492019-11-11 19:55:50 -080064inline static uint32_t math_min_u32(uint32_t a, uint32_t b) {
65 return XNN_UNPREDICTABLE(a < b) ? a : b;
66}
67
68inline static uint32_t math_max_u32(uint32_t a, uint32_t b) {
69 return XNN_UNPREDICTABLE(a > b) ? a : b;
70}
71
XNNPACK Teamb455b122019-09-27 18:10:33 -070072inline static float math_min_f32(float a, float b) {
Marat Dukhanb1120882020-05-15 23:50:47 -070073 #if defined(__GNUC__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
74 return __builtin_fminf(a, b);
75 #elif defined(__clang__) && defined(__riscv)
76 return __builtin_fminf(a, b);
77 #else
78 return XNN_UNPREDICTABLE(b < a) ? b : a;
79 #endif
XNNPACK Teamb455b122019-09-27 18:10:33 -070080}
81
82inline static float math_max_f32(float a, float b) {
Marat Dukhanb1120882020-05-15 23:50:47 -070083 #if defined(__GNUC__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
84 return __builtin_fmaxf(a, b);
85 #elif defined(__clang__) && defined(__riscv)
86 return __builtin_fmaxf(a, b);
87 #else
88 return XNN_UNPREDICTABLE(b < a) ? a : b;
89 #endif
XNNPACK Teamb455b122019-09-27 18:10:33 -070090}
Marat Dukhan8853b822020-05-07 12:19:01 -070091
92inline static float math_nonsign_mask_f32() {
93 #if defined(__INTEL_COMPILER)
94 // Suprisingly, Intel compiler ignores __builtin_nanf payload
95 return _castu32_f32(0x7FFFFFFF);
Marat Dukhan5592e622020-05-08 01:05:00 -070096 #elif defined(__GNUC__)
Marat Dukhan8853b822020-05-07 12:19:01 -070097 return __builtin_nanf("0x7FFFFF");
Marat Dukhan5592e622020-05-08 01:05:00 -070098 #else
99 union {
100 uint32_t as_word;
101 float as_float;
102 } f;
103 f.as_word = 0x7FFFFFFF;
104 return f.as_float;
Marat Dukhan8853b822020-05-07 12:19:01 -0700105 #endif
106}
Marat Dukhan5592e622020-05-08 01:05:00 -0700107