blob: 0271ea8b03e41772734a630914edbaf6663d3f80 [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###########################################################
Colin Cross8b170352018-05-09 15:28:28 -070018# Basic math functions for non-negative integers <= 100
Steven Morelanda64f3362017-05-31 12:48:55 -070019#
20# (SDK versions for example)
21###########################################################
Colin Cross8b170352018-05-09 15:28:28 -070022__MATH_POS_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__MATH_NUMBERS := 0 $(__MATH_POS_NUMBERS)
Steven Morelanda64f3362017-05-31 12:48:55 -070028
Colin Cross5bc27ea2018-05-09 13:28:06 -070029math-error = $(call pretty-error,$(1))
30math-expect :=
31math-expect-true :=
32math-expect :=
33math-expect-error :=
34
35# Run the math tests with:
Justin Yun66c7af82019-12-20 19:17:38 +090036# make -f ${ANDROID_BUILD_TOP}/build/make/common/math.mk RUN_MATH_TESTS=true
37# $(get_build_var CKATI) -f ${ANDROID_BUILD_TOP}//build/make/common/math.mk RUN_MATH_TESTS=true
Colin Cross5bc27ea2018-05-09 13:28:06 -070038ifdef RUN_MATH_TESTS
39 MATH_TEST_FAILURE :=
40 MATH_TEST_ERROR :=
41 math-error = $(if $(MATH_TEST_ERROR),,$(eval MATH_TEST_ERROR:=$(1)))
42 define math-expect
43 $(eval got:=$$$1) \
44 $(if $(subst $(got),,$(2))$(subst $(2),,$(got))$(MATH_TEST_ERROR), \
45 $(if $(MATH_TEST_ERROR),$(warning $(MATH_TEST_ERROR)),$(warning $$$1 '$(got)' != '$(2)')) \
46 $(eval MATH_TEST_FAILURE := true)) \
47 $(eval MATH_TEST_ERROR :=) \
48 $(eval got:=)
49 endef
50 math-expect-true = $(call math-expect,$(1),true)
51 math-expect-false = $(call math-expect,$(1),)
52
53 define math-expect-error
54 $(eval got:=$$$1) \
55 $(if $(subst $(MATH_TEST_ERROR),,$(2))$(subst $(2),,$(MATH_TEST_ERROR)), \
56 $(warning '$(MATH_TEST_ERROR)' != '$(2)') \
57 $(eval MATH_TEST_FAILURE := true)) \
58 $(eval MATH_TEST_ERROR :=) \
59 $(eval got:=)
60 endef
61endif
62
Colin Cross8b170352018-05-09 15:28:28 -070063# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing.
Steven Morelanda64f3362017-05-31 12:48:55 -070064define math_is_number
65$(strip \
Colin Cross5bc27ea2018-05-09 13:28:06 -070066 $(if $(1),,$(call math-error,Argument missing)) \
67 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
Steven Morelanda64f3362017-05-31 12:48:55 -070068 $(if $(filter $(1),$(__MATH_NUMBERS)),true))
69endef
70
Colin Cross8b170352018-05-09 15:28:28 -070071define math_is_zero
72$(strip \
73 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
74 $(if $(filter 0,$(1)),true))
75endef
Colin Cross5bc27ea2018-05-09 13:28:06 -070076
Colin Cross8b170352018-05-09 15:28:28 -070077$(call math-expect-true,(call math_is_number,0))
Colin Cross5bc27ea2018-05-09 13:28:06 -070078$(call math-expect-true,(call math_is_number,2))
79$(call math-expect-false,(call math_is_number,foo))
80$(call math-expect-false,(call math_is_number,-1))
81$(call math-expect-error,(call math_is_number,1 2),Multiple words in a single argument: 1 2)
82$(call math-expect-error,(call math_is_number,no 2),Multiple words in a single argument: no 2)
Steven Morelanda64f3362017-05-31 12:48:55 -070083
Colin Cross8b170352018-05-09 15:28:28 -070084$(call math-expect-true,(call math_is_zero,0))
85$(call math-expect-false,(call math_is_zero,1))
86$(call math-expect-false,(call math_is_zero,foo))
87$(call math-expect-error,(call math_is_zero,1 2),Multiple words in a single argument: 1 2)
88$(call math-expect-error,(call math_is_zero,no 2),Multiple words in a single argument: no 2)
89
Steven Morelanda64f3362017-05-31 12:48:55 -070090define _math_check_valid
Colin Cross8b170352018-05-09 15:28:28 -070091$(if $(call math_is_number,$(1)),,$(call math-error,Only non-negative integers <= 100 are supported (not $(1))))
Steven Morelanda64f3362017-05-31 12:48:55 -070092endef
93
Colin Cross8b170352018-05-09 15:28:28 -070094$(call math-expect,(call _math_check_valid,0))
Colin Cross5bc27ea2018-05-09 13:28:06 -070095$(call math-expect,(call _math_check_valid,1))
96$(call math-expect,(call _math_check_valid,100))
Colin Cross8b170352018-05-09 15:28:28 -070097$(call math-expect-error,(call _math_check_valid,-1),Only non-negative integers <= 100 are supported (not -1))
98$(call math-expect-error,(call _math_check_valid,101),Only non-negative integers <= 100 are supported (not 101))
Colin Cross5bc27ea2018-05-09 13:28:06 -070099$(call math-expect-error,(call _math_check_valid,),Argument missing)
100$(call math-expect-error,(call _math_check_valid,1 2),Multiple words in a single argument: 1 2)
101
Nan Zhangad818dc2017-10-04 09:26:06 -0700102# return a list containing integers ranging from [$(1),$(2)]
103define int_range_list
Colin Cross8b170352018-05-09 15:28:28 -0700104$(strip \
105 $(call _math_check_valid,$(1))$(call _math_check_valid,$(2)) \
106 $(if $(call math_is_zero,$(1)),0)\
107 $(wordlist $(if $(call math_is_zero,$(1)),1,$(1)),$(2),$(__MATH_POS_NUMBERS)))
Nan Zhangad818dc2017-10-04 09:26:06 -0700108endef
109
Colin Cross8b170352018-05-09 15:28:28 -0700110$(call math-expect,(call int_range_list,0,1),0 1)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700111$(call math-expect,(call int_range_list,1,1),1)
112$(call math-expect,(call int_range_list,1,2),1 2)
113$(call math-expect,(call int_range_list,2,1),)
Colin Cross8b170352018-05-09 15:28:28 -0700114$(call math-expect-error,(call int_range_list,1,101),Only non-negative integers <= 100 are supported (not 101))
Colin Cross5bc27ea2018-05-09 13:28:06 -0700115
Steven Morelanda64f3362017-05-31 12:48:55 -0700116
117# Returns the greater of $1 or $2.
118# If $1 or $2 is not a positive integer <= 100, then an error is generated.
119define math_max
120$(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \
121 $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS))))
122endef
123
Justin Yune81ec692021-11-09 23:09:52 +0900124# Returns the lesser of $1 or $2.
125define math_min
126$(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \
127 $(firstword $(filter $(1) $(2),$(__MATH_NUMBERS))))
128endef
129
Colin Cross5bc27ea2018-05-09 13:28:06 -0700130$(call math-expect-error,(call math_max),Argument missing)
131$(call math-expect-error,(call math_max,1),Argument missing)
132$(call math-expect-error,(call math_max,1 2,3),Multiple words in a single argument: 1 2)
Justin Yune81ec692021-11-09 23:09:52 +0900133$(call math-expect-error,(call math_min,1,2 3),Multiple words in a single argument: 2 3)
Colin Cross8b170352018-05-09 15:28:28 -0700134$(call math-expect,(call math_max,0,1),1)
135$(call math-expect,(call math_max,1,0),1)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700136$(call math-expect,(call math_max,1,1),1)
137$(call math-expect,(call math_max,5,42),42)
138$(call math-expect,(call math_max,42,5),42)
Justin Yune81ec692021-11-09 23:09:52 +0900139$(call math-expect,(call math_min,0,1),0)
140$(call math-expect,(call math_min,1,0),0)
141$(call math-expect,(call math_min,1,1),1)
142$(call math-expect,(call math_min,7,32),7)
143$(call math-expect,(call math_min,32,7),7)
Steven Morelanda64f3362017-05-31 12:48:55 -0700144
145define math_gt_or_eq
146$(if $(filter $(1),$(call math_max,$(1),$(2))),true)
147endef
148
Justin Yun66c7af82019-12-20 19:17:38 +0900149define math_gt
150$(if $(call math_gt_or_eq,$(2),$(1)),,true)
151endef
152
Colin Crossf9602572017-10-12 13:34:40 -0700153define math_lt
154$(if $(call math_gt_or_eq,$(1),$(2)),,true)
155endef
156
Colin Cross5bc27ea2018-05-09 13:28:06 -0700157$(call math-expect-true,(call math_gt_or_eq, 2, 1))
158$(call math-expect-true,(call math_gt_or_eq, 1, 1))
159$(call math-expect-false,(call math_gt_or_eq, 1, 2))
Justin Yun66c7af82019-12-20 19:17:38 +0900160$(call math-expect-true,(call math_gt, 4, 3))
161$(call math-expect-false,(call math_gt, 5, 5))
162$(call math-expect-false,(call math_gt, 6, 7))
163$(call math-expect-false,(call math_lt, 1, 0))
164$(call math-expect-false,(call math_lt, 8, 8))
165$(call math-expect-true,(call math_lt, 10, 11))
Steven Morelanda64f3362017-05-31 12:48:55 -0700166
167# $1 is the variable name to increment
168define inc_and_print
169$(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
170endef
Nan Zhangad818dc2017-10-04 09:26:06 -0700171
Colin Cross5bc27ea2018-05-09 13:28:06 -0700172ifdef RUN_MATH_TESTS
173a :=
174$(call math-expect,(call inc_and_print,a),1)
175$(call math-expect,(call inc_and_print,a),2)
176$(call math-expect,(call inc_and_print,a),3)
177$(call math-expect,(call inc_and_print,a),4)
178endif
179
Colin Crossf9602572017-10-12 13:34:40 -0700180# Returns the words in $2 that are numbers and are less than $1
181define numbers_less_than
182$(strip \
183 $(foreach n,$2, \
184 $(if $(call math_is_number,$(n)), \
185 $(if $(call math_lt,$(n),$(1)), \
186 $(n)))))
187endef
188
Colin Cross8b170352018-05-09 15:28:28 -0700189$(call math-expect,(call numbers_less_than,0,0 1 2 3),)
190$(call math-expect,(call numbers_less_than,1,0 2 1 3),0)
191$(call math-expect,(call numbers_less_than,2,0 2 1 3),0 1)
192$(call math-expect,(call numbers_less_than,3,0 2 1 3),0 2 1)
193$(call math-expect,(call numbers_less_than,4,0 2 1 3),0 2 1 3)
194$(call math-expect,(call numbers_less_than,3,0 2 1 3 2),0 2 1 2)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700195
yawanng522710f2020-10-27 01:48:41 +0000196# Returns the words in $2 that are numbers and are greater or equal to $1
197define numbers_greater_or_equal_to
198$(strip \
199 $(foreach n,$2, \
200 $(if $(call math_is_number,$(n)), \
201 $(if $(call math_gt_or_eq,$(n),$(1)), \
202 $(n)))))
203endef
204
205$(call math-expect,(call numbers_greater_or_equal_to,4,0 1 2 3),)
206$(call math-expect,(call numbers_greater_or_equal_to,3,0 2 1 3),3)
207$(call math-expect,(call numbers_greater_or_equal_to,2,0 2 1 3),2 3)
208$(call math-expect,(call numbers_greater_or_equal_to,1,0 2 1 3),2 1 3)
209$(call math-expect,(call numbers_greater_or_equal_to,0,0 2 1 3),0 2 1 3)
210$(call math-expect,(call numbers_greater_or_equal_to,1,0 2 1 3 2),2 1 3 2)
211
Nan Zhangad818dc2017-10-04 09:26:06 -0700212_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,\
213 $(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)))
214
215define _int_encode
216$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
Colin Cross5bc27ea2018-05-09 13:28:06 -0700217 $(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
Nan Zhangad818dc2017-10-04 09:26:06 -0700218 $(wordlist 1,$(1),$(_INT_LIMIT_WORDS)))
219endef
220
221# _int_max returns the maximum of the two arguments
222# input: two (x) lists; output: one (x) list
223# integer cannot be passed in directly. It has to be converted using _int_encode.
224define _int_max
225$(subst xx,x,$(join $(1),$(2)))
226endef
227
228# first argument is greater than second argument
229# output: non-empty if true
230# integer cannot be passed in directly. It has to be converted using _int_encode.
231define _int_greater-than
232$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2))))
233endef
234
235# first argument equals to second argument
236# output: non-empty if true
237# integer cannot be passed in directly. It has to be converted using _int_encode.
238define _int_equal
239$(filter $(words $(1)),$(words $(2)))
240endef
241
242# first argument is greater than or equal to second argument
243# output: non-empty if true
244# integer cannot be passed in directly. It has to be converted using _int_encode.
245define _int_greater-or-equal
246$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2))
247endef
248
249define int_plus
250$(words $(call _int_encode,$(1)) $(call _int_encode,$(2)))
251endef
252
Colin Cross5bc27ea2018-05-09 13:28:06 -0700253$(call math-expect,(call int_plus,0,0),0)
254$(call math-expect,(call int_plus,0,1),1)
255$(call math-expect,(call int_plus,1,0),1)
256$(call math-expect,(call int_plus,1,100),101)
257$(call math-expect,(call int_plus,100,100),200)
258
Nan Zhangad818dc2017-10-04 09:26:06 -0700259define int_subtract
Colin Cross5bc27ea2018-05-09 13:28:06 -0700260$(strip \
261 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
Nan Zhangad818dc2017-10-04 09:26:06 -0700262 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
Colin Cross5bc27ea2018-05-09 13:28:06 -0700263 $(call math-error,subtract underflow $(1) - $(2))))
Nan Zhangad818dc2017-10-04 09:26:06 -0700264endef
265
Colin Cross5bc27ea2018-05-09 13:28:06 -0700266$(call math-expect,(call int_subtract,0,0),0)
267$(call math-expect,(call int_subtract,1,0),1)
268$(call math-expect,(call int_subtract,1,1),0)
269$(call math-expect,(call int_subtract,100,1),99)
270$(call math-expect,(call int_subtract,200,100),100)
271$(call math-expect-error,(call int_subtract,0,1),subtract underflow 0 - 1)
272
Nan Zhangad818dc2017-10-04 09:26:06 -0700273define int_multiply
274$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
275endef
276
Colin Cross5bc27ea2018-05-09 13:28:06 -0700277$(call math-expect,(call int_multiply,0,0),0)
278$(call math-expect,(call int_multiply,1,0),0)
279$(call math-expect,(call int_multiply,1,1),1)
280$(call math-expect,(call int_multiply,100,1),100)
281$(call math-expect,(call int_multiply,1,100),100)
282$(call math-expect,(call int_multiply,4,100),400)
283$(call math-expect,(call int_multiply,100,4),400)
284
Nan Zhangad818dc2017-10-04 09:26:06 -0700285define int_divide
Colin Cross5bc27ea2018-05-09 13:28:06 -0700286$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \
Nan Zhangad818dc2017-10-04 09:26:06 -0700287 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
288 $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0)))
289endef
Colin Cross5bc27ea2018-05-09 13:28:06 -0700290
291$(call math-expect,(call int_divide,1,1),1)
292$(call math-expect,(call int_divide,200,1),200)
293$(call math-expect,(call int_divide,200,3),66)
294$(call math-expect,(call int_divide,1,2),0)
295$(call math-expect-error,(call int_divide,0,0),division by zero is not allowed!)
296$(call math-expect-error,(call int_divide,1,0),division by zero is not allowed!)
297
298ifdef RUN_MATH_TESTS
299 ifdef MATH_TEST_FAILURE
300 math-tests:
301 @echo FAIL
302 @false
303 else
304 math-tests:
305 @echo PASS
306 endif
307 .PHONY: math-tests
308endif