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 positive integers <= 100 19# 20# (SDK versions for example) 21########################################################### 22__MATH_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 28# Returns true if $(1) is a positive integer <= 100, otherwise returns nothing. 29define math_is_number 30$(strip \ 31 $(if $(1),,$(error Argument missing)) \ 32 $(if $(word 2,$(1)),$(error Multiple words in a single argument: $(1))) \ 33 $(if $(filter $(1),$(__MATH_NUMBERS)),true)) 34endef 35 36#$(warning true == $(call math_is_number,2)) 37#$(warning == $(call math_is_number,foo)) 38#$(call math_is_number,1 2) 39#$(call math_is_number,no 2) 40 41define _math_check_valid 42$(if $(call math_is_number,$(1)),,$(error Only positive integers <= 100 are supported (not $(1)))) 43endef 44 45# return a list containing integers ranging from [$(1),$(2)] 46define int_range_list 47$(call _math_check_valid,$(1))$(call _math_check_valid,$(2))$(wordlist $(1),$(2),$(__MATH_NUMBERS)) 48endef 49 50#$(call _math_check_valid,0) 51#$(call _math_check_valid,1) 52#$(call _math_check_valid,100) 53#$(call _math_check_valid,101) 54#$(call _math_check_valid,) 55#$(call _math_check_valid,1 2) 56 57# Returns the greater of $1 or $2. 58# If $1 or $2 is not a positive integer <= 100, then an error is generated. 59define math_max 60$(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \ 61 $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS)))) 62endef 63 64#$(call math_max) 65#$(call math_max,1) 66#$(call math_max,1 2,3) 67#$(warning 1 == $(call math_max,1,1)) 68#$(warning 42 == $(call math_max,5,42)) 69#$(warning 42 == $(call math_max,42,5)) 70 71define math_gt_or_eq 72$(if $(filter $(1),$(call math_max,$(1),$(2))),true) 73endef 74 75define math_lt 76$(if $(call math_gt_or_eq,$(1),$(2)),,true) 77endef 78 79#$(warning $(call math_gt_or_eq, 2, 1)) 80#$(warning $(call math_gt_or_eq, 1, 1)) 81#$(warning $(if $(call math_gt_or_eq, 1, 2),false,true)) 82 83# $1 is the variable name to increment 84define inc_and_print 85$(strip $(eval $(1) := $($(1)) .)$(words $($(1)))) 86endef 87 88# Returns the words in $2 that are numbers and are less than $1 89define numbers_less_than 90$(strip \ 91 $(foreach n,$2, \ 92 $(if $(call math_is_number,$(n)), \ 93 $(if $(call math_lt,$(n),$(1)), \ 94 $(n))))) 95endef 96 97_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,\ 98 $(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))) 99 100define _int_encode 101$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\ 102 $(call pretty-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\ 103 $(wordlist 1,$(1),$(_INT_LIMIT_WORDS))) 104endef 105 106# _int_max returns the maximum of the two arguments 107# input: two (x) lists; output: one (x) list 108# integer cannot be passed in directly. It has to be converted using _int_encode. 109define _int_max 110$(subst xx,x,$(join $(1),$(2))) 111endef 112 113# first argument is greater than second argument 114# output: non-empty if true 115# integer cannot be passed in directly. It has to be converted using _int_encode. 116define _int_greater-than 117$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2)))) 118endef 119 120# first argument equals to second argument 121# output: non-empty if true 122# integer cannot be passed in directly. It has to be converted using _int_encode. 123define _int_equal 124$(filter $(words $(1)),$(words $(2))) 125endef 126 127# first argument is greater than or equal to second argument 128# output: non-empty if true 129# integer cannot be passed in directly. It has to be converted using _int_encode. 130define _int_greater-or-equal 131$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2)) 132endef 133 134define int_plus 135$(words $(call _int_encode,$(1)) $(call _int_encode,$(2))) 136endef 137 138define int_subtract 139$(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\ 140 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\ 141 $(call pretty-error,$(1) subtract underflow $(2))) 142endef 143 144define int_multiply 145$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2)))) 146endef 147 148define int_divide 149$(if $(filter 0,$(2)),$(call pretty-error,division by zero is not allowed!),$(strip \ 150 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \ 151 $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0))) 152endef 153