24 bits (u)ints

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

24 bits (u)ints

Diogo Martins Silva
Hello all.

The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
(u)ints as an extension since version 4.7. How do I use them?

Thanks

Diogo


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

Senthil Kumar Selvaraj

Diogo Martins Silva writes:

> Hello all.
>
> The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
> (u)ints as an extension since version 4.7. How do I use them?

Use __uint24 as the type. Like so

$ cat test.c
volatile __uint24 x;
int main() {
  x++;
  x--;
}

$ avr-gcc -S test.c -Os

$ cat test.s
        .file "test.c"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
        .section .text.startup,"ax",@progbits
.global main
        .type main, @function
main:
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
        lds r24,x
        lds r25,x+1
        lds r26,x+2
        adiw r24,1
        adc r26,__zero_reg__
        sts x,r24
        sts x+1,r25
        sts x+2,r26
        lds r24,x
        lds r25,x+1
        lds r26,x+2
        sbiw r24,1
        sbc r26,__zero_reg__
        sts x,r24
        sts x+1,r25
        sts x+2,r26
        ret
        .size main, .-main
        .comm x,3,1
        .ident "GCC: (AVR_8_bit_GNU_Toolchain_3.5.4_1709) 4.9.2"
.global __do_clear_bss

Regards
Senthil

>
> Thanks
>
> Diogo
>
>
> _______________________________________________
> AVR-GCC-list mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/avr-gcc-list


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

Georg-Johann Lay-2
In reply to this post by Diogo Martins Silva
On 30.11.2016 08:40, Diogo Martins Silva wrote:
> Hello all.
>
> The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
> (u)ints as an extension since version 4.7. How do I use them?
>
> Thanks
>
> Diogo

You can use it basically like any other integer type, for example:


__uint24 mul24 (unsigned char a, unsigned int b)
{
     return (__uint24) a * b;
}

__int24 get_val24 (const __flash __int24 vals[], unsigned char pos)
{
     return vals[pos];
}

For a MCU with MUL, this gives

mul24:
        mul r24,r22
        movw r18,r0
        mul r24,r23
        add r19,r0
        mov r20,r1
        clr __zero_reg__
        adc r20,__zero_reg__
        mov r24,r20
        movw r22,r18
        ret

get_val24:
        ldi r18,lo8(3)
        mul r22,r18
        add r24,r0
        adc r25,r1
        clr __zero_reg__
        movw r30,r24
        lpm r22,Z+
        lpm r23,Z+
        lpm r24,Z
        ret

When you are passing a 24-bit value to a variadic function like printf,
you have to cast it to (un)signed long by hand; avr-gcc does not
automatically promote such types to 32-bit types.  printf et al. have no
format specifiers for 24-bit types.

Johann


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

Paul "LeoNerd" Evans
In reply to this post by Senthil Kumar Selvaraj
On Wed, 30 Nov 2016 18:18:11 +0530
Senthil Kumar Selvaraj <[hidden email]> wrote:

> Diogo Martins Silva writes:
>
> > Hello all.
> >
> > The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
> > (u)ints as an extension since version 4.7. How do I use them?  
>
> Use __uint24 as the type. Like so
>
> $ cat test.c
> volatile __uint24 x;
> int main() {
>   x++;
>   x--;
> }
Is it possible to get that added to <stdint.h> as the expected names

  int24_t
  uint24_t

?

--
Paul "LeoNerd" Evans

[hidden email]      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

