1# This is part of `prelude.mk`, split out for organizational purposes.
2# !!! NOTHING EAGER IS ALLOWED TO HAPPEN IN THIS FILE !!!
3
4#
5# Unsigned integer support
6
7_uint.is-uint-or-empty = $(if $1,$(call _uint.is-uint,$1),$(TRUE))
8_uint.is-uint = $(and $(call str.eq,$(words $1),1),$(filter 0% 1% 2% 3% 4% 5% 6% 7% 8% 9%,$1),$(call _uint.is-uint-or-empty,$(strip \
9 $(patsubst 0%,%,\
10 $(patsubst 1%,%,\
11 $(patsubst 2%,%,\
12 $(patsubst 3%,%,\
13 $(patsubst 4%,%,\
14 $(patsubst 5%,%,\
15 $(patsubst 6%,%,\
16 $(patsubst 7%,%,\
17 $(patsubst 8%,%,\
18 $(patsubst 9%,%,\
19 $1)))))))))))))
20
21# Usage: $(call _uint.normalize,UINT)
22# Example: $(call _uint.normalize,0) => 0
23# Example: $(call _uint.normalize,004) => 4
24#
25# Normalizes a decimal-uint-string. Right now, that just means
26# that it $(strip)s it, and trims leading 0s.
27_uint.normalize = $(strip \
28 $(if $(call _uint.is-uint,$1),,$(error argument to uint.* function is not a uint: '$1'))\
29 $(if $(filter 0%,$(filter-out 0,$1)),\
30 $(call _uint.normalize,$(patsubst 0%,%,$1)),\
31 $1))
32
33# Usage: $(call uint.max,UINT,UINT)
34# Example: $(call uint.max,3,2) => 3
35uint.max = $(call _uintx.to.uint,$(call _uintx.max,$(call _uintx.from.uint,$1),$(call _uintx.from.uint,$2)))
36
37# Usage: $(call uint.min,UINT,UINT)
38# Example: $(call uint.min,3,2) => 2
39uint.min = $(call _uintx.to.uint,$(call _uintx.min,$(call _uintx.from.uint,$1),$(call _uintx.from.uint,$2)))
40
41# Usage: $(call _uint.eq,UINT,UINT)
42uint.eq = $(call str.eq,$(call _uint.normalize,$1),$(call _uint.normalize,$2))
43
44# These opperate entirely in terms of functions that call
45# _uint.normalize, so they don't need to.
46uint.ge = $(call uint.eq,$(call uint.max,$1,$2),$1)
47uint.le = $(call uint.eq,$(call uint.min,$1,$2),$1)
48uint.gt = $(and $(call uint.ge,$1,$2),$(call not,$(call uint.eq,$1,$2)))
49uint.lt = $(and $(call uint.le,$1,$2),$(call not,$(call uint.eq,$1,$2)))
50
51#
52# "uintx" support: Unsigned integers represented as a list of "x"s
53#
54# Several operations are easier with this representation than with a
55# decimal-string representation.
56
57# Usage: $(call _uintx.to.uint,UINTX)
58# Example: $(call _uintx.to.uint,x x x x) => 4
59_uintx.to.uint = $(words $1)
60
61# Usage: $(call _uintx.from.uint.helper,UINT,PARTIALL_UINTX)
62# Example: $(call _uintx.from.uint.helper,3,) =>
63# $(call _uintx.from.uint.helper,3,x) =>
64# $(call _uintx.from.uint.helper,3,x x) =>
65# $(call _uintx.from.uint.helper,3,x x x) =>
66# x x x
67_uintx.from.uint.helper = $(if $(call str.eq,$1,$(call _uintx.to.uint,$2)), \
68 $2, \
69 $(call _uintx.from.uint.helper,$1,$2 x))
70
71# Usage: $(call _uintx.from.uint,UINT)
72# Example: $(call _uintx.from.uint,4) => x x x x
73_uintx.from.uint = $(foreach x,$(call _uintx.from.uint.helper,$(call _uint.normalize,$1),),x)
74
75# Usage: $(call _uintx.max,UINTX,UINTX)
76# Example: $(call _uintx.max,x x x,x x) => x x x
77_uintx.max = $(subst xx,x,$(join $1,$2))
78
79# Usage: $(call _uintx.min,UINTX,UINTX)
80# Example: $(call _uintx.min,x x x,x x) => x x
81_uintx.min = $(subst xx,x,$(filter xx,$(join $1,$2)))
View as plain text