comp.lang.ada
 help / color / mirror / Atom feed
* GNAT for AVR - Mathematical Functions
@ 2018-09-17  2:22 ada.newcomer
  2018-09-17  7:18 ` Dmitry A. Kazakov
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: ada.newcomer @ 2018-09-17  2:22 UTC (permalink / raw)


Hello.

I'm programming an ATmega328P with the GNAT compiler for AVR.

I need to use the Sqrt, Arctan and Atan2 functions. But maybe in the future I will need to use some more.

I don't have access to the regular Ada.Numerics package.

By now, I wrote the Sqrt using the Newton's method and Arctan using Taylor series.

I would like to know if there is a better way to use/implement mathematical functions (maybe import them from C?).

I really appreciate any help.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
@ 2018-09-17  7:18 ` Dmitry A. Kazakov
  2018-09-17 22:41   ` Aurele Vitali
  2018-09-17 12:22 ` Simon Wright
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2018-09-17  7:18 UTC (permalink / raw)


On 2018-09-17 04:22, ada.newcomer@gmail.com wrote:

> By now, I wrote the Sqrt using the Newton's method and Arctan using Taylor series.

Chebyshev's polynomials for arctan?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
  2018-09-17  7:18 ` Dmitry A. Kazakov
@ 2018-09-17 12:22 ` Simon Wright
  2018-09-17 17:28 ` Bill Findlay
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2018-09-17 12:22 UTC (permalink / raw)


ada.newcomer@gmail.com writes:

> I'm programming an ATmega328P with the GNAT compiler for AVR.
>
> I need to use the Sqrt, Arctan and Atan2 functions. But maybe in the
> future I will need to use some more.
>
> I don't have access to the regular Ada.Numerics package.
>
> By now, I wrote the Sqrt using the Newton's method and Arctan using
> Taylor series.
>
> I would like to know if there is a better way to use/implement
> mathematical functions (maybe import them from C?).
>
> I really appreciate any help.

For what it's worth, the FSF GCC arm-eabi compiler/runtime that I built
imports the basic maths functions from the C library. This may be
because I built the C library (newlib) first?

The GNAT CE arm-eabi compiler goes to basics with very deep-looking
code; possibly because it's "the Ada Cert Math specific version" (from
s-libsin.ads), i.e. one with which AdaCore will support customers with
certification requirements.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
  2018-09-17  7:18 ` Dmitry A. Kazakov
  2018-09-17 12:22 ` Simon Wright
@ 2018-09-17 17:28 ` Bill Findlay
  2018-09-17 22:35 ` Aurele Vitali
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Bill Findlay @ 2018-09-17 17:28 UTC (permalink / raw)


On 17 Sep 2018, ada.newcomer@gmail.com wrote
(in article<ed6bc2e2-a2e1-4a0b-b253-8142d47518c9@googlegroups.com>):

> Hello.
>
> I'm programming an ATmega328P with the GNAT compiler for AVR.
>
> I need to use the Sqrt, Arctan and Atan2 functions. But maybe in the future I
> will need to use some more.
>
> I don't have access to the regular Ada.Numerics package.
>
> By now, I wrote the Sqrt using the Newton's method and Arctan using Taylor
> series.
>
> I would like to know if there is a better way to use/implement mathematical
> functions (maybe import them from C?).
>
> I really appreciate any help.

Since you have sqrt, see:<http://www.findlayw.plus.com/KDF9/#ATN>

-- 
Bill Findlay


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
                   ` (2 preceding siblings ...)
  2018-09-17 17:28 ` Bill Findlay
@ 2018-09-17 22:35 ` Aurele Vitali
  2018-09-18  1:16 ` rakusu_klein
  2018-09-25  7:14 ` R R
  5 siblings, 0 replies; 8+ messages in thread
From: Aurele Vitali @ 2018-09-17 22:35 UTC (permalink / raw)


