blob: ecee474d1471248786ad4c46401fb515154f736c [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)
Justin Yunb2b55642023-10-26 22:28:34 +090028__MATH_ONE_NUMBERS := 0 1 2 3 4 5 6 7 8 9
Steven Morelanda64f3362017-05-31 12:48:55 -070029
Colin Cross5bc27ea2018-05-09 13:28:06 -070030math-error = $(call pretty-error,$(1))
31math-expect :=
32math-expect-true :=
33math-expect :=
34math-expect-error :=
35
36# Run the math tests with:
Justin Yun66c7af82019-12-20 19:17:38 +090037# 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
Colin Cross5bc27ea2018-05-09 13:28:06 -070039ifdef RUN_MATH_TESTS
Justin Yunb2b55642023-10-26 22:28:34 +090040 ifndef empty
41 empty :=
42 space := $(empty) $(empty)
43 endif
Colin Cross5bc27ea2018-05-09 13:28:06 -070044 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
Colin Cross8b170352018-05-09 15:28:28 -070068# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing.
Justin Yunb2b55642023-10-26 22:28:34 +090069define math_is_number_in_100
Steven Morelanda64f3362017-05-31 12:48:55 -070070$(strip \
Colin Cross5bc27ea2018-05-09 13:28:06 -070071 $(if $(1),,$(call math-error,Argument missing)) \
72 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
Steven Morelanda64f3362017-05-31 12:48:55 -070073 $(if $(filter $(1),$(__MATH_NUMBERS)),true))
74endef
75
Justin Yunb2b55642023-10-26 22:28:34 +090076# 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
Colin Cross8b170352018-05-09 15:28:28 -070092define 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
Colin Cross5bc27ea2018-05-09 13:28:06 -070097
Colin Cross8b170352018-05-09 15:28:28 -070098$(call math-expect-true,(call math_is_number,0))
Colin Cross5bc27ea2018-05-09 13:28:06 -070099$(call math-expect-true,(call math_is_number,2))
Justin Yunb2b55642023-10-26 22:28:34 +0900100$(call math-expect-true,(call math_is_number,202412))
Colin Cross5bc27ea2018-05-09 13:28:06 -0700101$(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)
Steven Morelanda64f3362017-05-31 12:48:55 -0700105
Colin Cross8b170352018-05-09 15:28:28 -0700106$(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
Steven Morelanda64f3362017-05-31 12:48:55 -0700112define _math_check_valid
Justin Yunb2b55642023-10-26 22:28:34 +0900113$(if $(call math_is_number_in_100,$(1)),,$(call math-error,Only non-negative integers <= 100 are supported (not $(1))))
Steven Morelanda64f3362017-05-31 12:48:55 -0700114endef
115
Colin Cross8b170352018-05-09 15:28:28 -0700116$(call math-expect,(call _math_check_valid,0))
Colin Cross5bc27ea2018-05-09 13:28:06 -0700117$(call math-expect,(call _math_check_valid,1))
118$(call math-expect,(call _math_check_valid,100))
Colin Cross8b170352018-05-09 15:28:28 -0700119$(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))
Colin Cross5bc27ea2018-05-09 13:28:06 -0700121$(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
Nan Zhangad818dc2017-10-04 09:26:06 -0700124# return a list containing integers ranging from [$(1),$(2)]
125define int_range_list
Colin Cross8b170352018-05-09 15:28:28 -0700126$(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)))
Nan Zhangad818dc2017-10-04 09:26:06 -0700130endef
131
Colin Cross8b170352018-05-09 15:28:28 -0700132$(call math-expect,(call int_range_list,0,1),0 1)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700133$(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),)
Colin Cross8b170352018-05-09 15:28:28 -0700136$(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 -0700137
Justin Yunb2b55642023-10-26 22:28:34 +0900138# 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)
Steven Morelanda64f3362017-05-31 12:48:55 -0700197
198# Returns the greater of $1 or $2.
Justin Yunb2b55642023-10-26 22:28:34 +0900199# If $1 or $2 is not a positive integer, then an error is generated.
Steven Morelanda64f3362017-05-31 12:48:55 -0700200define math_max
Justin Yunb2b55642023-10-26 22:28:34 +0900201$(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))))
Steven Morelanda64f3362017-05-31 12:48:55 -0700205endef
206
Justin Yune81ec692021-11-09 23:09:52 +0900207# Returns the lesser of $1 or $2.
208define math_min
Justin Yunb2b55642023-10-26 22:28:34 +0900209$(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))))
Justin Yune81ec692021-11-09 23:09:52 +0900213endef
214
Colin Cross5bc27ea2018-05-09 13:28:06 -0700215$(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)
Justin Yune81ec692021-11-09 23:09:52 +0900218$(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 -0700219$(call math-expect,(call math_max,0,1),1)
220$(call math-expect,(call math_max,1,0),1)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700221$(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)
Justin Yune81ec692021-11-09 23:09:52 +0900224$(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)
Steven Morelanda64f3362017-05-31 12:48:55 -0700229
Justin Yunb2b55642023-10-26 22:28:34 +0900230$(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
Steven Morelanda64f3362017-05-31 12:48:55 -0700239define math_gt_or_eq
240$(if $(filter $(1),$(call math_max,$(1),$(2))),true)
241endef
242
Justin Yun66c7af82019-12-20 19:17:38 +0900243define math_gt
244$(if $(call math_gt_or_eq,$(2),$(1)),,true)
245endef
246
Justin Yunb2b55642023-10-26 22:28:34 +0900247define math_lt_or_eq
248$(if $(call math_gt_or_eq,$(2),$(1)),true)
249endef
250
Colin Crossf9602572017-10-12 13:34:40 -0700251define math_lt
252$(if $(call math_gt_or_eq,$(1),$(2)),,true)
253endef
254
Colin Cross5bc27ea2018-05-09 13:28:06 -0700255$(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))
Justin Yun66c7af82019-12-20 19:17:38 +0900258$(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))
Justin Yunb2b55642023-10-26 22:28:34 +0900261$(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))
Justin Yun66c7af82019-12-20 19:17:38 +0900264$(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))
Steven Morelanda64f3362017-05-31 12:48:55 -0700267
Justin Yunb2b55642023-10-26 22:28:34 +0900268$(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
Steven Morelanda64f3362017-05-31 12:48:55 -0700272# $1 is the variable name to increment
273define inc_and_print
274$(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
275endef
Nan Zhangad818dc2017-10-04 09:26:06 -0700276
Colin Cross5bc27ea2018-05-09 13:28:06 -0700277ifdef 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
Colin Crossf9602572017-10-12 13:34:40 -0700285# 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
Colin Cross8b170352018-05-09 15:28:28 -0700294$(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)
Justin Yunb2b55642023-10-26 22:28:34 +0900300$(call math-expect,(call numbers_less_than,100,0 1000 50 101 100),0 50)
Colin Cross5bc27ea2018-05-09 13:28:06 -0700301
yawanng522710f2020-10-27 01:48:41 +0000302# 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
Nan Zhangad818dc2017-10-04 09:26:06 -0700318_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)))),\
Colin Cross5bc27ea2018-05-09 13:28:06 -0700323 $(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
Nan Zhangad818dc2017-10-04 09:26:06 -0700324 $(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
Colin Cross5bc27ea2018-05-09 13:28:06 -0700359$(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
Nan Zhangad818dc2017-10-04 09:26:06 -0700365define int_subtract
Colin Cross5bc27ea2018-05-09 13:28:06 -0700366$(strip \
367 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
Nan Zhangad818dc2017-10-04 09:26:06 -0700368 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
Colin Cross5bc27ea2018-05-09 13:28:06 -0700369 $(call math-error,subtract underflow $(1) - $(2))))
Nan Zhangad818dc2017-10-04 09:26:06 -0700370endef
371
Colin Cross5bc27ea2018-05-09 13:28:06 -0700372$(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
Nan Zhangad818dc2017-10-04 09:26:06 -0700379define int_multiply
380$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
381endef
382
Colin Cross5bc27ea2018-05-09 13:28:06 -0700383$(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
Nan Zhangad818dc2017-10-04 09:26:06 -0700391define int_divide
Colin Cross5bc27ea2018-05-09 13:28:06 -0700392$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \
Nan Zhangad818dc2017-10-04 09:26:06 -0700393 $(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
Colin Cross5bc27ea2018-05-09 13:28:06 -0700396
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