ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

AP_Scripting: same some flash, allow init unint32 from negative numbers

Open IamPete1 opened this issue 3 years ago • 2 comments

This allows initialization of unit32 from negative numbers, that allows us to pass negative inits that can be represented by lua ints instead of large positive numbers that loose precision as they are represented as lua floats.

Also saves some flash by re-using the div function for idiv and reusing the auto generated binding arg checker.

IamPete1 avatar Aug 10 '22 00:08 IamPete1

The motivator appears to be some oddities with using literals in scripts, aggravated by rejecting negative integer values. Given the need to have useful literals it might be better to just remove this negative check for uint32_t boxed types.

WickedShell avatar Aug 10 '22 01:08 WickedShell

Example test script:

  local f = uint32_t(4157483647)
  gcs:send_text(0, string.format("uint32_t(4157483647):toint() = %i", f:toint()) .. " string: " .. tostring(f))

  local f = uint32_t(10472251) * uint32_t(397)
  gcs:send_text(0, string.format("(uint32_t(10472251) * uint32_t(397)):toint() = %i", f:toint()) .. " string: " .. tostring(f))


  local f = uint32_t(-137483649)
  gcs:send_text(0, string.format("uint32_t(-137483649):toint() = %i", f:toint()) .. " string: " .. tostring(f))

IamPete1 avatar Aug 10 '22 22:08 IamPete1

Binary Name      Text [B]         Data [B]     BSS (B)        Total Flash Change [B] (%)      Flash Free After PR (B)
---------------  ---------------  -----------  -------------  ----------------------------  -------------------------
antennatracker   -988 (-0.0729%)  0 (0.0000%)  -4 (-0.0015%)  -988 (-0.0728%)                                  610524
arducopter-heli  -988 (-0.0553%)  0 (0.0000%)  -4 (-0.0015%)  -988 (-0.0552%)                                  175748
ardurover        -988 (-0.0615%)  0 (0.0000%)  4 (+0.0015%)   -988 (-0.0615%)                                  359660
arducopter       -988 (-0.0555%)  0 (0.0000%)  4 (+0.0015%)   -988 (-0.0554%)                                  182956
ardusub          -988 (-0.0631%)  0 (0.0000%)  -4 (-0.0015%)  -988 (-0.0630%)                                  398760
blimp            -988 (-0.0762%)  0 (0.0000%)  4 (+0.0015%)   -988 (-0.0761%)                                  668376
arduplane        -988 (-0.0560%)  0 (0.0000%)  4 (+0.0015%)   -988 (-0.0559%)                                  200196

IamPete1 avatar Aug 12 '22 09:08 IamPete1

we could check if the constructor has 2 arguments we can do: local foo = uint32_t(0xF000, 0x0007) that would create 0xF0000007

tridge avatar Aug 16 '22 01:08 tridge

Dropped controversial signed vs unsigned thing, now just the flash savings.

I will investigate the cost of moving to doubles in lua vs keeping uint32.

IamPete1 avatar Aug 16 '22 12:08 IamPete1

we could check if the constructor has 2 arguments we can do: local foo = uint32_t(0xF000, 0x0007) that would create 0xF0000007

Why not accept 0xFFFFFFFF directly? The compiler will turn it into -1 but that will be an integer literal? That's how normal lua code works at that point, and is far clearer...

WickedShell avatar Aug 16 '22 16:08 WickedShell