xref: /aosp_15_r20/build/make/common/math.mk (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1*9e94795aSAndroid Build Coastguard Worker#
2*9e94795aSAndroid Build Coastguard Worker# Copyright (C) 2017 The Android Open Source Project
3*9e94795aSAndroid Build Coastguard Worker#
4*9e94795aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*9e94795aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*9e94795aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*9e94795aSAndroid Build Coastguard Worker#
8*9e94795aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*9e94795aSAndroid Build Coastguard Worker#
10*9e94795aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*9e94795aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*9e94795aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9e94795aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*9e94795aSAndroid Build Coastguard Worker# limitations under the License.
15*9e94795aSAndroid Build Coastguard Worker#
16*9e94795aSAndroid Build Coastguard Worker
17*9e94795aSAndroid Build Coastguard Worker###########################################################
18*9e94795aSAndroid Build Coastguard Worker# Basic math functions for non-negative integers <= 100
19*9e94795aSAndroid Build Coastguard Worker#
20*9e94795aSAndroid Build Coastguard Worker# (SDK versions for example)
21*9e94795aSAndroid Build Coastguard Worker###########################################################
22*9e94795aSAndroid Build Coastguard Worker__MATH_POS_NUMBERS :=  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 \
23*9e94795aSAndroid Build Coastguard Worker                      21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
24*9e94795aSAndroid Build Coastguard Worker                      41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
25*9e94795aSAndroid Build Coastguard Worker                      61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 \
26*9e94795aSAndroid Build Coastguard Worker                      81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
27*9e94795aSAndroid Build Coastguard Worker__MATH_NUMBERS := 0 $(__MATH_POS_NUMBERS)
28*9e94795aSAndroid Build Coastguard Worker__MATH_ONE_NUMBERS := 0 1 2 3 4 5 6 7 8 9
29*9e94795aSAndroid Build Coastguard Worker
30*9e94795aSAndroid Build Coastguard Workermath-error = $(call pretty-error,$(1))
31*9e94795aSAndroid Build Coastguard Workermath-expect :=
32*9e94795aSAndroid Build Coastguard Workermath-expect-true :=
33*9e94795aSAndroid Build Coastguard Workermath-expect :=
34*9e94795aSAndroid Build Coastguard Workermath-expect-error :=
35*9e94795aSAndroid Build Coastguard Worker
36*9e94795aSAndroid Build Coastguard Worker# Run the math tests with:
37*9e94795aSAndroid Build Coastguard Worker#  make -f ${ANDROID_BUILD_TOP}/build/make/common/math.mk RUN_MATH_TESTS=true
38*9e94795aSAndroid Build Coastguard Worker#  $(get_build_var CKATI) -f ${ANDROID_BUILD_TOP}//build/make/common/math.mk RUN_MATH_TESTS=true
39*9e94795aSAndroid Build Coastguard Workerifdef RUN_MATH_TESTS
40*9e94795aSAndroid Build Coastguard Worker  ifndef empty
41*9e94795aSAndroid Build Coastguard Worker    empty :=
42*9e94795aSAndroid Build Coastguard Worker    space := $(empty) $(empty)
43*9e94795aSAndroid Build Coastguard Worker  endif
44*9e94795aSAndroid Build Coastguard Worker  MATH_TEST_FAILURE :=
45*9e94795aSAndroid Build Coastguard Worker  MATH_TEST_ERROR :=
46*9e94795aSAndroid Build Coastguard Worker  math-error = $(if $(MATH_TEST_ERROR),,$(eval MATH_TEST_ERROR:=$(1)))
47*9e94795aSAndroid Build Coastguard Worker  define math-expect
48*9e94795aSAndroid Build Coastguard Worker    $(eval got:=$$$1) \
49*9e94795aSAndroid Build Coastguard Worker    $(if $(subst $(got),,$(2))$(subst $(2),,$(got))$(MATH_TEST_ERROR), \
50*9e94795aSAndroid Build Coastguard Worker      $(if $(MATH_TEST_ERROR),$(warning $(MATH_TEST_ERROR)),$(warning $$$1 '$(got)' != '$(2)')) \
51*9e94795aSAndroid Build Coastguard Worker      $(eval MATH_TEST_FAILURE := true)) \
52*9e94795aSAndroid Build Coastguard Worker    $(eval MATH_TEST_ERROR :=) \
53*9e94795aSAndroid Build Coastguard Worker    $(eval got:=)
54*9e94795aSAndroid Build Coastguard Worker  endef
55*9e94795aSAndroid Build Coastguard Worker  math-expect-true = $(call math-expect,$(1),true)
56*9e94795aSAndroid Build Coastguard Worker  math-expect-false = $(call math-expect,$(1),)
57*9e94795aSAndroid Build Coastguard Worker
58*9e94795aSAndroid Build Coastguard Worker  define math-expect-error
59*9e94795aSAndroid Build Coastguard Worker    $(eval got:=$$$1) \
60*9e94795aSAndroid Build Coastguard Worker    $(if $(subst $(MATH_TEST_ERROR),,$(2))$(subst $(2),,$(MATH_TEST_ERROR)), \
61*9e94795aSAndroid Build Coastguard Worker      $(warning '$(MATH_TEST_ERROR)' != '$(2)') \
62*9e94795aSAndroid Build Coastguard Worker      $(eval MATH_TEST_FAILURE := true)) \
63*9e94795aSAndroid Build Coastguard Worker    $(eval MATH_TEST_ERROR :=) \
64*9e94795aSAndroid Build Coastguard Worker    $(eval got:=)
65*9e94795aSAndroid Build Coastguard Worker  endef
66*9e94795aSAndroid Build Coastguard Workerendif
67*9e94795aSAndroid Build Coastguard Worker
68*9e94795aSAndroid Build Coastguard Worker# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing.
69*9e94795aSAndroid Build Coastguard Workerdefine math_is_number_in_100
70*9e94795aSAndroid Build Coastguard Worker$(strip \
71*9e94795aSAndroid Build Coastguard Worker  $(if $(1),,$(call math-error,Argument missing)) \
72*9e94795aSAndroid Build Coastguard Worker  $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
73*9e94795aSAndroid Build Coastguard Worker  $(if $(filter $(1),$(__MATH_NUMBERS)),true))
74*9e94795aSAndroid Build Coastguard Workerendef
75*9e94795aSAndroid Build Coastguard Worker
76*9e94795aSAndroid Build Coastguard Worker# Same with math_is_number_in_100, but no limit.
77*9e94795aSAndroid Build Coastguard Workerdefine _math_ext_is_number
78*9e94795aSAndroid Build Coastguard Worker$(strip \
79*9e94795aSAndroid Build Coastguard Worker  $(if $(1),,$(call math-error,Argument missing)) \
80*9e94795aSAndroid Build Coastguard Worker  $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
81*9e94795aSAndroid Build Coastguard Worker  $(eval should_empty:=$(1)) \
82*9e94795aSAndroid Build Coastguard Worker  $(foreach num,$(__MATH_ONE_NUMBERS),\
83*9e94795aSAndroid Build Coastguard Worker    $(eval should_empty:=$(subst $(num),$(empty),$(should_empty)))) \
84*9e94795aSAndroid Build Coastguard Worker  $(if $(should_empty),,true))
85*9e94795aSAndroid Build Coastguard Workerendef
86*9e94795aSAndroid Build Coastguard Worker
87*9e94795aSAndroid Build Coastguard Worker# Returns true if $(1) is a non-negative integer.
88*9e94795aSAndroid Build Coastguard Workerdefine math_is_number
89*9e94795aSAndroid Build Coastguard Worker$(strip $(if $(call math_is_number_in_100,$(1)),true,$(call _math_ext_is_number,$(1))))
90*9e94795aSAndroid Build Coastguard Workerendef
91*9e94795aSAndroid Build Coastguard Worker
92*9e94795aSAndroid Build Coastguard Workerdefine math_is_zero
93*9e94795aSAndroid Build Coastguard Worker$(strip \
94*9e94795aSAndroid Build Coastguard Worker  $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
95*9e94795aSAndroid Build Coastguard Worker  $(if $(filter 0,$(1)),true))
96*9e94795aSAndroid Build Coastguard Workerendef
97*9e94795aSAndroid Build Coastguard Worker
98*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_is_number,0))
99*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_is_number,2))
100*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_is_number,202412))
101*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_is_number,foo))
102*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_is_number,-1))
103*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_is_number,1 2),Multiple words in a single argument: 1 2)
104*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_is_number,no 2),Multiple words in a single argument: no 2)
105*9e94795aSAndroid Build Coastguard Worker
106*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_is_zero,0))
107*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_is_zero,1))
108*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_is_zero,foo))
109*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_is_zero,1 2),Multiple words in a single argument: 1 2)
110*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_is_zero,no 2),Multiple words in a single argument: no 2)
111*9e94795aSAndroid Build Coastguard Worker
112*9e94795aSAndroid Build Coastguard Workerdefine _math_check_valid
113*9e94795aSAndroid Build Coastguard Worker$(if $(call math_is_number_in_100,$(1)),,$(call math-error,Only non-negative integers <= 100 are supported (not $(1))))
114*9e94795aSAndroid Build Coastguard Workerendef
115*9e94795aSAndroid Build Coastguard Worker
116*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_check_valid,0))
117*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_check_valid,1))
118*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_check_valid,100))
119*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_check_valid,-1),Only non-negative integers <= 100 are supported (not -1))
120*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_check_valid,101),Only non-negative integers <= 100 are supported (not 101))
121*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_check_valid,),Argument missing)
122*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_check_valid,1 2),Multiple words in a single argument: 1 2)
123*9e94795aSAndroid Build Coastguard Worker
124*9e94795aSAndroid Build Coastguard Worker# return a list containing integers ranging from [$(1),$(2)]
125*9e94795aSAndroid Build Coastguard Workerdefine int_range_list
126*9e94795aSAndroid Build Coastguard Worker$(strip \
127*9e94795aSAndroid Build Coastguard Worker  $(call _math_check_valid,$(1))$(call _math_check_valid,$(2)) \
128*9e94795aSAndroid Build Coastguard Worker  $(if $(call math_is_zero,$(1)),0)\
129*9e94795aSAndroid Build Coastguard Worker  $(wordlist $(if $(call math_is_zero,$(1)),1,$(1)),$(2),$(__MATH_POS_NUMBERS)))
130*9e94795aSAndroid Build Coastguard Workerendef
131*9e94795aSAndroid Build Coastguard Worker
132*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_range_list,0,1),0 1)
133*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_range_list,1,1),1)
134*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_range_list,1,2),1 2)
135*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_range_list,2,1),)
136*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call int_range_list,1,101),Only non-negative integers <= 100 are supported (not 101))
137*9e94795aSAndroid Build Coastguard Worker
138*9e94795aSAndroid Build Coastguard Worker# Split an integer into a list of digits
139*9e94795aSAndroid Build Coastguard Workerdefine _math_number_to_list
140*9e94795aSAndroid Build Coastguard Worker$(strip \
141*9e94795aSAndroid Build Coastguard Worker  $(if $(call _math_ext_is_number,$(1)),,\
142*9e94795aSAndroid Build Coastguard Worker    $(call math-error,Only non-negative integers are supported (not $(1)))) \
143*9e94795aSAndroid Build Coastguard Worker  $(eval num_list:=$(1)) \
144*9e94795aSAndroid Build Coastguard Worker  $(foreach num,$(__MATH_ONE_NUMBERS),\
145*9e94795aSAndroid Build Coastguard Worker    $(eval num_list:=$(subst $(num),$(space)$(num),$(num_list)))) \
146*9e94795aSAndroid Build Coastguard Worker  $(if $(filter $(words $(num_list)),$(__MATH_ONE_NUMBERS)),,\
147*9e94795aSAndroid Build Coastguard Worker    $(call math-error,Only non-negative integers with less than 9 digits are supported (not $(1)))) \
148*9e94795aSAndroid Build Coastguard Worker  $(if $(filter 0,$(word 1,$(num_list))),\
149*9e94795aSAndroid Build Coastguard Worker    $(call math-error,Only non-negative integers without leading zeros are supported (not $(1)))) \
150*9e94795aSAndroid Build Coastguard Worker  $(num_list))
151*9e94795aSAndroid Build Coastguard Workerendef
152*9e94795aSAndroid Build Coastguard Worker
153*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_number_to_list,123),1 2 3)
154*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_number_to_list,123 456),Multiple words in a single argument: 123 456)
155*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_number_to_list,-123),Only non-negative integers are supported (not -123))
156*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_number_to_list,002),Only non-negative integers without leading zeros are supported (not 002))
157*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call _math_number_to_list,1234567890),Only non-negative integers with less than 9 digits are supported (not 1234567890))
158*9e94795aSAndroid Build Coastguard Worker
159*9e94795aSAndroid Build Coastguard Worker# Compare 1-digit integer $(1) and $(2).
160*9e94795aSAndroid Build Coastguard Worker# Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals.
161*9e94795aSAndroid Build Coastguard Workerdefine _math_1digit_comp
162*9e94795aSAndroid Build Coastguard Worker$(strip \
163*9e94795aSAndroid Build Coastguard Worker  $(if $(filter $(1),$(2)),,\
164*9e94795aSAndroid Build Coastguard Worker    $(if $(filter $(1),$(firstword $(filter $(1) $(2),$(__MATH_ONE_NUMBERS)))),-1,1)))
165*9e94795aSAndroid Build Coastguard Workerendef
166*9e94795aSAndroid Build Coastguard Worker
167*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_1digit_comp,1,1))
168*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_1digit_comp,0,9),-1)
169*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_1digit_comp,3,1),1)
170*9e94795aSAndroid Build Coastguard Worker
171*9e94795aSAndroid Build Coastguard Worker# Compare the same $(3)-digit-length integers $(1) and $(2) that are split into a list of digits.
172*9e94795aSAndroid Build Coastguard Worker# Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals.
173*9e94795aSAndroid Build Coastguard Workerdefine _math_list_comp
174*9e94795aSAndroid Build Coastguard Worker$(strip \
175*9e94795aSAndroid Build Coastguard Worker  $(eval ans:=) \
176*9e94795aSAndroid Build Coastguard Worker  $(foreach num,$(call int_range_list,1,$(3)),\
177*9e94795aSAndroid Build Coastguard Worker    $(if $(ans),,$(eval ans:=$(call _math_1digit_comp,$(word $(num),$(1)),$(word $(num),$(2)))))) \
178*9e94795aSAndroid Build Coastguard Worker  $(ans))
179*9e94795aSAndroid Build Coastguard Workerendef
180*9e94795aSAndroid Build Coastguard Worker
181*9e94795aSAndroid Build Coastguard Worker# Compare any two non-negative integers $(1) and $(2).
182*9e94795aSAndroid Build Coastguard Worker# Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals.
183*9e94795aSAndroid Build Coastguard Workerdefine _math_ext_comp
184*9e94795aSAndroid Build Coastguard Worker$(strip \
185*9e94795aSAndroid Build Coastguard Worker  $(eval num_list1:=$(call _math_number_to_list,$(1))) \
186*9e94795aSAndroid Build Coastguard Worker  $(eval len1:=$(words $(num_list1))) \
187*9e94795aSAndroid Build Coastguard Worker  $(eval num_list2:=$(call _math_number_to_list,$(2))) \
188*9e94795aSAndroid Build Coastguard Worker  $(eval len2:=$(words $(num_list2))) \
189*9e94795aSAndroid Build Coastguard Worker  $(eval comp:=$(call _math_1digit_comp,$(len1),$(len2))) \
190*9e94795aSAndroid Build Coastguard Worker  $(if $(comp),$(comp),$(call _math_list_comp,$(num_list1),$(num_list2),$(len1))))
191*9e94795aSAndroid Build Coastguard Workerendef
192*9e94795aSAndroid Build Coastguard Worker
193*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_ext_comp,5,10),-1)
194*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_ext_comp,12345,12345))
195*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_ext_comp,500,5),1)
196*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call _math_ext_comp,202404,202504),-1)
197*9e94795aSAndroid Build Coastguard Worker
198*9e94795aSAndroid Build Coastguard Worker# Returns the greater of $1 or $2.
199*9e94795aSAndroid Build Coastguard Worker# If $1 or $2 is not a positive integer, then an error is generated.
200*9e94795aSAndroid Build Coastguard Workerdefine math_max
201*9e94795aSAndroid Build Coastguard Worker$(strip \
202*9e94795aSAndroid Build Coastguard Worker  $(if $(filter truetrue,$(call math_is_number_in_100,$(1))$(call math_is_number_in_100,$(2))),\
203*9e94795aSAndroid Build Coastguard Worker    $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS))),\
204*9e94795aSAndroid Build Coastguard Worker    $(if $(filter 1,$(call _math_ext_comp,$(1),$(2))),$(1),$(2))))
205*9e94795aSAndroid Build Coastguard Workerendef
206*9e94795aSAndroid Build Coastguard Worker
207*9e94795aSAndroid Build Coastguard Worker# Returns the lesser of $1 or $2.
208*9e94795aSAndroid Build Coastguard Workerdefine math_min
209*9e94795aSAndroid Build Coastguard Worker$(strip \
210*9e94795aSAndroid Build Coastguard Worker  $(if $(filter truetrue,$(call math_is_number_in_100,$(1))$(call math_is_number_in_100,$(2))),\
211*9e94795aSAndroid Build Coastguard Worker    $(firstword $(filter $(1) $(2),$(__MATH_NUMBERS))),\
212*9e94795aSAndroid Build Coastguard Worker    $(if $(filter -1,$(call _math_ext_comp,$(1),$(2))),$(1),$(2))))
213*9e94795aSAndroid Build Coastguard Workerendef
214*9e94795aSAndroid Build Coastguard Worker
215*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_max),Argument missing)
216*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_max,1),Argument missing)
217*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_max,1 2,3),Multiple words in a single argument: 1 2)
218*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call math_min,1,2 3),Multiple words in a single argument: 2 3)
219*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,0,1),1)
220*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,1,0),1)
221*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,1,1),1)
222*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,5,42),42)
223*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,42,5),42)
224*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,0,1),0)
225*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,1,0),0)
226*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,1,1),1)
227*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,7,32),7)
228*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,32,7),7)
229*9e94795aSAndroid Build Coastguard Worker
230*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,32759,7),32759)
231*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,7,32759),32759)
232*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,202404,202505),202505)
233*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_max,202404,202404),202404)
234*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,8908527,32),32)
235*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,32,8908527),32)
236*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,202404,202505),202404)
237*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call math_min,202404,202404),202404)
238*9e94795aSAndroid Build Coastguard Worker
239*9e94795aSAndroid Build Coastguard Workerdefine math_gt_or_eq
240*9e94795aSAndroid Build Coastguard Worker$(if $(filter $(1),$(call math_max,$(1),$(2))),true)
241*9e94795aSAndroid Build Coastguard Workerendef
242*9e94795aSAndroid Build Coastguard Worker
243*9e94795aSAndroid Build Coastguard Workerdefine math_gt
244*9e94795aSAndroid Build Coastguard Worker$(if $(call math_gt_or_eq,$(2),$(1)),,true)
245*9e94795aSAndroid Build Coastguard Workerendef
246*9e94795aSAndroid Build Coastguard Worker
247*9e94795aSAndroid Build Coastguard Workerdefine math_lt_or_eq
248*9e94795aSAndroid Build Coastguard Worker$(if $(call math_gt_or_eq,$(2),$(1)),true)
249*9e94795aSAndroid Build Coastguard Workerendef
250*9e94795aSAndroid Build Coastguard Worker
251*9e94795aSAndroid Build Coastguard Workerdefine math_lt
252*9e94795aSAndroid Build Coastguard Worker$(if $(call math_gt_or_eq,$(1),$(2)),,true)
253*9e94795aSAndroid Build Coastguard Workerendef
254*9e94795aSAndroid Build Coastguard Worker
255*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_gt_or_eq, 2, 1))
256*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_gt_or_eq, 1, 1))
257*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_gt_or_eq, 1, 2))
258*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_gt, 4, 3))
259*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_gt, 5, 5))
260*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_gt, 6, 7))
261*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_lt_or_eq, 11, 11))
262*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_lt_or_eq, 25, 15))
263*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_lt_or_eq, 9, 16))
264*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_lt, 1, 0))
265*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_lt, 8, 8))
266*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_lt, 10, 11))
267*9e94795aSAndroid Build Coastguard Worker
268*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_gt_or_eq, 2573904, 2573900))
269*9e94795aSAndroid Build Coastguard Worker$(call math-expect-true,(call math_gt_or_eq, 12345, 12345))
270*9e94795aSAndroid Build Coastguard Worker$(call math-expect-false,(call math_gt_or_eq, 56, 2780))
271*9e94795aSAndroid Build Coastguard Worker
272*9e94795aSAndroid Build Coastguard Worker# $1 is the variable name to increment
273*9e94795aSAndroid Build Coastguard Workerdefine inc_and_print
274*9e94795aSAndroid Build Coastguard Worker$(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
275*9e94795aSAndroid Build Coastguard Workerendef
276*9e94795aSAndroid Build Coastguard Worker
277*9e94795aSAndroid Build Coastguard Workerifdef RUN_MATH_TESTS
278*9e94795aSAndroid Build Coastguard Workera :=
279*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call inc_and_print,a),1)
280*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call inc_and_print,a),2)
281*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call inc_and_print,a),3)
282*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call inc_and_print,a),4)
283*9e94795aSAndroid Build Coastguard Workerendif
284*9e94795aSAndroid Build Coastguard Worker
285*9e94795aSAndroid Build Coastguard Worker# Returns the words in $2 that are numbers and are less than $1
286*9e94795aSAndroid Build Coastguard Workerdefine numbers_less_than
287*9e94795aSAndroid Build Coastguard Worker$(strip \
288*9e94795aSAndroid Build Coastguard Worker  $(foreach n,$2, \
289*9e94795aSAndroid Build Coastguard Worker    $(if $(call math_is_number,$(n)), \
290*9e94795aSAndroid Build Coastguard Worker      $(if $(call math_lt,$(n),$(1)), \
291*9e94795aSAndroid Build Coastguard Worker        $(n)))))
292*9e94795aSAndroid Build Coastguard Workerendef
293*9e94795aSAndroid Build Coastguard Worker
294*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,0,0 1 2 3),)
295*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,1,0 2 1 3),0)
296*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,2,0 2 1 3),0 1)
297*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,3,0 2 1 3),0 2 1)
298*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,4,0 2 1 3),0 2 1 3)
299*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,3,0 2 1 3 2),0 2 1 2)
300*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_less_than,100,0 1000 50 101 100),0 50)
301*9e94795aSAndroid Build Coastguard Worker
302*9e94795aSAndroid Build Coastguard Worker# Returns the words in $2 that are numbers and are greater or equal to $1
303*9e94795aSAndroid Build Coastguard Workerdefine numbers_greater_or_equal_to
304*9e94795aSAndroid Build Coastguard Worker$(strip \
305*9e94795aSAndroid Build Coastguard Worker  $(foreach n,$2, \
306*9e94795aSAndroid Build Coastguard Worker    $(if $(call math_is_number,$(n)), \
307*9e94795aSAndroid Build Coastguard Worker      $(if $(call math_gt_or_eq,$(n),$(1)), \
308*9e94795aSAndroid Build Coastguard Worker        $(n)))))
309*9e94795aSAndroid Build Coastguard Workerendef
310*9e94795aSAndroid Build Coastguard Worker
311*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,4,0 1 2 3),)
312*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,3,0 2 1 3),3)
313*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,2,0 2 1 3),2 3)
314*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,1,0 2 1 3),2 1 3)
315*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,0,0 2 1 3),0 2 1 3)
316*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call numbers_greater_or_equal_to,1,0 2 1 3 2),2 1 3 2)
317*9e94795aSAndroid Build Coastguard Worker
318*9e94795aSAndroid Build Coastguard Worker# 10,001 = 10 ** 4 + 1, contains 10,001 x's, so 1 more than 10,000 (future) API level
319*9e94795aSAndroid Build Coastguard Worker_INT_LIMIT_WORDS := x $(foreach a,0 1 2 3 4 5 6 7 8 9,$(foreach b,0 1 2 3 4 5 6 7 8 9,\
320*9e94795aSAndroid Build Coastguard Worker  $(foreach c,0 1 2 3 4 5 6 7 8 9,x x x x x x x x x x)))
321*9e94795aSAndroid Build Coastguard Worker
322*9e94795aSAndroid Build Coastguard Workerdefine _int_encode
323*9e94795aSAndroid Build Coastguard Worker$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
324*9e94795aSAndroid Build Coastguard Worker  $(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
325*9e94795aSAndroid Build Coastguard Worker    $(wordlist 1,$(1),$(_INT_LIMIT_WORDS)))
326*9e94795aSAndroid Build Coastguard Workerendef
327*9e94795aSAndroid Build Coastguard Worker
328*9e94795aSAndroid Build Coastguard Worker# _int_max returns the maximum of the two arguments
329*9e94795aSAndroid Build Coastguard Worker# input: two (x) lists; output: one (x) list
330*9e94795aSAndroid Build Coastguard Worker# integer cannot be passed in directly. It has to be converted using _int_encode.
331*9e94795aSAndroid Build Coastguard Workerdefine _int_max
332*9e94795aSAndroid Build Coastguard Worker$(subst xx,x,$(join $(1),$(2)))
333*9e94795aSAndroid Build Coastguard Workerendef
334*9e94795aSAndroid Build Coastguard Worker
335*9e94795aSAndroid Build Coastguard Worker# first argument is greater than second argument
336*9e94795aSAndroid Build Coastguard Worker# output: non-empty if true
337*9e94795aSAndroid Build Coastguard Worker# integer cannot be passed in directly. It has to be converted using _int_encode.
338*9e94795aSAndroid Build Coastguard Workerdefine _int_greater-than
339*9e94795aSAndroid Build Coastguard Worker$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2))))
340*9e94795aSAndroid Build Coastguard Workerendef
341*9e94795aSAndroid Build Coastguard Worker
342*9e94795aSAndroid Build Coastguard Worker# first argument equals to second argument
343*9e94795aSAndroid Build Coastguard Worker# output: non-empty if true
344*9e94795aSAndroid Build Coastguard Worker# integer cannot be passed in directly. It has to be converted using _int_encode.
345*9e94795aSAndroid Build Coastguard Workerdefine _int_equal
346*9e94795aSAndroid Build Coastguard Worker$(filter $(words $(1)),$(words $(2)))
347*9e94795aSAndroid Build Coastguard Workerendef
348*9e94795aSAndroid Build Coastguard Worker
349*9e94795aSAndroid Build Coastguard Worker# first argument is greater than or equal to second argument
350*9e94795aSAndroid Build Coastguard Worker# output: non-empty if true
351*9e94795aSAndroid Build Coastguard Worker# integer cannot be passed in directly. It has to be converted using _int_encode.
352*9e94795aSAndroid Build Coastguard Workerdefine _int_greater-or-equal
353*9e94795aSAndroid Build Coastguard Worker$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2))
354*9e94795aSAndroid Build Coastguard Workerendef
355*9e94795aSAndroid Build Coastguard Worker
356*9e94795aSAndroid Build Coastguard Workerdefine int_plus
357*9e94795aSAndroid Build Coastguard Worker$(words $(call _int_encode,$(1)) $(call _int_encode,$(2)))
358*9e94795aSAndroid Build Coastguard Workerendef
359*9e94795aSAndroid Build Coastguard Worker
360*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_plus,0,0),0)
361*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_plus,0,1),1)
362*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_plus,1,0),1)
363*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_plus,1,100),101)
364*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_plus,100,100),200)
365*9e94795aSAndroid Build Coastguard Worker
366*9e94795aSAndroid Build Coastguard Workerdefine int_subtract
367*9e94795aSAndroid Build Coastguard Worker$(strip \
368*9e94795aSAndroid Build Coastguard Worker  $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
369*9e94795aSAndroid Build Coastguard Worker  $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
370*9e94795aSAndroid Build Coastguard Worker    $(call math-error,subtract underflow $(1) - $(2))))
371*9e94795aSAndroid Build Coastguard Workerendef
372*9e94795aSAndroid Build Coastguard Worker
373*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_subtract,0,0),0)
374*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_subtract,1,0),1)
375*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_subtract,1,1),0)
376*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_subtract,100,1),99)
377*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_subtract,200,100),100)
378*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call int_subtract,0,1),subtract underflow 0 - 1)
379*9e94795aSAndroid Build Coastguard Worker
380*9e94795aSAndroid Build Coastguard Workerdefine int_multiply
381*9e94795aSAndroid Build Coastguard Worker$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
382*9e94795aSAndroid Build Coastguard Workerendef
383*9e94795aSAndroid Build Coastguard Worker
384*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,0,0),0)
385*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,1,0),0)
386*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,1,1),1)
387*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,100,1),100)
388*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,1,100),100)
389*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,4,100),400)
390*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_multiply,100,4),400)
391*9e94795aSAndroid Build Coastguard Worker
392*9e94795aSAndroid Build Coastguard Workerdefine int_divide
393*9e94795aSAndroid Build Coastguard Worker$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \
394*9e94795aSAndroid Build Coastguard Worker  $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
395*9e94795aSAndroid Build Coastguard Worker    $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0)))
396*9e94795aSAndroid Build Coastguard Workerendef
397*9e94795aSAndroid Build Coastguard Worker
398*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_divide,1,1),1)
399*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_divide,200,1),200)
400*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_divide,200,3),66)
401*9e94795aSAndroid Build Coastguard Worker$(call math-expect,(call int_divide,1,2),0)
402*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call int_divide,0,0),division by zero is not allowed!)
403*9e94795aSAndroid Build Coastguard Worker$(call math-expect-error,(call int_divide,1,0),division by zero is not allowed!)
404*9e94795aSAndroid Build Coastguard Worker
405*9e94795aSAndroid Build Coastguard Workerifdef RUN_MATH_TESTS
406*9e94795aSAndroid Build Coastguard Worker  ifdef MATH_TEST_FAILURE
407*9e94795aSAndroid Build Coastguard Worker    math-tests:
408*9e94795aSAndroid Build Coastguard Worker	@echo FAIL
409*9e94795aSAndroid Build Coastguard Worker	@false
410*9e94795aSAndroid Build Coastguard Worker  else
411*9e94795aSAndroid Build Coastguard Worker    math-tests:
412*9e94795aSAndroid Build Coastguard Worker	@echo PASS
413*9e94795aSAndroid Build Coastguard Worker  endif
414*9e94795aSAndroid Build Coastguard Worker  .PHONY: math-tests
415*9e94795aSAndroid Build Coastguard Workerendif
416