I don't know anything about the ATmega328P, but if it uses a builtin floating point processor, you can use Ada inline floating point assembler.  Its not hard to do. Here is a simple example of the square root function:

  with System.Machine_Code; use System.Machine_Code;

  function Sqrt( x : in Long_Float ) return Long_Float is
    Result : Long_Float := 0.0;
  begin
    Asm( ( "fldl  %1" & EOL &                 -- Load x in St(0)
           "fsqrt   " & EOL &                 -- Take the Sqrt of St(0)
           "fstpl %0" ),                      -- Store result and pop St(0)
         Outputs => ( Long_Float'Asm_Output("=m", Result ) ),
         Inputs  => ( Long_Float'Asm_Input ( "m",      x ) ) );
    return Result;
  end Sqrt;

You can do the same thing for trig functions...  Just cut and past and see if this example works. 

Cheers

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  7:18 ` Dmitry A. Kazakov
@ 2018-09-17 22:41   ` Aurele Vitali
  0 siblings, 0 replies; 8+ messages in thread
From: Aurele Vitali @ 2018-09-17 22:41 UTC (permalink / raw)


I don't know anything about the ATmega328P, but if it uses a builtin floating point processor, you can use Ada inline floating point assembler.  Its not hard to do. Here is a simple example of the square root function: 

  with System.Machine_Code; use System.Machine_Code; 
  EOL : constant String := ASCII.LF & ASCII.HT;

  function Sqrt( x : in Long_Float ) return Long_Float is 
    Result : Long_Float := 0.0; 
  begin 
    Asm( ( "fldl  %1" & EOL &                 -- Load x in St(0) 
           "fsqrt   " & EOL &                 -- Take the Sqrt of St(0) 
           "fstpl %0" ),                      -- Store result and pop St(0) 
         Outputs => ( Long_Float'Asm_Output("=m", Result ) ), 
         Inputs  => ( Long_Float'Asm_Input ( "m",      x ) ) ); 
    return Result; 
  end Sqrt; 

You can do the same thing for trig functions...  Just cut and past and see if this example works. 

Cheers 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
                   ` (3 preceding siblings ...)
  2018-09-17 22:35 ` Aurele Vitali
@ 2018-09-18  1:16 ` rakusu_klein
  2018-09-25  7:14 ` R R
  5 siblings, 0 replies; 8+ messages in thread
From: rakusu_klein @ 2018-09-18  1:16 UTC (permalink / raw)


It is always be bottleneck — doing math on slow integer 8-bit CPU with tiny amount of memory and instructions only for addition and subtraction, — because it produce a huge pieces of slow machine code. So all math there are doing in integers by a table calculations, adds and shifts. Therefore in future it's better to go away from AVR for you with math, I think.
Btw, there is a quite old C-runtime library for AVR at http://savannah.nongnu.org/projects/avr-libc/ — it might be helpful for you, especially its handwritten libm. It can also be useful to look at CORDIC algorithm, frex http://www.dcs.gla.ac.uk/~jhw/cordic/index.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: GNAT for AVR - Mathematical Functions
  2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
                   ` (4 preceding siblings ...)
  2018-09-18  1:16 ` rakusu_klein
@ 2018-09-25  7:14 ` R R
  5 siblings, 0 replies; 8+ messages in thread
From: R R @ 2018-09-25  7:14 UTC (permalink / raw)


On Monday, September 17, 2018 at 4:22:47 AM UTC+2, Ada Newcomer wrote:
> Hello.
> 
> I'm programming an ATmega328P with the GNAT compiler for AVR.
> 
> I need to use the Sqrt, Arctan and Atan2 functions. But maybe in the future I will need to use some more.
> 
> I don't have access to the regular Ada.Numerics package.
> 
> By now, I wrote the Sqrt using the Newton's method and Arctan using Taylor series.
> 
> I would like to know if there is a better way to use/implement mathematical functions (maybe import them from C?).
> 
> I really appreciate any help.

The old AVR-ADA project never supported floting point math. The AVR 8bit processors are not made for that, even though you can use float and double in Arduino. The AVR compiler by Adacore from around 2011 did support floating point variables as far as I remember, I am not sure about the math functions.

regards
    Rolf

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-09-25  7:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-17  2:22 GNAT for AVR - Mathematical Functions ada.newcomer
2018-09-17  7:18 ` Dmitry A. Kazakov
2018-09-17 22:41   ` Aurele Vitali
2018-09-17 12:22 ` Simon Wright
2018-09-17 17:28 ` Bill Findlay
2018-09-17 22:35 ` Aurele Vitali
2018-09-18  1:16 ` rakusu_klein
2018-09-25  7:14 ` R R

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox