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 ; 25HAIKU_DIGIT_ADD_7 = $(HAIKU_PAD_9) 7 8 9 0 1 2 3 4 5 6 ; 26HAIKU_DIGIT_ADD_8 = $(HAIKU_PAD_9) 8 9 0 1 2 3 4 5 6 7 ; 27HAIKU_DIGIT_ADD_9 = $(HAIKU_PAD_9) 9 0 1 2 3 4 5 6 7 8 ; 28 29# pragma mark - Number Operations 30 31rule AddNumAbs a : b 32{ 33 # AddNum <a> : <b> ; 34 35 local result ; 36 local carry ; 37 while $(a) || $(b) || $(carry) { 38 # chop off the first digit 39 local da = $(a[1]:E=0) ; 40 local db = $(b[1]:E=0) ; 41 a = $(a[2-]) ; 42 b = $(b[2-]) ; 43 44 # add carry to the first digit 45 if $(carry) { 46 local daa = $(HAIKU_DIGIT_ADD_1[1$(da)]) ; 47 carry = $(HAIKU_DIGIT_GREATER_$(da)[1$(daa)]) ; 48 da = $(daa) ; 49 } 50 51 # add digits 52 local dr = $(HAIKU_DIGIT_ADD_$(da)[1$(db)]) ; 53 if $(HAIKU_DIGIT_GREATER_$(da)[1$(dr)]) { 54 carry = 1 ; 55 } 56 57 result += $(dr) ; 58 } 59 60 return $(result) ; 61} 62 63