Opened 12 years ago

Closed 10 years ago

#1193 closed defect (worksforme)

Replace SpiderMonkey's Math functions with platform consistent versions

Reported by: historic_bruno Owned by:
Priority: Should Have Milestone: Alpha 16
Component: Core engine Keywords: spidermonkey
Cc: Yves Patch:

Description (last modified by historic_bruno)

As Philip reported on Bugzilla, there are differences in SpiderMonkey's floating point Math functions on different platforms. Even a small discrepancy can cause OOS errors in multiplayer games, if those functions are used in e.g. simulation, map generation, or AI code. We already use fixed point approximations for sin/cos and atan2 in the engine, but not yet for scripts. Currently no scripts appear to use atan, acos, or asin.

(There may also be differences in pow and exp which we do use rarely, but I wasn't able to reproduce them with the above test cases.)

Attachments (1)

1193.diff (2.6 KB ) - added by Jonathan Waller 12 years ago.
These changes should fix the ticket, will wait till after A9 to commit them

Download all attachments as: .zip

Change History (26)

comment:1 by Jonathan Waller, 12 years ago

Owner: changed from historic_bruno to Jonathan Waller

The sin/cos approximations which are accurate to about 10-9 have been added with commit http://trac.wildfiregames.com/changeset/11226 . I will look at writing a good approximation for atan2.

comment:2 by historic_bruno, 12 years ago

Description: modified (diff)
Summary: Replace SpiderMonkey's trig functions with safe fixed-point versionsReplace SpiderMonkey's trig functions with platform consistent versions

Updated title and description since fixed point is no longer the focus.

Also [11220] is relevant, it introduced a new rmgen function GetAngle, which calls Math.acos. I don't know if acos is even a problem, I simply mentioned all the trig functions :)

comment:3 by Jonathan Waller, 12 years ago

In 11231:

Added atan and atan2. Refs #1193

comment:4 by historic_bruno, 12 years ago

This ticket is probably solved well enough for Alpha 9. Would be nice to add some tests eventually.

comment:5 by Kieran P, 12 years ago

Milestone: Alpha 9Alpha 10

by Jonathan Waller, 12 years ago

Attachment: 1193.diff added

These changes should fix the ticket, will wait till after A9 to commit them

comment:6 by Kieran P, 12 years ago

Bump. Can we get this finished up easily?

comment:7 by leper, 12 years ago

A slightly modified version of the last diff was applied in [11363].

comment:8 by Jonathan Waller, 12 years ago

This is not closed because we were wanting to add some tests. I should get round to this sometime soon.

comment:9 by Kieran P, 12 years ago

Milestone: Alpha 10Backlog

comment:10 by historic_bruno, 12 years ago

I would suggest that as described in #433, the Math tests be run at game startup by everyone, not only in the standard test suite. That way we can hopefully detect problems like [11857] sooner and with more explicit errors.

comment:11 by ben, 12 years ago

In 12056:

Implements global tech modification function. Fixes #1358, refs #1520.
Applies tech modifications to template data returned by GuiInterface.
Extends engine to load arbitrary global scripts, separates this from RNG replacement. Refs #1193.
Loads global scripts for most script contexts for consistency.
Adds simulation tests for global scripts.

comment:12 by historic_bruno, 11 years ago

Summary: Replace SpiderMonkey's trig functions with platform consistent versionsReplace SpiderMonkey's Math functions with platform consistent versions

Should also replace Math.pow and Math.exp at some point.

in reply to:  12 comment:13 by historic_bruno, 11 years ago

Milestone: BacklogAlpha 14
Priority: Should HaveRelease Blocker

Replying to historic_bruno:

Should also replace Math.pow and Math.exp at some point.

This is in fact rather important, see #1990.

comment:14 by sanderd17, 11 years ago

There are some implementations of Math.pow, Math.exp and Math.log in #1990 now.

comment:15 by Yves, 11 years ago

Cc: Yves added

I found this in Spidermonkey's configure script:

--enable-more-deterministic Enable changes that make the shell more deterministic"

I don't know what it does and the word "more" looks suspicious.

comment:16 by ben, 11 years ago

In 13515:

Adds JS implementations of Math.pow, exp, and log, by sanderd17, refs #1193.
Fixes #1990 (OOS caused by Math.pow).
Adds tests and documentation for Math functions.

comment:17 by sanderd17, 11 years ago

As a reference, link to a sqrt algorithm: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29

Of course, there is also a very simple way to just define sqrt(x)=pow(x,0.5). But a native sqrt algorithm would be more optimal.

comment:18 by wraitii, 11 years ago

There are a few cases where Aegis and qBot use Math.sqrt, Math.round, Math.min, Math.max, and Math.floor/Math.ceil.

If their behaviors can be inconsistent too, this might explain both #1966 and #2000.

Last edited 11 years ago by fabio (previous) (diff)

comment:19 by sanderd17, 11 years ago

I hope Math.round/ceil/floor don't give problems, as I use them for pow and exp calculation. And I don't know how we can replace those functions, as we can't read the internal values.

For Math.sqrt, you could try defining it as pow(x,0.5) and see if those problems still occur (although they're hard to test).

comment:20 by historic_bruno, 11 years ago

Owner: Jonathan Waller removed

comment:21 by historic_bruno, 11 years ago

Current plan: test before A14 release, if there are no OOS errors with Aegis, push this to backlog. Otherwise experiment with replacing Math.pow, but note it's much slower than the native Math.sqrt (by 600x or so)

comment:22 by historic_bruno, 11 years ago

Milestone: Alpha 14Alpha 15
Priority: Release BlockerShould Have

No recent OOS reports with Aegis, so I'm pushing this back to A15 and lowering priority. All that remains is deciding if we want to replace Math.sqrt and adding some runtime tests like #433.

comment:23 by historic_bruno, 11 years ago

Cc: Philip Taylor removed

Replacing Math.sqrt doesn't solve #2000.

comment:24 by wraitii, 10 years ago

Milestone: Alpha 15Alpha 16

comment:25 by sanderd17, 10 years ago

Resolution: worksforme
Status: newclosed

It has been working a long time now, so I don't think there are issues with the remaining functions.

Note: See TracTickets for help on using tickets.