From WikiChip
Difference between revisions of "Module:frequency table"

 
(15 intermediate revisions by 2 users not shown)
Line 51: Line 51:
 
function p.get_color_from_normalized_speed(speed)
 
function p.get_color_from_normalized_speed(speed)
 
speed = to_raw_megahertz(speed)
 
speed = to_raw_megahertz(speed)
 +
local percent
 
 
local percent = (speed - p.lowestF) / p.highestF;
+
if p.lowestF == p.highestF then
local color = 120 * percent -- red being 120, green is 0
+
percent = 1
 +
else
 +
percent = (speed - p.lowestF) / (p.highestF - p.lowestF)
 +
end
 +
 +
local color
 +
 +
if (p.lowestF / p.highestF) > 0.85 then --if less than 15% diff, we just go to orange
 +
color  = 60 * percent + 60
 +
else
 +
color  = 120 * percent -- green being 120, red is 0
 +
end
 
 
 
return 'hsl(' .. color .. ', 100%, 50%)'
 
return 'hsl(' .. color .. ', 100%, 50%)'
Line 67: Line 79:
 
      
 
      
 
     local norm, avx2, avx512 = false, false, false
 
     local norm, avx2, avx512 = false, false, false
     local normN, avx2N, avx512N = 0, 0, 0
+
     local normN, avx2N, avx512N, tmp = 0, 0, 0, 0
     local highestF, lowestF, tmp = 0, 999999, 0
+
     p.highestF, p.lowestF = 0, 999999
 
      
 
      
 
     -- check how many cores we have
 
     -- check how many cores we have
Line 76: Line 88:
 
     normN = i; norm = true
 
     normN = i; norm = true
 
     tmp = to_raw_megahertz(arg('freq_' .. i))
 
     tmp = to_raw_megahertz(arg('freq_' .. i))
     if tmp > highest then highest = tmp end
+
     if tmp > p.highestF then p.highestF = tmp end
     if tmp < lowest then lowest = tmp end
+
     if tmp < p.lowestF then p.lowestF = tmp end
 
end
 
end
 
     if has_arg('freq_avx2_' .. i) then
 
     if has_arg('freq_avx2_' .. i) then
 
avx2N = i; avx2 = true
 
avx2N = i; avx2 = true
 
     tmp = to_raw_megahertz(arg('freq_avx2_' .. i))
 
     tmp = to_raw_megahertz(arg('freq_avx2_' .. i))
     if tmp > highest then highest = tmp end
+
     if tmp > p.highestF then p.highestF = tmp end
     if tmp < lowest then lowest = tmp end
+
     if tmp < p.lowestF then p.lowestF = tmp end
 
end
 
end
 
     if has_arg('freq_avx512_' .. i) then
 
     if has_arg('freq_avx512_' .. i) then
 
     avx512N = i; avx512 = true
 
     avx512N = i; avx512 = true
 
     tmp = to_raw_megahertz(arg('freq_avx512_' .. i))
 
     tmp = to_raw_megahertz(arg('freq_avx512_' .. i))
     if tmp > highest then highest = tmp end
+
     if tmp > p.highestF then p.highestF = tmp end
     if tmp < lowest then lowest = tmp end
+
     if tmp < p.lowestF then p.lowestF = tmp end
 
end
 
end
 
end
 
end
Line 95: Line 107:
 
-- max cores of all
 
-- max cores of all
 
local maxN = math.max(normN, avx2N, avx512N)
 
local maxN = math.max(normN, avx2N, avx512N)
 
-- low, high, third
 
local thirdF = (highestF - lowestF) / 3
 
p.highestF = highestF
 
p.lowestF = lowestF
 
p.thirdF = (highestF - lowestF) / 3
 
 
 
 
 
Line 112: Line 118:
 
tr:tag('th'):attr('rowspan', '2'):wikitext('Mode')
 
tr:tag('th'):attr('rowspan', '2'):wikitext('Mode')
 
tr:tag('th'):attr('rowspan', '2'):wikitext('Base')
 
tr:tag('th'):attr('rowspan', '2'):wikitext('Base')
     tr:tag('th'):attr('colspan', maxN):attr('style', 'text-align: left;'):wikitext('Frequency/Active Cores')
+
     tr:tag('th'):attr('colspan', maxN):attr('style', 'text-align: left;'):wikitext('Turbo Frequency/Active Cores')
 
    
 
    
 
tr = tbl:tag('tr')
 
tr = tbl:tag('tr')
Line 125: Line 131:
 
for i = 1, normN do
 
for i = 1, normN do
 
if has_arg('freq_' .. i) then
 
if has_arg('freq_' .. i) then
tr:tag('td'):wikitext(arg('freq_' .. i))
+
tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_' .. i))):wikitext(arg('freq_' .. i))
 
else
 
else
 
tr:tag('td'):wikitext('&nbsp;')
 
tr:tag('td'):wikitext('&nbsp;')
Line 138: Line 144:
 
for i = 1, avx2N do
 
for i = 1, avx2N do
 
if has_arg('freq_avx2_' .. i) then
 
if has_arg('freq_avx2_' .. i) then
tr:tag('td'):wikitext(arg('freq_avx2_' .. i))
+
tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_avx2_' .. i))):wikitext(arg('freq_avx2_' .. i))
 
else
 
else
 
tr:tag('td'):wikitext('&nbsp;')
 
tr:tag('td'):wikitext('&nbsp;')
Line 151: Line 157:
 
for i = 1, avx512N do
 
for i = 1, avx512N do
 
if has_arg('freq_avx512_' .. i) then
 
if has_arg('freq_avx512_' .. i) then
tr:tag('td'):wikitext(arg('freq_avx512_' .. i))
+
tr:tag('td'):attr('style', 'background-color:' .. p.get_color_from_normalized_speed(arg('freq_avx512_' .. i))):wikitext(arg('freq_avx512_' .. i))
 
else
 
else
 
tr:tag('td'):wikitext('&nbsp;')
 
tr:tag('td'):wikitext('&nbsp;')

Latest revision as of 11:53, 27 July 2017

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
	
	local color
	
	if (p.lowestF / p.highestF) > 0.85 then --if less than 15% diff, we just go to orange
		color  = 60 * percent + 60
	else
		color  = 120 * percent -- green being 120, red is 0
	end
	
	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