blob: 57cb681810a5590ddd1a17b44c6fb2d53e3b5eb2 [file] [log] [blame]
Steven Morelanda64f3362017-05-31 12:48:55 -07001#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17###########################################################
18# Basic math functions for positive integers <= 100
19#
20# (SDK versions for example)
21###########################################################
22__MATH_NUMBERS := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
23 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
24 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
25 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 \
26 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
27
28# Returns true if $(1) is a positive integer <= 100, otherwise returns nothing.
29define math_is_number
30$(strip \
31 $(if $(1),,$(error Argument missing)) \
32 $(if $(word 2,$(1)),$(error Multiple words in a single argument: $(1))) \
33 $(if $(filter $(1),$(__MATH_NUMBERS)),true))
34endef
35
36#$(warning true == $(call math_is_number,2))
37#$(warning == $(call math_is_number,foo))
38#$(call math_is_number,1 2)
39#$(call math_is_number,no 2)
40
41define _math_check_valid
42$(if $(call math_is_number,$(1)),,$(error Only positive integers <= 100 are supported (not $(1))))
43endef
44
Nan Zhangad818dc2017-10-04 09:26:06 -070045# return a list containing integers ranging from [$(1),$(2)]
46define int_range_list
47$(call _math_check_valid,$(1))$(call _math_check_valid,$(2))$(wordlist $(1),$(2),$(__MATH_NUMBERS))
48endef
49
Steven Morelanda64f3362017-05-31 12:48:55 -070050#$(call _math_check_valid,0)
51#$(call _math_check_valid,1)
52#$(call _math_check_valid,100)
53#$(call _math_check_valid,101)
54#$(call _math_check_valid,)
55#$(call _math_check_valid,1 2)
56
57# Returns the greater of $1 or $2.
58# If $1 or $2 is not a positive integer <= 100, then an error is generated.
59define math_max
60$(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \
61 $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS))))
62endef
63
64#$(call math_max)
65#$(call math_max,1)
66#$(call math_max,1 2,3)
67#$(warning 1 == $(call math_max,1,1))
68#$(warning 42 == $(call math_max,5,42))
69#$(warning 42 == $(call math_max,42,5))
70
71define math_gt_or_eq
72$(if $(filter $(1),$(call math_max,$(1),$(2))),true)
73endef
74
75#$(warning $(call math_gt_or_eq, 2, 1))
76#$(warning $(call math_gt_or_eq, 1, 1))
77#$(warning $(if $(call math_gt_or_eq, 1, 2),false,true))
78
79# $1 is the variable name to increment
80define inc_and_print
81$(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
82endef
Nan Zhangad818dc2017-10-04 09:26:06 -070083
84_INT_LIMIT_WORDS := $(foreach a,x x,$(foreach b,x x x x x x x x x x x x x x x x,\
85 $(foreach c,x x x x x x x x x x x x x x x x,x x x x x x x x x x x x x x x x)))
86
87define _int_encode
88$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
89 $(call pretty-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
90 $(wordlist 1,$(1),$(_INT_LIMIT_WORDS)))
91endef
92
93# _int_max returns the maximum of the two arguments
94# input: two (x) lists; output: one (x) list
95# integer cannot be passed in directly. It has to be converted using _int_encode.
96define _int_max
97$(subst xx,x,$(join $(1),$(2)))
98endef
99
100# first argument is greater than second argument
101# output: non-empty if true
102# integer cannot be passed in directly. It has to be converted using _int_encode.
103define _int_greater-than
104$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2))))
105endef
106
107# first argument equals to second argument
108# output: non-empty if true
109# integer cannot be passed in directly. It has to be converted using _int_encode.
110define _int_equal
111$(filter $(words $(1)),$(words $(2)))
112endef
113
114# first argument is greater than or equal to second argument
115# output: non-empty if true
116# integer cannot be passed in directly. It has to be converted using _int_encode.
117define _int_greater-or-equal
118$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2))
119endef
120
121define int_plus
122$(words $(call _int_encode,$(1)) $(call _int_encode,$(2)))
123endef
124
125define int_subtract
126$(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
127 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
128 $(call pretty-error,$(1) subtract underflow $(2)))
129endef
130
131define int_multiply
132$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
133endef
134
135define int_divide
136$(if $(filter 0,$(2)),$(call pretty-error,division by zero is not allowed!),$(strip \
137 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
138 $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0)))
139endef