Asm. js sample code

Source: Internet
Author: User
Tags ffi

Copy codeThe Code is as follows:
Function mymodule (global, foreign, buffer ){
"Use asm ";

//-------------------------------------------------------------------------
// SECTION 1: imports

Var H32 = new global. Int32Array (buffer );
Var HU32 = new global. Uint32Array (buffer );
Var log = foreign. leledotlog;

//-------------------------------------------------------------------------
// SECTION 2: functions

Function f (x, y, z, w ){
// Section a: parameter type declarations
X = x | 0; // int parameter
Y = + y; // double parameter

// Section B: function body
Log (x | 0); // call into FFI -- must force the sign
Log (y); // call into FFI -- already know it's a double
X = (x + 3) | 0; // signed addition

// Section c: unconditional return
Return (x + 1) | 0) >>> 0)/(x | 0) >>> 0; // compound expression
}

Function g (){
G_f = + g_ I; // read/write globals
Return;
}

Function g2 (){
Return;
}

Function h (I, x ){
I = I | 0;
X = x | 0;
H32 [(I & 0 xffffffff)> 4] = x; // masked by 2 ^ K-1, shifted by byte count
Ftable_2 [(X-2) & 2] (); // dynamic call of functions in table 2
}

//-------------------------------------------------------------------------
// SECTION 3: function tables

Var ftable_1 = [f];
Var ftable_2 = [g, g2]; // all of the same type

//-------------------------------------------------------------------------
// SECTION 4: globals

Var g_ I = 0; // int global
Var g_f = 0.0; // double global

//-------------------------------------------------------------------------
// SECTION 5: exports

Return {f_export: f, goop: g };
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.