materialsystem/cmatlightmaps.cpp
fltx4 ConvertLightmapColorToRGBScale( FLTX4 lightmapColor )
{
	
	static const fltx4 vTwoFiftyFive = {255.0f, 255.0f, 255.0f, 255.0f};
	static const fltx4 FourPoint1s = { 0.1, 0.1, 0.1, 0.1 };
	static const fltx4 vTwoFiftyFiveOverSixteen = {255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f};
	// static const fltx4 vSixteenOverTwoFiftyFive = { 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f };


	// find the highest color value in lightmapColor and replicate it
	fltx4 scale = FindHighestSIMD3( lightmapColor );
	fltx4 minscale = FindLowestSIMD3( lightmapColor );
	fltx4 fl4OutofRange = OrSIMD( CmpGeSIMD( scale, Four_Ones ), CmpLeSIMD( scale, FourPoint1s ) );
	fl4OutofRange = OrSIMD( fl4OutofRange, CmpGtSIMD( minscale, MulSIMD( Four_PointFives, scale ) ) );

	// scale needs to be divided by 16 (because the shader multiplies it by 16)
	// then mapped to 0..255 and quantized. 
	scale = __vrfip(MulSIMD(scale, vTwoFiftyFiveOverSixteen)); // scale = ceil(scale * 255/16)
		
	fltx4 result = MulSIMD(vTwoFiftyFive, lightmapColor); // start the scale cooking on the final result
		
	fltx4 invScale = ReciprocalEstSIMD(scale); // invScale = (16/255)(1/scale). may be +inf
	invScale = MulSIMD(invScale, vTwoFiftyFiveOverSixteen); // take the quantizing factor back out
															// of the inverse scale (one less
															// dependent op if you do it this way)
		
	// scale the input channels
	// compute so the numbers are all 0..255 ints. (if one happens to 
	// be 256 due to numerical error in the reciprocation, the unsigned-saturate
	// store we'll use later on will bake it back down to 255)
	result = MulSIMD(result, invScale);
		
	// now, output --
	// if the input color was nonzero, slip the scale into return value's w
	// component and return. If the input was zero, return zero.

	result = MaskedAssign( 
		fl4OutofRange,
		SetWSIMD( result, scale ),
		SetWSIMD( MulSIMD( lightmapColor, vTwoFiftyFive ), vTwoFiftyFiveOverSixteen ) );
	return result;
}
// SIMD version of above
// input numbers from pSrc are on the domain [0..16]
// output is RGBA 
// ignores contents of w channel of input
// the shader does this: rOut = Rin * Ain * 16.0f 
// where Rin is [0..1], a float computed from a byte value [0..255]
// Ain is therefore the brightest channel (say R) divided by 16 and quantized
// Rin is computed from pSrc->r by dividing by Ain
// this outputs RGBa where RGB are [0..255] and a is the shader's scaling factor (also 0..255)
//
// WARNING - this code appears to be vulnerable to a compiler bug. Be very careful modifying and be
// sure to test
SIMD version of above
input numbers from pSrc are on the domain [0..16]
output is RGBA
ignores contents of w channel of input
the shader does this: rOut = Rin * Ain * 16.0f
where Rin is [0..1], a float computed from a byte value [0..255]
Ain is therefore the brightest channel (say R) divided by 16 and quantized
Rin is computed from pSrc->r by dividing by Ain
this outputs RGBa where RGB are [0..255] and a is the shader's scaling factor (also 0..255)

