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 29math-error = $(call pretty-error,$(1)) 30math-expect := 31math-expect-true := 32math-expect := 33math-expect-error := 34 35# Run the math tests with: 36# 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 38ifdef 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 63# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing. 64define math_is_number 65$(strip \ 66 $(if $(1),,$(call math-error,Argument missing)) \ 67 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \ 68 $(if $(filter $(1),$(__MATH_NUMBERS)),true)) 69endef 70 71define 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 76 77$(call math-expect-true,(call math_is_number,0)) 78$(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) 83 84$(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 90define _math_check_valid 91$(if $(call math_is_number,$(1)),,$(call math-error,Only non-negative integers <= 100 are supported (not $(1)))) 92endef 93 94$(call math-expect,(call _math_check_valid,0)) 95$(call math-expect,(call _math_check_valid,1)) 96$(call math-expect,(call _math_check_valid,100)) 97$(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)) 99$(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 102# return a list containing integers ranging from [$(1),$(2)] 103define int_range_list 104$(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))) 108endef 109 110$(call math-expect,(call int_range_list,0,1),0 1) 111$(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),) 114$(call math-expect-error,(call int_range_list,1,101),Only non-negative integers <= 100 are supported (not 101)) 115 116 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 124$(call math-expect-error,(call math_max),Argument missing) 125$(call math-expect-error,(call math_max,1),Argument missing) 126$(call math-expect-error,(call math_max,1 2,3),Multiple words in a single argument: 1 2) 127$(call math-expect,(call math_max,0,1),1) 128$(call math-expect,(call math_max,1,0),1) 129$(call math-expect,(call math_max,1,1),1) 130$(call math-expect,(call math_max,5,42),42) 131$(call math-expect,(call math_max,42,5),42) 132 133define math_gt_or_eq 134$(if $(filter $(1),$(call math_max,$(1),$(2))),true) 135endef 136 137define math_gt 138$(if $(call math_gt_or_eq,$(2),$(1)),,true) 139endef 140 141define math_lt 142$(if $(call math_gt_or_eq,$(1),$(2)),,true) 143endef 144 145$(call math-expect-true,(call math_gt_or_eq, 2, 1)) 146$(call math-expect-true,(call math_gt_or_eq, 1, 1)) 147$(call math-expect-false,(call math_gt_or_eq, 1, 2)) 148$(call math-expect-true,(call math_gt, 4, 3)) 149$(call math-expect-false,(call math_gt, 5, 5)) 150$(call math-expect-false,(call math_gt, 6, 7)) 151$(call math-expect-false,(call math_lt, 1, 0)) 152$(call math-expect-false,(call math_lt, 8, 8)) 153$(call math-expect-true,(call math_lt, 10, 11)) 154 155# $1 is the variable name to increment 156define inc_and_print 157$(strip $(eval $(1) := $($(1)) .)$(words $($(1)))) 158endef 159 160ifdef RUN_MATH_TESTS 161a := 162$(call math-expect,(call inc_and_print,a),1) 163$(call math-expect,(call inc_and_print,a),2) 164$(call math-expect,(call inc_and_print,a),3) 165$(call math-expect,(call inc_and_print,a),4) 166endif 167 168# Returns the words in $2 that are numbers and are less than $1 169define numbers_less_than 170$(strip \ 171 $(foreach n,$2, \ 172 $(if $(call math_is_number,$(n)), \ 173 $(if $(call math_lt,$(n),$(1)), \ 174 $(n))))) 175endef 176 177$(call math-expect,(call numbers_less_than,0,0 1 2 3),) 178$(call math-expect,(call numbers_less_than,1,0 2 1 3),0) 179$(call math-expect,(call numbers_less_than,2,0 2 1 3),0 1) 180$(call math-expect,(call numbers_less_than,3,0 2 1 3),0 2 1) 181$(call math-expect,(call numbers_less_than,4,0 2 1 3),0 2 1 3) 182$(call math-expect,(call numbers_less_than,3,0 2 1 3 2),0 2 1 2) 183 184_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,\ 185 $(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))) 186 187define _int_encode 188$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\ 189 $(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\ 190 $(wordlist 1,$(1),$(_INT_LIMIT_WORDS))) 191endef 192 193# _int_max returns the maximum of the two arguments 194# input: two (x) lists; output: one (x) list 195# integer cannot be passed in directly. It has to be converted using _int_encode. 196define _int_max 197$(subst xx,x,$(join $(1),$(2))) 198endef 199 200# first argument is greater than second argument 201# output: non-empty if true 202# integer cannot be passed in directly. It has to be converted using _int_encode. 203define _int_greater-than 204$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2)))) 205endef 206 207# first argument equals to second argument 208# output: non-empty if true 209# integer cannot be passed in directly. It has to be converted using _int_encode. 210define _int_equal 211$(filter $(words $(1)),$(words $(2))) 212endef 213 214# first argument is greater than or equal to second argument 215# output: non-empty if true 216# integer cannot be passed in directly. It has to be converted using _int_encode. 217define _int_greater-or-equal 218$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2)) 219endef 220 221define int_plus 222$(words $(call _int_encode,$(1)) $(call _int_encode,$(2))) 223endef 224 225$(call math-expect,(call int_plus,0,0),0) 226$(call math-expect,(call int_plus,0,1),1) 227$(call math-expect,(call int_plus,1,0),1) 228$(call math-expect,(call int_plus,1,100),101) 229$(call math-expect,(call int_plus,100,100),200) 230 231define int_subtract 232$(strip \ 233 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\ 234 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\ 235 $(call math-error,subtract underflow $(1) - $(2)))) 236endef 237 238$(call math-expect,(call int_subtract,0,0),0) 239$(call math-expect,(call int_subtract,1,0),1) 240$(call math-expect,(call int_subtract,1,1),0) 241$(call math-expect,(call int_subtract,100,1),99) 242$(call math-expect,(call int_subtract,200,100),100) 243$(call math-expect-error,(call int_subtract,0,1),subtract underflow 0 - 1) 244 245define int_multiply 246$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2)))) 247endef 248 249$(call math-expect,(call int_multiply,0,0),0) 250$(call math-expect,(call int_multiply,1,0),0) 251$(call math-expect,(call int_multiply,1,1),1) 252$(call math-expect,(call int_multiply,100,1),100) 253$(call math-expect,(call int_multiply,1,100),100) 254$(call math-expect,(call int_multiply,4,100),400) 255$(call math-expect,(call int_multiply,100,4),400) 256 257define int_divide 258$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \ 259 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \ 260 $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0))) 261endef 262 263$(call math-expect,(call int_divide,1,1),1) 264$(call math-expect,(call int_divide,200,1),200) 265$(call math-expect,(call int_divide,200,3),66) 266$(call math-expect,(call int_divide,1,2),0) 267$(call math-expect-error,(call int_divide,0,0),division by zero is not allowed!) 268$(call math-expect-error,(call int_divide,1,0),division by zero is not allowed!) 269 270ifdef RUN_MATH_TESTS 271 ifdef MATH_TEST_FAILURE 272 math-tests: 273 @echo FAIL 274 @false 275 else 276 math-tests: 277 @echo PASS 278 endif 279 .PHONY: math-tests 280endif 281