{"action":"created","issue":{"url":"https://api.github.com/repos/kripken/emscripten/issues/3369","labels_url":"https://api.github.com/repos/kripken/emscripten/issues/3369/labels{/name}","comments_url":"https://api.github.com/repos/kripken/emscripten/issues/3369/comments","events_url":"https://api.github.com/repos/kripken/emscripten/issues/3369/events","html_url":"https://github.com/kripken/emscripten/issues/3369","id":69413630,"number":3369,"title":"Emulating 64-bit longs","user":{"login":"httpdigest","id":7488613,"avatar_url":"https://avatars.githubusercontent.com/u/7488613?v=3","gravatar_id":"","url":"https://api.github.com/users/httpdigest","html_url":"https://github.com/httpdigest","followers_url":"https://api.github.com/users/httpdigest/followers","following_url":"https://api.github.com/users/httpdigest/following{/other_user}","gists_url":"https://api.github.com/users/httpdigest/gists{/gist_id}","starred_url":"https://api.github.com/users/httpdigest/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/httpdigest/subscriptions","organizations_url":"https://api.github.com/users/httpdigest/orgs","repos_url":"https://api.github.com/users/httpdigest/repos","events_url":"https://api.github.com/users/httpdigest/events{/privacy}","received_events_url":"https://api.github.com/users/httpdigest/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-04-19T11:14:32Z","updated_at":"2015-08-07T06:32:42Z","closed_at":null,"body":"I have a question regarding 64-bit integers. As I understand those have no corresponding data type in JavaScript and a 64-bit floating point double is the closest that we can get there.\r\n\r\nThe Emscripten Wiki http://kripken.github.io/emscripten-site/docs/porting/guidelines/portability_guidelines.html#code-that-compiles-but-might-run-slowly as well as the issue https://github.com/kripken/emscripten/issues/103 mentions 64-bit integer arithmetic being emulated and therefore running slower than native 64-bit integer arithmetic would be had there been support in JavaScript for it and had it been implemented via hardware arithmetic operations in the browsers.\r\n\r\nThe Wiki does not seem to mention, though, that emulation can produce incorrect results or in which cases exactly incorrect results would occur.\r\nOne example would be when printing out LLONG_MAX via printf and %lld. This will give a value that has the least significant three decimal digits rounded to zero.\r\nIs \"printing\" a long value the only issue here whereas arithmetics would still be performed/emulated correctly by Emscripten's generated code?\r\nOr would for large long values computations also yield rounded results because they might be based on double arithmetic?\r\n\r\nSo the question is, in which circumstance would a program observe incorrect results when using long long in C?\r\nI guess, one answer would be \"A program observes an incorrect 64-bit long value whenever that value needs to go trough native JavaScript functions which are not compiled via Emscripten, where it would then be converted to a double.\"\r\nIs this correct?\r\n\r\nGiven this, could fprint'ing a C 64-bit integer value then instead be done by first C-toString'ing that real 64-bit long value and handing that string to the JavaScript native print function?"},"comment":{"url":"https://api.github.com/repos/kripken/emscripten/issues/comments/128611270","html_url":"https://github.com/kripken/emscripten/issues/3369#issuecomment-128611270","issue_url":"https://api.github.com/repos/kripken/emscripten/issues/3369","id":128611270,"user":{"login":"juj","id":225351,"avatar_url":"https://avatars.githubusercontent.com/u/225351?v=3","gravatar_id":"","url":"https://api.github.com/users/juj","html_url":"https://github.com/juj","followers_url":"https://api.github.com/users/juj/followers","following_url":"https://api.github.com/users/juj/following{/other_user}","gists_url":"https://api.github.com/users/juj/gists{/gist_id}","starred_url":"https://api.github.com/users/juj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juj/subscriptions","organizations_url":"https://api.github.com/users/juj/orgs","repos_url":"https://api.github.com/users/juj/repos","events_url":"https://api.github.com/users/juj/events{/privacy}","received_events_url":"https://api.github.com/users/juj/received_events","type":"User","site_admin":false},"created_at":"2015-08-07T06:32:42Z","updated_at":"2015-08-07T06:32:42Z","body":"Tested building this application\r\n\r\n\t#include <stdio.h>\r\n\t#include <stdlib.h>\r\n\t#include <emscripten.h>\r\n\r\n\tunsigned int __attribute__((noinline)) foo() { return emscripten_random() < -10 ? 0 : 5; }\r\n\r\n\tint main()\r\n\t{\r\n\t\tunsigned long long x = 0ull;\r\n\t\tunsigned int n = foo(); //let n be between 0 and 63\r\n\t\tx |= (1ul << (n & 63ul));\r\n\t\tprintf(\"%llu\\n\", x);\r\n\t}\r\n\r\nwith `em++ a.cpp -o a.html -O3`, and it prints out 32 when run.\r\n\r\nMy hunch is that you are dealing with an unaligned 64-bit quantity in your program. Try building with `-s SAFE_HEAP=1` to add assertion checks to detect and abort in such scenarios."}}