this code appears to be vulnerable to a compiler bug. Be very careful modifying and be
sure to test
SIMD version of above input numbers from pSrc are on the domain [ 0 .. 16 ] output is RGBA ignores contents of w channel of input the shader does this : rOut = Rin * Ain * 16 . 0f where Rin is [ 0 .. 1 ] a float computed from a byte value [ 0 .. 255 ] Ain is therefore the brightest channel ( say R ) divided by 16 and quantized Rin is computed from pSrc - > r by dividing by Ain this outputs RGBa where RGB are [ 0 .. 255 ] and a is the shader ' s scaling factor ( also 0 .. 255 ) this code appears to be vulnerable to a compiler bug . Be very careful modifying and be sure to test
fltx4 ConvertLightmapColorToRGBScale( FLTX4 lightmapColor )
{
	static const fltx4 vTwoFiftyFive = {255.0f, 255.0f, 255.0f, 255.0f};
	static const fltx4 FourPoint1s = { 0.1, 0.1, 0.1, 0.1 };
	static const fltx4 vTwoFiftyFiveOverSixteen = {255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f, 255.0f / 16.0f};
	fltx4 scale = FindHighestSIMD3( lightmapColor );
	fltx4 minscale = FindLowestSIMD3( lightmapColor );
	fltx4 fl4OutofRange = OrSIMD( CmpGeSIMD( scale, Four_Ones ), CmpLeSIMD( scale, FourPoint1s ) );
	fl4OutofRange = OrSIMD( fl4OutofRange, CmpGtSIMD( minscale, MulSIMD( Four_PointFives, scale ) ) );
	scale = __vrfip(MulSIMD(scale, vTwoFiftyFiveOverSixteen)); 
	fltx4 result = MulSIMD(vTwoFiftyFive, lightmapColor); 
	fltx4 invScale = ReciprocalEstSIMD(scale); 
	invScale = MulSIMD(invScale, vTwoFiftyFiveOverSixteen); 
	result = MulSIMD(result, invScale);
	result = MaskedAssign( 
		fl4OutofRange,
		SetWSIMD( result, scale ),
		SetWSIMD( MulSIMD( lightmapColor, vTwoFiftyFive ), vTwoFiftyFiveOverSixteen ) );
	return result;
}
fltx4 ConvertLightmapColorToRGBScale ( FLTX4 lightmapColor ) { static const fltx4 vTwoFiftyFive = { 255.0f , 255.0f , 255.0f , 255.0f } ; static const fltx4 FourPoint1s = { 0.1 , 0.1 , 0.1 , 0.1 } ; static const fltx4 vTwoFiftyFiveOverSixteen = { 255.0f / 16.0f , 255.0f / 16.0f , 255.0f / 16.0f , 255.0f / 16.0f } ; fltx4 scale = FindHighestSIMD3 ( lightmapColor ) ; fltx4 minscale = FindLowestSIMD3 ( lightmapColor ) ; fltx4 fl4OutofRange = OrSIMD ( CmpGeSIMD ( scale , Four_Ones ) , CmpLeSIMD ( scale , FourPoint1s ) ) ; fl4OutofRange = OrSIMD ( fl4OutofRange , CmpGtSIMD ( minscale , MulSIMD ( Four_PointFives , scale ) ) ) ; scale = __vrfip ( MulSIMD ( scale , vTwoFiftyFiveOverSixteen ) ) ; fltx4 result = MulSIMD ( vTwoFiftyFive , lightmapColor ) ; fltx4 invScale = ReciprocalEstSIMD ( scale ) ; invScale = MulSIMD ( invScale , vTwoFiftyFiveOverSixteen ) ; result = MulSIMD ( result , invScale ) ; result = MaskedAssign ( fl4OutofRange , SetWSIMD ( result , scale ) , SetWSIMD ( MulSIMD ( lightmapColor , vTwoFiftyFive ) , vTwoFiftyFiveOverSixteen ) ) ; return result ; }
SIMD version of above
input numbers from pSrc are on the domain [0..16]
output is RGBA
ignores contents of w channel of input
the shader does this: rOut = Rin * Ain * 16.0f
where Rin is [0..1], a float computed from a byte value [0..255]
Ain is therefore the brightest channel (say R) divided by 16 and quantized
Rin is computed from pSrc->r by dividing by Ain
this outputs RGBa where RGB are [0..255] and a is the shader's scaling factor (also 0..255)
SIMD version of above input numbers from pSrc are on the domain [ 0 .. 16 ] output is RGBA ignores contents of w channel of input the shader does this : rOut = Rin * Ain * 16 . 0f where Rin is [ 0 .. 1 ] a float computed from a byte value [ 0 .. 255 ] Ain is therefore the brightest channel ( say R ) divided by 16 and quantized Rin is computed from pSrc - > r by dividing by Ain this outputs RGBa where RGB are [ 0 .. 255 ] and a is the shader ' s scaling factor ( also 0 .. 255 )
// static const fltx4 vSixteenOverTwoFiftyFive = { 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f, 16.0f / 255.0f }; // find the highest color value in lightmapColor and replicate it // scale needs to be divided by 16 (because the shader multiplies it by 16) // then mapped to 0..255 and quantized.  // scale = ceil(scale * 255/16) // start the scale cooking on the final result // invScale = (16/255)(1/scale). may be +inf // take the quantizing factor back out // of the inverse scale (one less // dependent op if you do it this way) // scale the input channels // compute so the numbers are all 0..255 ints. (if one happens to  // be 256 due to numerical error in the reciprocation, the unsigned-saturate // store we'll use later on will bake it back down to 255) // now, output -- // if the input color was nonzero, slip the scale into return value's w // component and return. If the input was zero, return zero.