Avoiding Denormals

From ActionScriptWiki

Jump to: navigation, search

Denormal numbers are floating point numbers that are really small. When such a number occurs the CPU switches into denormal mode and calculations become very slow.

Contents

[edit] Example

In this example the framerate will jump from approximately 40fps to 1fps. The reason are denormal numbers.

private var _denormals: Vector.<Number>;

private function init(): void
{
	_denormals = new Vector.<Number>( 0xfffff, true );
	
	var n: int = 0xfffff;
	
	while( --n > -1 )
		_denormals[ n ] = 1e-256;//very small number!
		
	EnterFrameProvider.connect( enterFrame );
}

private function enterFrame(): void
{
	var n: int = 0xfffff;
	
	while( --n > -1 )
		_denormals[ n ] *= 0.5;//do some calculations with veery small numbers
}

[edit] ActionScript Solution

There are a couple of solutions to deal with denormal numbers. Some try to avoid them by adding always a very small value or by adding some white noise. In ActionScript the best and most simple solution is by adding and subtracting an anti-denormal constant.

[edit] Solution

This solution is easy to implement in ActionScript and will result in real zero values. It has been tested successfully on

  • Mac OS X (Intel CPU)
  • Windows/Linux (Intel/AMD CPU)
x = x + 1e-18 - 1e-18;

[edit] Example

This is the same example with the fix for denormal numbers. The framerate will now stay at approximately 40fps.

private var _denormals: Vector.<Number>;

private function init(): void
{
	_denormals = new Vector.<Number>( 0xfffff, true );
	
	var n: int = 0xfffff;
	
	while( --n > -1 )
		_denormals[ n ] = 1e-256;//very small number!
		
	EnterFrameProvider.connect( enterFrame );
}

private function enterFrame(): void
{
	var n: int = 0xfffff;
	
	while( --n > -1 )
		_denormals[ n ] = _denormals[ n ] * 0.5 + 1e-18 - 1e-18;//avoiding denormals
}

[edit] See Also

Personal tools