<commit_before>open Ir

let brightness = 50

let i = Var("i")

let outbuf = 2
let inbuf = 1

(* TODO: can't work with vectors until we can lift contstants to vectors *)
(* let vecwidth = 16 *)
(*let vt = Vector(u8, vecwidth)*)
let vecwidth = 1
let vt = u8

let load = Load (vt, {buf = inbuf; idx = i})
let store vec = Store(vec, {buf = outbuf; idx = i})

let prgm w h c =
  Map({name = "i"; range = (0, (w*h*c)/vecwidth)},
      (* TODO: add max to clamp add *)
      store (Add(vt, (load, Cast(u8, UIntImm(brightness))))))

let () =
  Test_runner.run prgm "brightness"<commit_msg>Modified brightness example to use saturating add<commit_after>open Ir

let brightness = 50

let i = Var("i")

let outbuf = 2
let inbuf = 1

(* TODO: can't work with vectors until we can lift contstants to vectors *)
(* let vecwidth = 16 *)
(*let vt = Vector(u8, vecwidth)*)
let vecwidth = 1
let vt = u8

let load = Load (vt, {buf = inbuf; idx = i})
let store vec = Store(vec, {buf = outbuf; idx = i})

exception Unsupported_type of val_type

(* saturating add *)
let sadd a b =
  let t = val_type_of_expr a in
  let max_val = match t with
    | UInt(8)  -> Cast(t, UIntImm(0xFF))
    | UInt(16) -> Cast(t, UIntImm(0xFFFF))
    | UInt(32) -> Cast(t, UIntImm(0xFFFFFFFF))
    | t -> raise (Unsupported_type(t))
  in
    Select(
      (* a > 0xFF... - b *)
      GT(a, Sub(t, (max_val, b))),
      (* ? 0xFF... *)
      max_val,
      (* : a+b *)
      Add(t, (a, b))
    )

let prgm w h c =
  Map(
    {name = "i"; range = (0, (w*h*c)/vecwidth)},
    store (
      sadd load (Cast(u8, UIntImm(brightness)))
    )
  )

let () =
  Test_runner.run prgm "brightness"
{'fraction_non_alphanumeric': 0.16017052375152252, 'fraction_numerical': 0.0194884287454324, 'mean_word_length': 2.9119047619047618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3171515', 'n_tokens_mistral': 678, 'n_tokens_neox': 637, 'n_words': 207}