From WikiChip
Module:frequency table
Revision as of 17:37, 16 July 2017 by At32Hz (talk | contribs)

To add this template, simple add
{{frequency table}}
to the page and save. The chip infobox will have a small [Modify Frequency Info] button at the top-right corner which can be used to add values using a form.
local p = {}
local origArgs

function p.add_file_prefix(str)
  if string.match(string.lower(str), '^file:') then
    return str
  else
    return 'File:' .. str
  end
end

function p.add_separator(box, title)
  box:tag('tr')
           :tag('td')
           :attr('class', 'header')
           :attr('colspan', '2')
           :wikitext(title)
end

function p.add_entry(value, right)
  local e = mw.html.create('tr')
  e:tag('td')
    :attr('class', 'label')
    :wikitext(value)
  e:node(right)
  return e   
end

function has_arg(name)
	-- The argument can exist and be empty or not exist at all
	return string.len(origArgs[name] or '') > 0
end

function arg(name)
	return origArgs[name]
end

function p.frequency_table(frame)
	
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame.args
    end
    
    local norm, avx2, avx512 = false, false, false
    
    local n = 1
    for i = 1, 50 do
    	if	has_arg('freq_' .. i) then n = i; norm = true end
    	if	has_arg('freq_avx2_' .. i) then n = i; avx2 = true end
    	if	has_arg('freq_avx512_' .. i) then n = i; avx512 = true end
	end
	
	
    local tbl = mw.html.create('table')

    tbl
        :attr('class', 'wikitable')
        :attr('style', 'margin-left: 10px;')
        
    local tr = tbl:tag('tr')
    	tr:tag('th'):attr('colspan', '2')
    	tr:tag('th'):attr('colspan', i):wikitext('Frequency/Active Cores')
    	
	tr = tbl:tag('tr')
		tr:tag('th'):wikitext('Mode')
		tr:tag('th'):wikitext('Base')
		for i = 1, n do
			tr:tag('th'):wikitext(i)	
		end
	
	if norm then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('Normal')
		for i = 1, n do
			if has_arg('freq_' .. i) then
				tr:tag('td'):wikitext(arg('freq_' .. i))
			else
				tr:tag('td'):wikitext(' ')
			end
		end
	end
	
	if avx2 then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('AVX2')
		for i = 1, n do
			if has_arg('freq_avx2_' .. i) then
				tr:tag('td'):wikitext(arg('freq_avx2_' .. i))
			else
				tr:tag('td'):wikitext(' ')
			end
		end
	end
	
	if avx512 then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('AVX512')
		for i = 1, n do
			if has_arg('freq_avx512_' .. i) then
				tr:tag('td'):wikitext(arg('freq_avx512_' .. i))
			else
				tr:tag('td'):wikitext(' ')
			end
		end
	end
	
    return tbl
end

return p