Re: [Interest] armv7a-hard-float in Qt android apps

2016-05-01 Thread Thiago Macieira
On sábado, 30 de abril de 2016 10:46:21 PDT Jean-Michaël Celerier wrote:
> https://godbolt.org/g/DqBlFG
> 
> With gcc -O1
> 
> float f(float x)
> {
>   return 2. * x;
> }
> 
> becomes
> 
> f(float):
> addss   %xmm0, %xmm0
> ret
> 
> 
> and
> 
> float g (float x)
> {
>   return 2.f * x;
> }
> 
> becomes
> 
> g(float):
> addss   %xmm0, %xmm0
> ret

Because 2 can be represented with no loss of precision in both single- and 
double-precision floating point. The compiler actually produced an "as-if" 
optimisation here.

Try multiplying by something different, something that isn't precise, like 
2.1.

f:
cvtss2sd%xmm0, %xmm0
mulsd   .LC0(%rip), %xmm0
cvtsd2ss%xmm0, %xmm0
ret


https://godbolt.org/g/4yEj4Y

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-30 Thread Elvis Stansvik
2016-04-30 11:51 GMT+02:00 Ola Røer Thorsen :
>
> 2016-04-30 10:46 GMT+02:00 Jean-Michaël Celerier
> :
>>
>> https://godbolt.org/g/DqBlFG
>>
>> With gcc -O1
>>
>> float f(float x)
>> {
>>   return 2. * x;
>> }
>>
>> becomes
>>
>> f(float):
>> addss   %xmm0, %xmm0
>> ret
>>
>>
>> and
>>
>> float g (float x)
>> {
>>   return 2.f * x;
>> }
>>
>> becomes
>>
>> g(float):
>> addss   %xmm0, %xmm0
>> ret
>>
>
> This is on x86. I was talking about ARM with single-precision FPUs and the
> general advice not to use double precision constants if possible there.

Also, Jean-Michaël's example is a little unfortunate since the
compiler is smart enough to convert the multiplication by 2 to an
addss instruction. Changing the constant to 2.1 is more interesting
(https://godbolt.org/g/Cgw2cR):

f(float):
cvtss2sd%xmm0, %xmm0
mulsd   .LC0(%rip), %xmm0
cvtsd2ss%xmm0, %xmm0
ret
g(float):
mulss   .LC1(%rip), %xmm0
ret
.LC0:
.long   3435973837
.long   1073794252
.LC1:
.long   1074161254

So conversion to double-precision and then back to single-precision in
the code for f.

For reference, here's the result with ARM GCC 4.5.3:
https://godbolt.org/g/r7Kf1H

Elvis

>
> This is what GCC itself says in the documentation for -Wdouble-promotion:
>
> -Wdouble-promotion (C, C++, Objective-C and Objective-C++ only)Give a
> warning when a value of type float is implicitly promoted to double. CPUs
> with a 32-bit “single-precision” floating-point unit implement float in
> hardware, but emulate double in software. On such a machine, doing
> computations using double values is much more expensive because of the
> overhead required for software emulation.
>
> It is easy to accidentally do computations with double because
> floating-point literals are implicitly of type double. For example, in:
>
>   float area(float radius)
>   {
>  return 3.14159 * radius * radius;
>   }
>
> the compiler performs the entire computation with double because the
> floating-point literal is a double.
>
>
>
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-30 Thread Ola Røer Thorsen
2016-04-30 10:46 GMT+02:00 Jean-Michaël Celerier <
jeanmichael.celer...@gmail.com>:

> https://godbolt.org/g/DqBlFG
>
> With gcc -O1
>
> float f(float x)
> {
>   return 2. * x;
> }
>
> becomes
>
> f(float):
> addss   %xmm0, %xmm0
> ret
>
>
> and
>
> float g (float x)
> {
>   return 2.f * x;
> }
>
> becomes
>
> g(float):
> addss   %xmm0, %xmm0
> ret
>
>
This is on x86. I was talking about ARM with single-precision FPUs and the
general advice not to use double precision constants if possible there.

This is what GCC itself says in the documentation for -Wdouble-promotion:

-Wdouble-promotion (C, C++, Objective-C and Objective-C++ only)Give a
warning when a value of type float is implicitly promoted to double. CPUs
with a 32-bit “single-precision” floating-point unit implement float in
hardware, but emulate double in software. On such a machine, doing
computations using double values is much more expensive because of the
overhead required for software emulation.

It is easy to accidentally do computations with double because
floating-point literals are implicitly of type double. For example, in:

  float area(float radius)
  {
 return 3.14159 * radius * radius;
  }

the compiler performs the entire computation with double because the
floating-point literal is a double.
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-30 Thread Jean-Michaël Celerier
https://godbolt.org/g/DqBlFG

With gcc -O1

float f(float x)
{
  return 2. * x;
}

becomes

f(float):
addss   %xmm0, %xmm0
ret


and

float g (float x)
{
  return 2.f * x;
}

becomes

g(float):
addss   %xmm0, %xmm0
ret


On Fri, Apr 29, 2016 at 6:30 PM, Ola Røer Thorsen 
wrote:

>
>
> 2016-04-29 16:36 GMT+02:00 Jean-Michaël Celerier <
> jeanmichael.celer...@gmail.com>:
>
>>
>> On Fri, Apr 29, 2016 at 1:55 PM, Ola Røer Thorsen 
>> wrote:
>>
>>>
>>> float a = 1.0f;
>>> float b = 2.0*a; // BAD!
>>> float b = 2.0f*a; // Good!
>>>
>>
>> Pretty sure that this would be a non-problem starting at -O1 optimization
>> level.
>>
>
> Well you're wrong. If you multiply with a double precision constant value
> (2.0), the multiplication is done in double precision and the result is
> then converted to single precision, regardless of the optimize level. This
> makes a big difference on hardware that only support single-precision in
> hardware (I know this from experience, not assumptions).
>
>
>
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Nuno Santos
Ok Thiago, many thanks! 

> On 29 Apr 2016, at 17:35, Thiago Macieira  wrote:
> 
> On sexta-feira, 29 de abril de 2016 17:19:03 PDT Nuno Santos wrote:
>>> So I have to ask:
>>> a) are you sure your system wasn't already hard-float?
>> 
>> I don’t have a clue
>> 
>>> b) if it wasn't, are the hard-float libs for EVERYTHING available on the 
>>> 
>>>  system? And does the lib loader know how to find them?
>> 
>> I don’t have a clue
> 
> Then I recommend not pursuing this line of development until you find out 
> more.
> 
> -- 
> Thiago Macieira - thiago.macieira (AT) intel.com
>  Software Architect - Intel Open Source Technology Center
> 
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Thiago Macieira
On sexta-feira, 29 de abril de 2016 17:19:03 PDT Nuno Santos wrote:
> > So I have to ask:
> > a) are you sure your system wasn't already hard-float?
> 
> I don’t have a clue
> 
> > b) if it wasn't, are the hard-float libs for EVERYTHING available on the 
> >
> >   system? And does the lib loader know how to find them?
> 
> I don’t have a clue

