With newer versions of emscripten, linker errors surface eariler.
This makes it easier to find function pointer cast errors without
having to execute the bad code path -- the errors happen when the
wasm modules are linked (at load time in the browser)

Anyways, this fixes more linker errors. Mostly the problems have
to do with LAPACK functions that take string arguments. Most
LAPACK functions that take string arguments use them as enums and
only care about the first character of the string. Because of the
way that f2c works, we need to replace these strings with the ascii
code of the first character so we should replace:

sTRSV( 'UPPER', 'NOTRANS', 'NONUNIT', J, H, LDH, Y, 1 )
==>
CALL sTRSV( 85, 78, 78, J, H, LDH, Y, 1 )

where 85 and 78 are the ascii codes of U and N. Various character
variables are subbed into being integer variables. The two
functions `ilaenv` and `xerbla` expect actual C strings as an
argument, but it is very annoying to produce C strings so instead
I added wrapper functions ilaenvf2c and xerblaf2c to clapack and
instead of calling ilaenv and xerbla we call the f2c versions.

Fix scipy linking errors. With newer versions of emscripten, linker errors surface eariler.
This makes it easier to find function pointer cast errors without
having to execute the bad code path -- the errors happen when the
wasm modules are linked (at load time in the browser)

Anyways, this fixes more linker errors. Mostly the problems have
to do with LAPACK functions that take string arguments. Most
LAPACK functions that take string arguments use them as enums and
only care about the first character of the string. Because of the
way that f2c works, we need to replace these strings with the ascii
code of the first character so we should replace:

sTRSV( 'UPPER', 'NOTRANS', 'NONUNIT', J, H, LDH, Y, 1 )
==>
CALL sTRSV( 85, 78, 78, J, H, LDH, Y, 1 )

where 85 and 78 are the ascii codes of U and N. Various character
variables are subbed into being integer variables. The two
functions `ilaenv` and `xerbla` expect actual C strings as an
argument, but it is very annoying to produce C strings so instead
I added wrapper functions ilaenvf2c and xerblaf2c to clapack and
instead of calling ilaenv and xerbla we call the f2c versions.

// Macros to access memory from JavaScript

#define DEREF_U8(addr, offset) HEAPU8[addr + offset]
#define DEREF_I8(addr, offset) HEAP8[addr + offset]

#define DEREF_U16(addr, offset) HEAPU16[(addr >> 1) + offset]
#define DEREF_I16(addr, offset) HEAP16[(addr >> 1) + offset]

#define DEREF_U32(addr, offset) HEAPU32[(addr >> 2) + offset]
#define DEREF_I32(addr, offset) HEAP32[(addr >> 2) + offset]

#define DEREF_F32(addr, offset) HEAPF32[(addr >> 2) + offset]
#define DEREF_F64(addr, offset) HEAPF64[(addr >> 3) + offset]

#define ASSIGN_U32(addr, offset, value) DEREF_U32(addr, offset) = value
#if WASM_BIGINT
// We have HEAPU64 / HEAPI64 in this case.
#define DEREF_U64(addr, offset) HEAPU64[(addr >> 3) + offset]
#define DEREF_I64(addr, offset) HEAP64[(addr >> 3) + offset]

#define LOAD_U64(addr, offset) DEREF_U64(addr, offset)
#define LOAD_I64(addr, offset) DEREF_I64(addr, offset)

#define STORE_U64(addr, offset, val) (DEREF_U64(addr, offset) = val)
#define STORE_I64(addr, offset, val) (DEREF_U64(addr, offset) = val)

#else

// No BigUint64Array, have to manually split / join lower and upper byte
//
#define BIGINT_LOWER(x) (Number((x)&BigInt(0xffffffff)) | 0)
#define BIGINT_UPPER(x) (Number((x) >> BigInt(32)) | 0)
#define UBIGINT_FROM_PAIR(lower, upper)                                        \
  (BigInt(lower) | (BigInt(upper) << BigInt(32)))

#define IBIGINT_FROM_PAIR(lower, upper)                                        \
  (BigInt(lower) | (BigInt(upper - 2 * (upper & 0x80000000)) << BigInt(32)))

#define LOAD_U64(addr, offset)                                                 \
  UBIGINT_FROM_PAIR(DEREF_U32(addr, offset * 2),                               \
                    DEREF_U32(addr, offset * 2 + 1))
#define LOAD_I64(addr, offset)                                                 \
  IBIGINT_FROM_PAIR(DEREF_U32(addr, offset * 2),                               \
                    DEREF_U32(addr, offset * 2 + 1))

#define STORE_U64(addr, offset, val)                                           \
  ((DEREF_U32(addr, offset * 2) = BIGINT_LOWER(val)),                          \
   (DEREF_U32(addr, offset * 2 + 1) = BIGINT_UPPER(val)))
#define STORE_I64 STORE_U64
#endif

// Macros to access memory from JavaScript

#define DEREF_U8(addr, offset) HEAPU8[addr + offset]
#define DEREF_I8(addr, offset) HEAP8[addr + offset]

#define DEREF_U16(addr, offset) HEAPU16[(addr >> 1) + offset]
#define DEREF_I16(addr, offset) HEAP16[(addr >> 1) + offset]

#define DEREF_U32(addr, offset) HEAPU32[(addr >> 2) + offset]
#define DEREF_I32(addr, offset) HEAP32[(addr >> 2) + offset]

#define DEREF_F32(addr, offset) HEAPF32[(addr >> 2) + offset]
#define DEREF_F64(addr, offset) HEAPF64[(addr >> 3) + offset]

#define ASSIGN_U32(addr, offset, value) DEREF_U32(addr, offset) = value
#if WASM_BIGINT
// We have HEAPU64 / HEAPI64 in this case.
#define DEREF_U64(addr, offset) HEAPU64[(addr >> 3) + offset]
#define DEREF_I64(addr, offset) HEAP64[(addr >> 3) + offset]

#define LOAD_U64(addr, offset) DEREF_U64(addr, offset)
#define LOAD_I64(addr, offset) DEREF_I64(addr, offset)

#define STORE_U64(addr, offset, val) (DEREF_U64(addr, offset) = val)
#define STORE_I64(addr, offset, val) (DEREF_U64(addr, offset) = val)

#else

// No BigUint64Array, have to manually split / join lower and upper byte
//
#define BIGINT_LOWER(x) (Number((x)&BigInt(0xffffffff)) | 0)
#define BIGINT_UPPER(x) (Number((x) >> BigInt(32)) | 0)
#define UBIGINT_FROM_PAIR(lower, upper)                                        \
  (BigInt(lower) | (BigInt(upper) << BigInt(32)))

#define IBIGINT_FROM_PAIR(lower, upper)                                        \
  (BigInt(lower) | (BigInt(upper - 2 * (upper & 0x80000000)) << BigInt(32)))

#define LOAD_U64(addr, offset)                                                 \
  UBIGINT_FROM_PAIR(DEREF_U32(addr, offset * 2),                               \
                    DEREF_U32(addr, offset * 2 + 1))
#define LOAD_I64(addr, offset)                                                 \
  IBIGINT_FROM_PAIR(DEREF_U32(addr, offset * 2),                               \
                    DEREF_U32(addr, offset * 2 + 1))

#define STORE_U64(addr, offset, val)                                           \
  ((DEREF_U32(addr, offset * 2) = BIGINT_LOWER(val)),                          \
   (DEREF_U32(addr, offset * 2 + 1) = BIGINT_UPPER(val)))
#define STORE_I64 STORE_U64
#endif