attachment0 (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

David Kelly
On Nov 30, 2016, at 8:32 AM, Paul LeoNerd Evans <[hidden email]> wrote:

> Is it possible to get that added to <stdint.h> as the expected names
>
>  int24_t
>  uint24_t

I am opposed because 24 is not a standard int.
stdint.h standardizes types across compilers.

--
David Kelly N4HHE, [hidden email]
============================================================
Whom computers would destroy, they must first drive mad.


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

MortenEngelhardt.Olsen
In reply to this post by Diogo Martins Silva
Extensions => Types => Signed and unsigned 24-bit integers: __int24 (4.7), __uint24 (4.7).

int main(void)
{
    volatile __uint24 a = 0;
}

Gives
    volatile __uint24 a = 0;
  58: 19 82       std Y+1, r1 ; 0x01
  5a: 1a 82       std Y+2, r1 ; 0x02
  5c: 1b 82       std Y+3, r1 ; 0x03


 :: Morten

-----Original Message-----
From: AVR-GCC-list [mailto:avr-gcc-list-bounces+meolsen=[hidden email]] On Behalf Of Diogo Martins Silva
Sent: 30. november 2016 08:41
To: [hidden email]
Subject: [avr-gcc-list] 24 bits (u)ints

Hello all.

The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits (u)ints as an extension since version 4.7. How do I use them?

Thanks

Diogo


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

Georg-Johann Lay-2
In reply to this post by Paul "LeoNerd" Evans
On 30.11.2016 15:32, Paul "LeoNerd" Evans wrote:

> On Wed, 30 Nov 2016 18:18:11 +0530
> Senthil Kumar Selvaraj <[hidden email]> wrote:
>
>> Diogo Martins Silva writes:
>>
>>> Hello all.
>>>
>>> The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
>>> (u)ints as an extension since version 4.7. How do I use them?
>>
>> Use __uint24 as the type. Like so
>>
>> $ cat test.c
>> volatile __uint24 x;
>> int main() {
>>   x++;
>>   x--;
>> }
>
> Is it possible to get that added to <stdint.h> as the expected names
>
>   int24_t
>   uint24_t
>
> ?

The C99 and later standards don't supply these types or reserve these
identifiers.  Since C99, "int8_t" is an identifier that resides in the
namespace of the implementation, whereas "int24_t" is still in the
namespace of the application.  Hence, for the new 3-byte types names
were chosen that are in the namespace of the implementation, i.e.
reserved identifiers which start with "__" (__int24 and __uint24).

If you like you can define such types in your application, but they
should not go into any standard header.

Johann



_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

Diogo Martins Silva
In reply to this post by Georg-Johann Lay-2
Thanks, guys.

I asked because I tried a code snippet, but was getting errors.

I found later that the errors came from my IDE's code analysis, not the
compiler. The code works fine.

Thumbs up for the assembly examples!

Diogo


On Wed, 2016-11-30 at 14:14 +0100, Georg-Johann Lay wrote:

> On 30.11.2016 08:40, Diogo Martins Silva wrote:
> > Hello all.
> >
> > The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
> > (u)ints as an extension since version 4.7. How do I use them?
> >
> > Thanks
> >
> > Diogo
>
> You can use it basically like any other integer type, for example:
>
>
> __uint24 mul24 (unsigned char a, unsigned int b)
> {
>      return (__uint24) a * b;
> }
>
> __int24 get_val24 (const __flash __int24 vals[], unsigned char pos)
> {
>      return vals[pos];
> }
>
> For a MCU with MUL, this gives
>
> mul24:
> mul r24,r22
> movw r18,r0
> mul r24,r23
> add r19,r0
> mov r20,r1
> clr __zero_reg__
> adc r20,__zero_reg__
> mov r24,r20
> movw r22,r18
> ret
>
> get_val24:
> ldi r18,lo8(3)
> mul r22,r18
> add r24,r0
> adc r25,r1
> clr __zero_reg__
> movw r30,r24
> lpm r22,Z+
> lpm r23,Z+
> lpm r24,Z
> ret
>
> When you are passing a 24-bit value to a variadic function like printf,
> you have to cast it to (un)signed long by hand; avr-gcc does not
> automatically promote such types to 32-bit types.  printf et al. have no
> format specifiers for 24-bit types.
>
> Johann
>



_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

David Brown-4
In reply to this post by Georg-Johann Lay-2
On 30/11/16 17:07, Georg-Johann Lay wrote:

> On 30.11.2016 15:32, Paul "LeoNerd" Evans wrote:
>> On Wed, 30 Nov 2016 18:18:11 +0530
>> Senthil Kumar Selvaraj <[hidden email]> wrote:
>>
>>> Diogo Martins Silva writes:
>>>
>>>> Hello all.
>>>>
>>>> The avr-gcc wiki (https://gcc.gnu.org/wiki/avr-gcc) lists 24 bits
>>>> (u)ints as an extension since version 4.7. How do I use them?
>>>
>>> Use __uint24 as the type. Like so
>>>
>>> $ cat test.c
>>> volatile __uint24 x;
>>> int main() {
>>>   x++;
>>>   x--;
>>> }
>>
>> Is it possible to get that added to <stdint.h> as the expected names
>>
>>   int24_t
>>   uint24_t
>>
>> ?
>
> The C99 and later standards don't supply these types or reserve these
> identifiers.  Since C99, "int8_t" is an identifier that resides in the
> namespace of the implementation, whereas "int24_t" is still in the
> namespace of the application.  Hence, for the new 3-byte types names
> were chosen that are in the namespace of the implementation, i.e.
> reserved identifiers which start with "__" (__int24 and __uint24).
>
> If you like you can define such types in your application, but they
> should not go into any standard header.
>
> Johann

Actually, names such as int24_t and uint24_t /are/ reserved for the
implementation to use in <stdint.h>.  See 7.31.10 of the C11 standards
under "Future library directions".  (I don't know if the same applies to
C99 - I don't have that standard conveniently on hand.)  Types int24_t
and uint24_t are optional, even if the compiler supports them as an
integer type (unlike the 8, 16, 32 and 64 bit types).

However, if int24_t and uint24_t are defined in <stdint.h>, then the
implementation must also define printf and scanf format specifier macros
PRId24, PRIi24, etc., in <inttypes.h>.  This in turn implies that printf
and scanf must support the types.  These can be avoided by classifying
avr-gcc as a "freestanding" compiler rather than a "hosted" compiler,
but I believe the norm in gcc is that an integer type only goes in
<stdint.h> if it is completely supported throughout the compiler and
library, including printf support.

So in gcc on 64-bit systems, the __int128 and __uint128 types don't have
printf support, nor is there any way to make literals of these types
(assuming the target does not have 128-bit long long's) - thus they are
not considered "extended integer types" and don't have corresponding
<stdint.h> types.

For 24-bit integers in avr-gcc, it /would/ be possible to make a
conforming freestanding C implementation with the types defined in
<stdint.h> - but it would be inconsistent with other ports of gcc and
with the printf in the library, and therefore probably not a good idea.

mvh.,

David




_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 24 bits (u)ints

David Brown-4
In reply to this post by Diogo Martins Silva
On 30/11/16 23:08, Diogo Martins Silva wrote:
> Thanks, guys.
>
> I asked because I tried a code snippet, but was getting errors.
>
> I found later that the errors came from my IDE's code analysis, not the
> compiler. The code works fine.
>
> Thumbs up for the assembly examples!
>

Sometimes it helps to "cheat" a little for the benefit of the IDE.
IDE's code analysis and syntax highlighting is not always in sync with
the compiler, which may have newer standards support or extensions that
confuse the IDE.  I usually have an ECLIPSE symbol defined as part of
the project within Eclipse, but not in my makefile (I use external
makefiles, not the IDE's project management).  Then I could have:

#ifdef ECLIPE
// In the IDE
#define __attribute__(x)

// Old-style static assertion
#define STATIC_ASSERT_NAME_(line)       STATIC_ASSERT_NAME2_(line)
#define STATIC_ASSERT_NAME2_(line)      assertion_failed_at_line_##line
#define static_assert(claim, warning) \
        typedef struct { \
                char STATIC_ASSERT_NAME_(__COUNTER__) [(claim) ? 2 : -2]; \
        } STATIC_ASSERT_NAME_(__COUNTER__)

// Fake 24-bit ints for IDE
typedef long int int24_t;
typedef unsigned long int uint24_t;


#else
// In the compiler

#ifdef __cplusplus
// static_assert is standard in c++11
#else
// C11 static_assertion
#define static_assert _Static_assert
#endif

typedef __int24 int24_t;
typedef __uint24 uint24_t;


#endif


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Loading...