Then I recommend not pursuing this line of development until you find out more.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Ola Røer Thorsen
2016-04-29 16:36 GMT+02:00 Jean-Michaël Celerier <
jeanmichael.celer...@gmail.com>:

>
> On Fri, Apr 29, 2016 at 1:55 PM, Ola Røer Thorsen 
> wrote:
>
>>
>> float a = 1.0f;
>> float b = 2.0*a; // BAD!
>> float b = 2.0f*a; // Good!
>>
>
> Pretty sure that this would be a non-problem starting at -O1 optimization
> level.
>

Well you're wrong. If you multiply with a double precision constant value
(2.0), the multiplication is done in double precision and the result is
then converted to single precision, regardless of the optimize level. This
makes a big difference on hardware that only support single-precision in
hardware (I know this from experience, not assumptions).
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Nuno Santos
Thiago,

> On 29 Apr 2016, at 17:12, Thiago Macieira  wrote:
> 
> So I have to ask:
> a) are you sure your system wasn't already hard-float?

I don’t have a clue

> b) if it wasn't, are the hard-float libs for EVERYTHING available on the 
>   system? And does the lib loader know how to find them?

I don’t have a clue

I was able to compile my code with the hard-float settings but it doesn’t seem 
to speed up. For the contrary. I think it is slowing down.

This is unknown territory for me and I don’t know what I am doing.

Nuno___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Thiago Macieira
On sexta-feira, 29 de abril de 2016 14:59:35 PDT Nuno Santos wrote:
> While Qt code may be compiled with support for hard float, the qmake.specs
> file for android doesn’t show that and, in general, it seems that hard
> float support is disabled by default in android:

Unless you're recompiling ALL of Android, you can't change the option. If you 
turn on hard float on a system that uses soft float parameter passing, all 
function calls that take or return floating point will fail in unpredictable 
ways.

So I have to ask:
a) are you sure your system wasn't already hard-float?
b) if it wasn't, are the hard-float libs for EVERYTHING available on the 
   system? And does the lib loader know how to find them?

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Thiago Macieira
On sexta-feira, 29 de abril de 2016 13:55:05 PDT Ola Røer Thorsen wrote:
> As far as I know Qt is built with qreal as double as a default setting.

