Author Topic: Complex numbers: problem with structure initialisation  (Read 177 times)

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Complex numbers: problem with structure initialisation
« on: August 14, 2017, 07:08:21 PM »
From Raymond Filiatreault's zlib:

Code: [Select]
the real and imaginary coefficients must be at adjacent memory addresses, the real coefficient being at the lower
address. Although structure addressing is not used within the functions, and structures are not an absolute necessity, a
complex number structure could be declared as:

Z_NUM STRUCT
real :DWORD (or QWORD, or TBYTE)
imag :DWORD (or QWORD, or TBYTE)
Z_NUM ENDS

and then for the variable in your data section:

z1 Z_NUM <>

I've tried that for OWORDs aka REAL16, and ran into a problem:
Code: [Select]
include \masm32\include\masm32rt.inc
.686
.xmm

COMPLEX16 STRUCT
real OWORD ?
imag OWORD ?
COMPLEX16 ENDS

.code
MyF16 OWORD ? ; works fine
MyX16 COMPLEX16 <-123, 456> ; chokes with A1016: Internal Assembler Error for ML 6.14 and 6.15

start:
  movups xmm0, MyX16.real
  movd eax, xmm0
  print str$(eax), " "
  movups xmm1, MyX16.imag
  movd eax, xmm1
  inkey str$(eax), "i"
  exit
end start

Works fine with UAsm and ML 8.0+. Any workaround that would satisfy my desperate attempt to remain compatible with ML 6.15?

Siekmanski

  • Member
  • *****
  • Posts: 1094
Re: Complex numbers: problem with structure initialisation
« Reply #1 on: August 14, 2017, 10:31:39 PM »
Does Masm 6.14 and 6.15 recognize SIMD instructions?
AFAIK they don't.
Maybe you can use SIMD macros for 6.14 an 6.15.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4814
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Complex numbers: problem with structure initialisation
« Reply #2 on: August 14, 2017, 11:24:15 PM »
If you want to use later instructions that did not exist before 2000, use a later version.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Re: Complex numbers: problem with structure initialisation
« Reply #3 on: August 15, 2017, 12:22:21 AM »
Normally my stuff requires ML 6.15, and yes, that includes SIMD. ML 6.15 knows what an OWORD is (but it doesn't know about XMMWORD). However, it can't use the OWORD inside a structure definition - that is only possible from version 8.0 onwards.

Queue

  • Regular Member
  • *
  • Posts: 13
Re: Complex numbers: problem with structure initialisation
« Reply #4 on: August 15, 2017, 04:52:06 AM »
Depending on circumstances, sometimes the old MASM OWORD struct issue can be worked around with unions or QWORD 2 dup(?) substitutions.

For example:
Code: [Select]
COMPLEX16 STRUCT
union
_real QWORD ?
real OWORD ?
ends
union
_imag QWORD ?
imag OWORD ?
ends
COMPLEX16 ENDS
.errnz COMPLEX16 - OWORD * 2

MyX16 COMPLEX16 <{-123},{456}>
Edit: Note that this specific workaround isn't going to get negative numbers right (they'll only populate the QWORD's width).

The OWORD issue is screwy specifically because it is a bug.
Code: [Select]
_dummy1 struct
dummy0 OWORD ?
dummy1 OWORD ?
dummy2 OWORD ?
dummy3 QWORD ?
dummy4 QWORD ?
dummy5 QWORD ?
dummy6 QWORD ?
dummy7 QWORD ?
_dummy1 ends
_dummy2 struct
_dummy1 <>
_dummy2 ends
Change QWORDs to OWORDs in this example and note the internal assembler errors thrown for the line "_dummy1 <>" (via MASM 6.15).

Queue
« Last Edit: August 15, 2017, 06:52:53 AM by Queue »

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Re: Complex numbers: problem with structure initialisation
« Reply #5 on: August 15, 2017, 06:57:16 AM »
Depending on circumstances, sometimes the old MASM OWORD struct issue can be worked around with unions

It seems we have an expert among us - thanks a lot :t

Code: [Select]
include \masm32\include\masm32rt.inc
.686p
.xmm

COMPLEX16 STRUCT
union
dummy1 QWORD ?, ?
real OWORD ?
ends
union
dummy2 QWORD ?, ?
imag OWORD ?
ends
COMPLEX16 ENDS
; .errnz COMPLEX16 - OWORD * 2

.data
My16 COMPLEX16 <>

.code
q0 dq 5555666677778888h, 1111222233334444h
q1 dq 1212121212121212h, 3434343434343434h
start:
   lea eax, q0
   lea edx, My16
   ; int 3
   movups xmm0, oword ptr q0
   movups xmm1, oword ptr q1
   movups My16.real, xmm0
   movups My16.imag, xmm1
   inkey "ok?"
   exit
end start


P.S.: There is an old thread with similar problems.

Queue

  • Regular Member
  • *
  • Posts: 13
Re: Complex numbers: problem with structure initialisation
« Reply #6 on: August 15, 2017, 07:53:09 AM »
Some other old threads with potentially useful information:
http://www.masmforum.com/board/index.php?topic=2610.0
http://www.masmforum.com/board/index.php?topic=15872.0
You were a major part of that second one. The first one points out using the label directive to allow more freeform structure initialization; could probably make a macro for OWORD and OWORD-containing structure initialization if you're intent on supporting MASM 6.X.

Queue

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Re: Complex numbers: problem with structure initialisation
« Reply #7 on: August 15, 2017, 08:41:14 AM »
Thanks, Queue. The best (and only) solution seems to be the union with two dummy QWORDs. Weird stuff, really.