From WikiChip
Module:frequency table
Revision as of 11:49, 27 July 2017 by Inject (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 to_raw_megahertz(txt)
	txt = string.gsub(string.lower(txt), ",", "")
	txt = string.gsub(txt, ' ', '')
	if string.find(txt, 'ghz') then
		txt = string.gsub(txt, 'ghz', '')
		return tonumber(txt) * 1000
	end
	if string.find(txt, 'mhz') then
		txt = string.gsub(txt, 'mhz', '')
		return tonumber(txt)
	end
end

function p.get_color_from_normalized_speed(speed)
	speed = to_raw_megahertz(speed)
	local percent
	
	if p.lowestF == p.highestF then
		percent = 1
	else
		percent = (speed - p.lowestF) / (p.highestF - p.lowestF)
	end
	
	if (p.lowestF / p.highestF) > 0.85 then --if less than 15% diff, we just go to orange
		percent = percent + .5
	end
	
	local color = 120 * percent -- green being 120, red is 0
	
	return 'hsl(' .. color .. ', 100%, 50%)'
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 normN, avx2N, avx512N, tmp = 0, 0, 0, 0
    p.highestF, p.lowestF = 0, 999999
    
    -- check how many cores we have
    -- also check highest/lowest frequencies
    for i = 1, 50 do
    	if	has_arg('freq_' .. i) then
    		normN = i; norm = true
    		tmp = to_raw_megahertz(arg('freq_' .. i))
    		if tmp > p.highestF then p.highestF = tmp end
    		if tmp < p.lowestF then p.lowestF = tmp end
		end
    	if	has_arg('freq_avx2_' .. i) then
			avx2N = i; avx2 = true
    		tmp = to_raw_megahertz(arg('freq_avx2_' .. i))
    		if tmp > p.highestF then p.highestF = tmp end
    		if tmp < p.lowestF then p.lowestF = tmp end
		end
    	if	has_arg('freq_avx512_' .. i) then
    		avx512N = i; avx512 = true
    		tmp = to_raw_megahertz(arg('freq_avx512_' .. i))
    		if tmp > p.highestF then p.highestF = tmp end
    		if tmp < p.lowestF then p.lowestF = tmp end
		end
	end

	-- max cores of all
	local maxN = math.max(normN, avx2N, avx512N)
	
	
    local tbl = mw.html.create('table')

    tbl
        :attr('class', 'wikitable')
        :attr('style', 'white-space: nowrap;')
        
    local tr = tbl:tag('tr')
		tr:tag('th'):attr('rowspan', '2'):wikitext('Mode')
		tr:tag('th'):attr('rowspan', '2'):wikitext('Base')
    	tr:tag('th'):attr('colspan', maxN):attr('style', 'text-align: left;'):wikitext('Turbo Frequency/Active Cores')
    	
	tr = tbl:tag('tr')
		for i = 1, maxN do
			tr:tag('th'):wikitext(i)	
		end
	
	if norm or has_arg('freq_base') then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('Normal')
		tr:tag('th'):wikitext(arg('freq_base'))
		for i = 1, normN do
			if has_arg('freq_' .. i) then
				tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_' .. i))):wikitext(arg('freq_' .. i))
			else
				tr:tag('td'):wikitext('&nbsp;')
			end
		end
	end
	
	if avx2 or has_arg('freq_avx2_base') then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('AVX2')
		tr:tag('th'):wikitext(arg('freq_avx2_base'))
		for i = 1, avx2N do
			if has_arg('freq_avx2_' .. i) then
				tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_avx2_' .. i))):wikitext(arg('freq_avx2_' .. i))
			else
				tr:tag('td'):wikitext('&nbsp;')
			end
		end
	end
	
	if avx512 or has_arg('freq_avx512_base') then
		tr = tbl:tag('tr')
		tr:tag('th'):wikitext('AVX512')
		tr:tag('th'):wikitext(arg('freq_avx512_base'))
		for i = 1, avx512N do
			if has_arg('freq_avx512_' .. i) then
				tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_avx512_' .. i))):wikitext(arg('freq_avx512_' .. i))
			else
				tr:tag('td'):wikitext('&nbsp;')
			end
		end
	end
	
    return tbl
end

return p