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 #include <stddef.h>
12 #include <stdint.h>
13 #include <assert.h>
14
15 #include <xnnpack/common.h>
16
17
min(size_t a,size_t b)18 inline static size_t min(size_t a, size_t b) {
19 return XNN_UNPREDICTABLE(b < a) ? b : a;
20 }
21
max(size_t a,size_t b)22 inline static size_t max(size_t a, size_t b) {
23 return XNN_UNPREDICTABLE(b < a) ? a : b;
24 }
25
doz(size_t a,size_t b)26 inline static size_t doz(size_t a, size_t b) {
27 return XNN_UNPREDICTABLE(b < a) ? a - b : 0;
28 }
29
divide_round_up(size_t n,size_t q)30 inline static size_t divide_round_up(size_t n, size_t q) {
31 return XNN_UNPREDICTABLE(n % q == 0) ? n / q : n / q + 1;
32 }
33
round_up(size_t n,size_t q)34 inline static size_t round_up(size_t n, size_t q) {
35 return divide_round_up(n, q) * q;
36 }
37
round_down_po2(size_t n,size_t q)38 inline static size_t round_down_po2(size_t n, size_t q) {
39 assert(q != 0);
40 assert((q & (q - 1)) == 0);
41 return n & -q;
42 }
43
round_up_po2(size_t n,size_t q)44 inline static size_t round_up_po2(size_t n, size_t q) {
45 return round_down_po2(n + q - 1, q);
46 }
47
subtract_modulo(size_t a,size_t b,size_t m)48 inline static size_t subtract_modulo(size_t a, size_t b, size_t m) {
49 assert(a < m);
50 assert(b < m);
51 return XNN_UNPREDICTABLE(a >= b) ? a - b : a - b + m;
52 }
53
math_min_u32(uint32_t a,uint32_t b)54 inline static uint32_t math_min_u32(uint32_t a, uint32_t b) {
55 return XNN_UNPREDICTABLE(a < b) ? a : b;
56 }
57
math_max_u32(uint32_t a,uint32_t b)58 inline static uint32_t math_max_u32(uint32_t a, uint32_t b) {
59 return XNN_UNPREDICTABLE(a > b) ? a : b;
60 }
61
math_min_f32(float a,float b)62 inline static float math_min_f32(float a, float b) {
63 return XNN_UNPREDICTABLE(b < a) ? b : a;
64 }
65
math_max_f32(float a,float b)66 inline static float math_max_f32(float a, float b) {
67 return XNN_UNPREDICTABLE(b < a) ? a : b;
68 }
69