qreal is double.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Jean-Michaël Celerier
On Fri, Apr 29, 2016 at 1:55 PM, Ola Røer Thorsen 
wrote:

>
> float a = 1.0f;
> float b = 2.0*a; // BAD!
> float b = 2.0f*a; // Good!
>

Pretty sure that this would be a non-problem starting at -O1 optimization
level.
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Nuno Santos
Ola,

Thanks for your reply.

While Qt code may be compiled with support for hard float, the qmake.specs file 
for android doesn’t show that and, in general, it seems that hard float support 
is disabled by default in android:

QMAKE_CFLAGS = -Wno-psabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfp 
-ffunction-sections -funwind-tables -fstack-protector -fno-short-enums 
-DANDROID -Wa,--noexecstack -fno-builtin-memmove

I have found some questions/answer around the internet suggesting the following:

QMAKE_CFLAGS = -Wno-psabi -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 
-mhard-float -ffunction-sections -funwind-tables -fstack-protector 
-fno-short-enums -DANDROID -D_NDK_MATH_NO_SOFTFP=1 -Wa,--noexecstack 
-fno-builtin-memmove

How can I have sure that it is in fact enabled?

Regards,

Nuno Santos
Founder / CEO / CTO
www.imaginando.pt
+351 91 621 69 62

> On 29 Apr 2016, at 12:55, Ola Røer Thorsen  wrote:
> 
> 2016-04-29 10:11 GMT+02:00 Nuno Santos  >:
> 
> Code compiled and ruined fine but couldn’t notice a faster performance (I 
> have profiling timers in critical parts of the application).
> 
> Has anyone done this before? 
> 
> 
> As far as I know Qt is built with qreal as double as a default setting. 
> 
> In case your cpu only does single precision floats in hardware:
> 
> Maybe try to build Qt with qreal as single precision floats?
> I think the Qt configure option is "-qreal float", please check to be sure. 
> 
> Makes a notable difference on my ancient arm omap3 processor, at least. 
> 
> Also I'd recommend using the gcc warnings
> -Wdouble-promotion
> -Wfloat-conversion
> and make sure you are just using single precision floats as much as possible. 
> Lots of performance is lost if you keep mixing single- and double precision. 
> 
> float a = 1.0f;
> float b = 2.0*a; // BAD!
> float b = 2.0f*a; // Good!
> 
> Make sure you are using single-precision math functions (use std::sin, 
> std::abs, not cmath sin, abs, etc). The warnings above will show you all 
> these cases. 
> 
> Cheers,
> Ola
> 
> 
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Ola Røer Thorsen
2016-04-29 10:11 GMT+02:00 Nuno Santos :

>
> Code compiled and ruined fine but couldn’t notice a faster performance (I
> have profiling timers in critical parts of the application).
>
> Has anyone done this before?
>
>
As far as I know Qt is built with qreal as double as a default setting.

In case your cpu only does single precision floats in hardware:

Maybe try to build Qt with qreal as single precision floats?
I think the Qt configure option is "-qreal float", please check to be sure.

Makes a notable difference on my ancient arm omap3 processor, at least.

Also I'd recommend using the gcc warnings
-Wdouble-promotion
-Wfloat-conversion
and make sure you are just using single precision floats as much as
possible. Lots of performance is lost if you keep mixing single- and double
precision.

float a = 1.0f;
float b = 2.0*a; // BAD!
float b = 2.0f*a; // Good!

Make sure you are using single-precision math functions (use std::sin,
std::abs, not cmath sin, abs, etc). The warnings above will show you all
these cases.

Cheers,
Ola
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


[Interest] armv7a-hard-float in Qt android apps

2016-04-29 Thread Nuno Santos
Hi,

I’m trying to enable hard float computation on arm.

I think I have managed to enable it doing the following changes:

1) Set a new environment var ANDROID_ARCH_TARGET=armeabi-v7a-hard
2) Adding 

equals(ANDROID_TARGET_ARCH, armeabi-v7a-hard): \
QMAKE_CFLAGS = -Wno-psabi -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 
-mhard-float -ffunction-sections -funwind-tables -fstack-protector 
-fno-short-enums -DANDROID -D_NDK_MATH_NO_SOFTFP=1 -Wa,--noexecstack 
-fno-builtin-memmove

3) QMAKE_LFLAGS= --sysroot=$$ANDROID_PLATFORM_ROOT_PATH 
-Wl,--no-warn-mismatch -lm_hard
4) QMAKE_LIBS_PRIVATE  = -lgnustl_shared -llog -lz -lm_hard -ldl -lc -lgcc

Code compiled and ruined fine but couldn’t notice a faster performance (I have 
profiling timers in critical parts of the application).

Has anyone done this before? 

Regards,

Nuno___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest