Internals: Not getting floating point correct

Posted on
  • I'm working on support for an as un-yet supported board and am making progress. During my unit testing, I am finding that something odd is happening with numbers. For example, I ran the following function:

    var x=1; setInterval(function() {x=x+1;}, 1000);
    

    Expecting to find that the function would be called every second. Instead, I seem to find it being called very, very fast. When I run dump(), I find the following:

    setInterval(function () {x=x+1;}, 8.855);
    

    which as you can see, the last parameter has been changed. Digging further, I found the function called:

    JsVar *_jswrap_interface_setTimeoutOrInterval(JsVar *func, JsVarFloat interval, JsVar *args, bool isTimeout)
    

    and added a logging statement to print the value of "interval" when it is called. What is logged is:

    8.85454559326
    

    I'm not sure where to go next. Apparently something is wrong in my setup. My processor is the xtensa lx106 (ESP8266) which I believe is a 32bit device. I sense I may be mixing 64bit processing and 32bit processing ... but haven't seen where yet.

    Neil

  • It'll almost certainly be to do with jsnative.c. This calls C functions with a varying number of arguments and argument types - unfortunately how it does so depends on how the underlying architecture of the CPU calls things.

    It might be that just changing the #ifdef ARM to #if defined(ARM) || defined(XTENSA) would do the trick, but you'd have to do some testing (actually it should probably work the other way, testing for x86 - as that's the one that's completely crazy).

    The issue is really whether floating point arguments are passed on the same stack/registers as normal args. In ARM (and prob Xtensa) they are, but in x86 they aren't.

  • Am starting to examine and debug now ... I ran a trace ... and the following is interesting as it appears to clearly show the puzzle. Now going to start looking at jsnative.c:

    >var x = setInterval(function() {
    :  print("!");
    :}, 1000);
    =1
    #1[r2,l1] Object {
      #2[r1,l2] Name String [1 blocks] "ÿ"    #3[r1,l2] Object {
          #6[r1,l2] Name String [2 blocks] "timers"        #8[r2,l1] Array(2) [
              #46[r1,l2] Name Integer 1            #38[r1,l1] Object {
                  #40[r1,l2] Name String [1 blocks] "time"                #39[r1,l1] Integer 10249
                  #42[r1,l2] Name String [2 blocks] "interval"                #41[r1,l1] Integer 8855
                  #44[r1,l2] Name String [2 blocks] "callback"                #25[r1,l1] Function {
                      #21[r1,l2] Name String [1 blocks] "ÿcod"                    #24[r1,l1] String [3 blocks] "{\n  print(\"!\");\n}"
                    }
                }
            ]
          #9[r1,l2] Name String [2 blocks] "watches"        #11[r2,l1] Array(0) [ ]
          #12[r1,l2] Name String [2 blocks] "history"        #16[r1,l1] Array(3) [
              #17[r1,l2] Name Integer 0            #13[r1,l1] String [2 blocks] "echo(0);"
              #18[r1,l2] Name Integer 1            #19[r1,l1] String [2 blocks] "echo(1);"
              #26[r1,l2] Name Integer 2            #28[r1,l1] String [8 blocks] "var x = setInterval(function() {\n  print(\"!\");\n}, 1000);"
            ]
        }
      #27[r1,l2] Name String [1 blocks] "x"= int 1
    }
    =undefined
    
  • Well ... that was nice. This is what an ounce of expert knowledge does. Based on Gordon's suggestion, I added a new processor flag called XTENSA and changed the preprocessor to behave as he posted in his previous post and it worked first time. I'd NEVER have guessed to do that without his knowledge. Awesome Gordon!!! Thank you sir. .... setInterval() is now working.

  • Great! To be honest PC is really the odd one out here, so I guess I should really have ifdef'd based on that.

    jsnative.c is probably the one architecture-specific bit of code in the interpreter. It still amazes me there's no C API to do what jsnative.c does though - it seems like it's a really common thing to want to do.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Internals: Not getting floating point correct

Posted by Avatar for Kolban @Kolban

Actions