# bend-ndarray's element functions: one cell (32 bits) at a time, by operation and dtype. float32 cells hold their # IEEE bits; int32 two's complement; uint32 and uint8 their value (uint8 below 256); bool 0 or 1. Integer arithmetic # wraps around (uint8 modulo 256), as NumPy's does. import Base import ./shape.bend as S def fb(x: U32) -> F32: match x: case U32{w}: F32{w} def bf(x: F32) -> U32: match x: case F32{w}: U32{w} def b2u(b: Bool) -> U32: Bool.to_u32(b) def neg(+x: U32) -> U32: (0 - x : U32) def is_neg(+x: U32) -> Bool: U32.is_ge(x, 2147483648) def u8(+x: U32) -> U32: U32.and(x, 255) # signed order on int32 bits def slt(+x: U32, +y: U32) -> Bool: U32.is_lt(U32.xor(x, 2147483648), U32.xor(y, 2147483648)) def fnan(+x: U32) -> Bool: Bool.not(F32.is_eq(fb(x), fb(x))) # ---- binary operations ---- type B2 is Data: BAdd{} BSub{} BMul{} BDiv{} BFloorDiv{} BRem{} BMin{} BMax{} BPow{} BEq{} BNe{} BLt{} BLe{} BGt{} BGe{} BAnd{} BOr{} BXor{} def op_name(op: B2) -> String: match op: case BAdd{}: "add" case BSub{}: "subtract" case BMul{}: "multiply" case BDiv{}: "divide" case BFloorDiv{}: "floor_divide" case BRem{}: "remainder" case BMin{}: "minimum" case BMax{}: "maximum" case BPow{}: "power" case BEq{}: "equal" case BNe{}: "not_equal" case BLt{}: "less" case BLe{}: "less_equal" case BGt{}: "greater" case BGe{}: "greater_equal" case BAnd{}: "logical_and" case BOr{}: "logical_or" case BXor{}: "logical_xor" def is_f32(dt: S.DType) -> Bool: match dt: case S.DF32{}: True{} case _: False{} def is_bool(dt: S.DType) -> Bool: match dt: case S.DBool{}: True{} case _: False{} # which dtypes an operation takes: arithmetic on numbers (true division and power on float32 only), comparisons on # every dtype, logic on bool def supports(op: B2, +dt: S.DType) -> Bool: match op: case BDiv{}: is_f32(dt) case BPow{}: is_f32(dt) case BAnd{}: is_bool(dt) case BOr{}: is_bool(dt) case BXor{}: is_bool(dt) case BEq{}: True{} case BNe{}: True{} case BLt{}: True{} case BLe{}: True{} case BGt{}: True{} case BGe{}: True{} case _: Bool.not(is_bool(dt)) def is_cmp(op: B2) -> Bool: match op: case BEq{}: True{} case BNe{}: True{} case BLt{}: True{} case BLe{}: True{} case BGt{}: True{} case BGe{}: True{} case _: False{} def is_i32(dt: S.DType) -> Bool: match dt: case S.DI32{}: True{} case _: False{} def is_u8(dt: S.DType) -> Bool: match dt: case S.DU8{}: True{} case _: False{} def beq(a: Bool, b: Bool) -> Bool: match a b: case True{} True{}: True{} case False{} False{}: True{} case _ _: False{} # ---- float32 ---- # fmod's remainder (the dividend's sign) to Python's (the divisor's) def f_rem_fix(+r: F32, +y: F32) -> F32: Bool.pick(F32, Bool.and(Bool.not(F32.is_eq(r, 0.0)), Bool.not(beq(F32.is_lt(r, 0.0), F32.is_lt(y, 0.0)))), F32.add(r, y), r) def f2(op: B2, +x: U32, +y: U32) -> U32: match op: case BAdd{}: bf(F32.add(fb(x), fb(y))) case BSub{}: bf(F32.sub(fb(x), fb(y))) case BMul{}: bf(F32.mul(fb(x), fb(y))) case BDiv{}: bf(F32.div(fb(x), fb(y))) case BFloorDiv{}: bf(F32.floor(F32.div(fb(x), fb(y)))) case BRem{}: bf(f_rem_fix(F32.mod(fb(x), fb(y)), fb(y))) case BMin{}: Bool.pick(U32, fnan(x), x, Bool.pick(U32, fnan(y), y, Bool.pick(U32, F32.is_lt(fb(y), fb(x)), y, x))) case BMax{}: Bool.pick(U32, fnan(x), x, Bool.pick(U32, fnan(y), y, Bool.pick(U32, F32.is_lt(fb(x), fb(y)), y, x))) case BPow{}: bf(F32.pow(fb(x), fb(y))) case BEq{}: b2u(F32.is_eq(fb(x), fb(y))) case BNe{}: b2u(Bool.not(F32.is_eq(fb(x), fb(y)))) case BLt{}: b2u(F32.is_lt(fb(x), fb(y))) case BLe{}: b2u(F32.is_le(fb(x), fb(y))) case BGt{}: b2u(F32.is_lt(fb(y), fb(x))) case BGe{}: b2u(F32.is_le(fb(y), fb(x))) case _: 0 # ---- integers ---- # int32 floor division and remainder (Python's // and %; 0 for a zero divisor, as NumPy) def i_div_go(+q: U32, +r: U32, diff: Bool) -> U32: match diff: case True{}: Bool.pick(U32, U32.is_eq(r, 0), neg(q), neg((q + 1 : U32))) case False{}: q def abs32(+x: U32) -> U32: Bool.pick(U32, is_neg(x), neg(x), x) def i_fdiv(+x: U32, +y: U32) -> U32: Bool.pick(U32, U32.is_eq(y, 0), 0, i_div_go(U32.div(abs32(x), abs32(y)), U32.mod(abs32(x), abs32(y)), Bool.not(beq(is_neg(x), is_neg(y))))) def i_rem(+x: U32, +y: U32) -> U32: Bool.pick(U32, U32.is_eq(y, 0), 0, (x - (i_fdiv(x, y) * y : U32) : U32)) def u_fdiv(+x: U32, +y: U32) -> U32: Bool.pick(U32, U32.is_eq(y, 0), 0, U32.div(x, y)) def u_rem(+x: U32, +y: U32) -> U32: Bool.pick(U32, U32.is_eq(y, 0), 0, U32.mod(x, y)) # int32 (signed) and uint32 / uint8 / bool (unsigned); m is the mask for the result (255 for uint8) def i2(op: B2, +signed: Bool, +m: U32, +x: U32, +y: U32) -> U32: match op: case BAdd{}: U32.and((x + y : U32), m) case BSub{}: U32.and((x - y : U32), m) case BMul{}: U32.and((x * y : U32), m) case BFloorDiv{}: Bool.pick(U32, signed, i_fdiv(x, y), u_fdiv(x, y)) case BRem{}: Bool.pick(U32, signed, i_rem(x, y), u_rem(x, y)) case BMin{}: Bool.pick(U32, Bool.pick(Bool, signed, slt(y, x), U32.is_lt(y, x)), y, x) case BMax{}: Bool.pick(U32, Bool.pick(Bool, signed, slt(x, y), U32.is_lt(x, y)), y, x) case BEq{}: b2u(U32.is_eq(x, y)) case BNe{}: b2u(Bool.not(U32.is_eq(x, y))) case BLt{}: b2u(Bool.pick(Bool, signed, slt(x, y), U32.is_lt(x, y))) case BLe{}: b2u(Bool.not(Bool.pick(Bool, signed, slt(y, x), U32.is_lt(y, x)))) case BGt{}: b2u(Bool.pick(Bool, signed, slt(y, x), U32.is_lt(y, x))) case BGe{}: b2u(Bool.not(Bool.pick(Bool, signed, slt(x, y), U32.is_lt(x, y)))) case BAnd{}: b2u(Bool.and(U32.is_ne(x, 0), U32.is_ne(y, 0))) case BOr{}: b2u(Bool.or(U32.is_ne(x, 0), U32.is_ne(y, 0))) case BXor{}: b2u(Bool.not(beq(U32.is_ne(x, 0), U32.is_ne(y, 0)))) case _: 0 # one cell of a binary operation def cell2(+op: B2, dt: S.DType, +x: U32, +y: U32) -> U32: match dt: case S.DF32{}: f2(op, x, y) case S.DI32{}: i2(op, True{}, 4294967295, x, y) case S.DU32{}: i2(op, False{}, 4294967295, x, y) case S.DU8{}: i2(op, False{}, 255, x, y) case S.DBool{}: i2(op, False{}, 1, x, y) # ---- unary operations and conversions ---- type U1 is Data: UNeg{} UAbs{} UNot{} UScale{by: U32} UConv{to: S.DType} def i32_to_f32(+x: U32) -> F32: Bool.pick(F32, is_neg(x), F32.neg(U32.to_f32(neg(x))), U32.to_f32(x)) # float32 to int32 bits, toward zero def f32_to_i32(+f: F32) -> U32: Bool.pick(U32, F32.is_lt(f, 0.0), neg(F32.to_u32(F32.neg(f))), F32.to_u32(f)) def conv_from_f32(to: S.DType, +x: U32) -> U32: match to: case S.DF32{}: x case S.DI32{}: f32_to_i32(fb(x)) case S.DU32{}: f32_to_i32(fb(x)) case S.DU8{}: u8(f32_to_i32(fb(x))) case S.DBool{}: b2u(Bool.not(F32.is_eq(fb(x), 0.0))) def conv_int(+signed: Bool, to: S.DType, +x: U32) -> U32: match to: case S.DF32{}: bf(Bool.pick(F32, signed, i32_to_f32(x), U32.to_f32(x))) case S.DU8{}: u8(x) case S.DBool{}: b2u(U32.is_ne(x, 0)) case _: x def conv(from: S.DType, +to: S.DType, +x: U32) -> U32: match from: case S.DF32{}: conv_from_f32(to, x) case S.DI32{}: conv_int(True{}, to, x) case _: conv_int(False{}, to, x) def cell1(op: U1, +dt: S.DType, +x: U32) -> U32: match op: case UNeg{}: Bool.pick(U32, is_f32(dt), bf(F32.neg(fb(x))), Bool.pick(U32, is_bool(dt), x, U32.and(neg(x), Bool.pick(U32, Bool.not(is_u8(dt)), 4294967295, 255)))) case UAbs{}: Bool.pick(U32, is_f32(dt), U32.and(x, 2147483647), Bool.pick(U32, is_i32(dt), abs32(x), x)) case UNot{}: b2u(U32.is_eq(x, 0)) case UScale{by}: bf(F32.div(fb(x), fb(by))) case UConv{to}: conv(dt, to, x)