Efficiently create a thermometer code in verilog:

localparam M = 32;

function [M-1:0] therm_code;
  input    [$clog2(M):0] val;
  begin
    for (int i = 0; i<M; i++) begin
      therm_code[i] = (i<val);
    end
  end
endfunction

Other methods which do not synthesis as well:

therm_code  = (2**val) - 1;

therm_code = ~({M{1'b1}} << val);