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