Re: [fpc-devel] Currency * Double

2021-10-25 Thread LacaK via fpc-devel



Hi ,
what data type is result of multiplication of: (Currency * Double) on
Win64 (and on Win32)?
    c:=1115;
    d:=1;
    writeln(Round(c*d)); // gives -72967440737

32 bit version gives always 1115 as a result (and
floating point math should always have extended precision intermeiate
there) . But even with double intermediate result on 64 bit versions
there should not be any problems. 1.1150E+011 is well in
range of that type.


Let's look at disassembly on Win32:
-
(FPU here uses "st registers" 80 bits wide)

test_curr_int64.lpr:77    c1:=1115;
00401EFC a178a14200   mov    0x42a178,%eax
00401F01 a320204300   mov    %eax,0x432020
00401F06 a17ca14200   mov    0x42a17c,%eax
00401F0B a324204300   mov    %eax,0x432024
test_curr_int64.lpr:78    d:=1;
00401F10 a180a14200   mov    0x42a180,%eax
00401F15 a330204300   mov    %eax,0x432030
00401F1A a184a14200   mov    0x42a184,%eax
00401F1F a334204300   mov    %eax,0x432034
test_curr_int64.lpr:79    i1:=Round(c1*d);
00401F24 dd0530204300 fldl   0x432030 // 
st0:=1
00401F2A dc0d08a14200 fmull  0x42a108 // 
st0:=1
00401F30 df2d20204300 fildll 0x432020 // 
st0:=1115, st1:=1
00401F36 dec9 fmulp  %st,%st(1)   // 
st0:=1.115e+019
00401F38 df2d28a04200 fildll 0x42a028 // 
st0:=1, st1:=1.115e+019
00401F3E def9 fdivrp %st,%st(1)   // 
st0:=1115
00401F40 df2d28a04200 fildll 0x42a028 // 
st0:=1, st1:=1115
00401F46 def9 fdivrp %st,%st(1)   // 
st0:=1115

00401F48 df7df8   fistpll -0x8(%ebp)
00401F4B 9b   fwait
00401F4C 8b45f8   mov    -0x8(%ebp),%eax
00401F4F a340204300   mov    %eax,0x432040
00401F54 8b45fc   mov    -0x4(%ebp),%eax
00401F57 a344204300   mov    %eax,0x432044

And now on Win64:
---
test_curr_int64.lpr:77    c1:=1115;
00011EAC 48c70569410400b022aa00   movq 
$0xaa22b0,0x44169(%rip)    # 0x100046020

test_curr_int64.lpr:78    d:=1;
00011EB7 488b05f2f20200   mov 0x2f2f2(%rip),%rax    
# 0x1000311b0 <_$TEST_CURR_INT64$_Ld16>
00011EBE 4889056b410400   mov %rax,0x4416b(%rip)    
# 0x100046030

test_curr_int64.lpr:79    i1:=Round(c1*d);
00011EC5 f20f100563410400 movsd 
0x44163(%rip),%xmm0    // 1
00011ECD f20f590553f20200 mulsd 
0x2f253(%rip),%xmm0    // 1
00011ED5 f2480f2dc8   cvtsd2si 
%xmm0,%rcx   // 1
00011EDA 488b053f410400   mov 0x4413f(%rip),%rax 
// 1115
00011EE1 480fafc8 imul 
%rax,%rcx  // -7296744073709551616 <---OVERFLOW HERE

00011EE5 48b84b598638d6c56d34 movabs $0x346dc5d63886594b,%rax
00011EEF 48f7e9   imul   %rcx
00011EF2 48c1fa0b sar    $0xb,%rdx
00011EF6 48c1e93f shr    $0x3f,%rcx
00011EFA 4801ca   add    %rcx,%rdx
00011EFD 4889d1   mov    %rdx,%rcx
00011F00 e8fb11   callq  0x13100 

00011F05 48890534410400   mov %rax,0x44134(%rip)    
# 0x100046040


If I understand correctly multipllication "imul" is performed as 
multiplication of two currency values (int64)

(1115 * 1 = 1115 * 10^16) which is over Max Int64 ...
So on Win64 there is currency (int64) intermediate ...

L.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Currency * Double

2021-10-23 Thread Virgo Pärna via fpc-devel
On Fri, 22 Oct 2021 08:42:57 +0200, LacaK via fpc-devel 
 wrote:
> Hi ,
> what data type is result of multiplication of: (Currency * Double) on 
> Win64 (and on Win32)?
>    c:=1115;
>    d:=1;
>    writeln(Round(c*d)); // gives -72967440737

32 bit version gives always 1115 as a result (and
floating point math should always have extended precision intermeiate
there) . But even with double intermediate result on 64 bit versions 
there should not be any problems. 1.1150E+011 is well in 
range of that type.


-- 
Virgo Pärna 
virgo.pa...@mail.ee

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


[fpc-devel] Currency * Double

2021-10-22 Thread LacaK via fpc-devel

Hi ,
what data type is result of multiplication of: (Currency * Double) on 
Win64 (and on Win32)?

  c:=1115;
  d:=1;
  writeln(Round(c*d)); // gives -72967440737

I suppose that Currency*Double is Currency (on Win64) and result 
overflows MaxCurrency ...?
(but then how FPC determines result data type?; On Win32 there is result 
Extended?)


TIA
-Laco.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel