Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86797 > unrolled thread

how to specify an 8 byte bool ?

Started byLynn McGuire <lynnmcguire5@gmail.com>
First post2022-10-04 20:41 -0500
Last post2022-10-10 18:04 +0200
Articles 20 on this page of 26 — 8 participants

Back to article view | Back to comp.lang.c++


Contents

  how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-04 20:41 -0500
    Re: how to specify an 8 byte bool ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-10-04 19:58 -0700
      Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-04 22:35 -0500
        Re: how to specify an 8 byte bool ? Christian Gollwitzer <auriocus@gmx.de> - 2022-10-05 07:38 +0200
          Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:02 -0500
          Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:03 -0500
        Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-05 09:38 +0200
          Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:03 -0500
        Re: how to specify an 8 byte bool ? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-10-05 23:19 +0200
          Re: how to specify an 8 byte bool ? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-05 18:45 -0700
            Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 21:05 -0500
              Re: how to specify an 8 byte bool ? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-05 19:07 -0700
            Re: how to specify an 8 byte bool ? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-10-06 10:39 +0200
            Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-06 14:14 +0200
            Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-06 14:03 +0000
    Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 14:10 -0500
      Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 20:34 +0000
        Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 16:18 -0500
          Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 22:25 +0000
            Re: how to specify an 8 byte bool ? scott@slp53.sl.home (Scott Lurndal) - 2022-10-05 22:27 +0000
              Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-05 19:16 -0500
      Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-06 14:16 +0200
        Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-06 15:07 -0500
          Re: how to specify an 8 byte bool ? David Brown <david.brown@hesbynett.no> - 2022-10-07 09:07 +0200
            Re: how to specify an 8 byte bool ? Lynn McGuire <lynnmcguire5@gmail.com> - 2022-10-07 14:23 -0500
    Re: how to specify an 8 byte bool ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-10-10 18:04 +0200

Page 1 of 2  [1] 2  Next page →


#86797 — how to specify an 8 byte bool ?

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-04 20:41 -0500
Subjecthow to specify an 8 byte bool ?
Message-ID<thinbh$2pm70$1@dont-email.me>
How do I specify an 8 byte bool ?  Long long ?

Thanks,
Lynn

[toc] | [next] | [standalone]


#86798

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2022-10-04 19:58 -0700
Message-ID<87fsg3rn6s.fsf@nosuchdomain.example.com>
In reply to#86797
Lynn McGuire <lynnmcguire5@gmail.com> writes:
> How do I specify an 8 byte bool ?  Long long ?

What for?

bool is a predefined type, and its size is determined by the implementation.

long long is at least 64 bits (and almost always exactly 64 bits).
uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
exist.  A "byte" is almost always 8 bits, but may be larger (see
CHAR_BIT).

What problem are you trying to solve?

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#86800

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-04 22:35 -0500
Message-ID<thiu1f$2t2ra$1@dont-email.me>
In reply to#86798
On 10/4/2022 9:58 PM, Keith Thompson wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> How do I specify an 8 byte bool ?  Long long ?
> 
> What for?
> 
> bool is a predefined type, and its size is determined by the implementation.
> 
> long long is at least 64 bits (and almost always exactly 64 bits).
> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
> exist.  A "byte" is almost always 8 bits, but may be larger (see
> CHAR_BIT).
> 
> What problem are you trying to solve?

I've got a double array, integer*8 array, logical*8 array, and 
character*8 array equivalenced in my Fortran code that I am converting 
to C++.  I want to have the same capability in my C++ code until I get 
down the road and figure out what I want to do with this code, if anything.

Thanks,
Lynn

[toc] | [prev] | [next] | [standalone]


#86801

FromChristian Gollwitzer <auriocus@gmx.de>
Date2022-10-05 07:38 +0200
Message-ID<thj5cn$2tlga$1@dont-email.me>
In reply to#86800
Am 05.10.22 um 05:35 schrieb Lynn McGuire:
> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> How do I specify an 8 byte bool ?  Long long ?
>>
>> What for?
>>
>> bool is a predefined type, and its size is determined by the 
>> implementation.
>>
>> long long is at least 64 bits (and almost always exactly 64 bits).
>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>> CHAR_BIT).
>>
>> What problem are you trying to solve?
> 
> I've got a double array, integer*8 array, logical*8 array, and 
> character*8 array equivalenced in my Fortran code that I am converting 
> to C++.  I want to have the same capability in my C++ code until I get 
> down the road and figure out what I want to do with this code, if anything.

Then I'd use uint64_t for the logical, int64_t for the integer and 
char[8] for the character array (not sure about the last one, though, 
since I don't use Fortran much). You can also check how the old f2c 
converter compiles it (https://netlib.org/f2c/). It generally works well 
for F77 code, although the output is not overly readable.
I guess you used fable instead of f2c because you want to work on the 
code after translation, right?

             Christian

[toc] | [prev] | [next] | [standalone]


#86804

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 14:02 -0500
Message-ID<thkkc4$31u9n$3@dont-email.me>
In reply to#86801
On 10/5/2022 12:38 AM, Christian Gollwitzer wrote:
> Am 05.10.22 um 05:35 schrieb Lynn McGuire:
>> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>> How do I specify an 8 byte bool ?  Long long ?
>>>
>>> What for?
>>>
>>> bool is a predefined type, and its size is determined by the 
>>> implementation.
>>>
>>> long long is at least 64 bits (and almost always exactly 64 bits).
>>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>>> CHAR_BIT).
>>>
>>> What problem are you trying to solve?
>>
>> I've got a double array, integer*8 array, logical*8 array, and 
>> character*8 array equivalenced in my Fortran code that I am converting 
>> to C++.  I want to have the same capability in my C++ code until I get 
>> down the road and figure out what I want to do with this code, if 
>> anything.
> 
> Then I'd use uint64_t for the logical, int64_t for the integer and 
> char[8] for the character array (not sure about the last one, though, 
> since I don't use Fortran much). You can also check how the old f2c 
> converter compiles it (https://netlib.org/f2c/). It generally works well 
> for F77 code, although the output is not overly readable.
> I guess you used fable instead of f2c because you want to work on the 
> code after translation, right?
> 
>              Christian

I am building my own version of f2c with significant code from fable. 
Fable is written in python and is part of another app so separating it 
out is a "Hot Skillz".  F2c is written in very nasty plain old C code.

Here is my task list for f2c to date:

//  1. added support for a .cpp output file instead of just the .c file
//  2. convert the F90 cycle command to the C continue
//  3. convert the F90 exit command to the C break
//  4. convert the c /* comment */  to // comment
//  5. added support for the logical*8 data type, complementary to the 
integer*8
//      data type for memory alignment, both resolve to the C long long 
integer
//  6. generate true and false for logicals if C++ instead of TRUE_ and 
FALSE_
//  7. removed all trailing underscores from variable and subroutine names
//  8. removed the ftnlen argument from the calls to subroutines with 
character arguements
//  9. added the Fabel FEM library


//  Things to do:
//  1. make all argument arrays start at index 0
//  2. convert writes to the FEM single command write with embedded format
//  3. retain array lengths
//  4. retain parameters with their values

Lynn

[toc] | [prev] | [next] | [standalone]


#86805

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 14:03 -0500
Message-ID<thkkeg$31u9n$4@dont-email.me>
In reply to#86801
On 10/5/2022 12:38 AM, Christian Gollwitzer wrote:
> Am 05.10.22 um 05:35 schrieb Lynn McGuire:
>> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>> How do I specify an 8 byte bool ?  Long long ?
>>>
>>> What for?
>>>
>>> bool is a predefined type, and its size is determined by the 
>>> implementation.
>>>
>>> long long is at least 64 bits (and almost always exactly 64 bits).
>>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>>> CHAR_BIT).
>>>
>>> What problem are you trying to solve?
>>
>> I've got a double array, integer*8 array, logical*8 array, and 
>> character*8 array equivalenced in my Fortran code that I am converting 
>> to C++.  I want to have the same capability in my C++ code until I get 
>> down the road and figure out what I want to do with this code, if 
>> anything.
> 
> Then I'd use uint64_t for the logical, int64_t for the integer and 
> char[8] for the character array (not sure about the last one, though, 
> since I don't use Fortran much). You can also check how the old f2c 
> converter compiles it (https://netlib.org/f2c/). It generally works well 
> for F77 code, although the output is not overly readable.
> I guess you used fable instead of f2c because you want to work on the 
> code after translation, right?
> 
>              Christian

BTW, thanks !

Lynn

[toc] | [prev] | [next] | [standalone]


#86802

FromDavid Brown <david.brown@hesbynett.no>
Date2022-10-05 09:38 +0200
Message-ID<thjcag$2u7dl$1@dont-email.me>
In reply to#86800
On 05/10/2022 05:35, Lynn McGuire wrote:
> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> How do I specify an 8 byte bool ?  Long long ?
>>
>> What for?
>>
>> bool is a predefined type, and its size is determined by the 
>> implementation.
>>
>> long long is at least 64 bits (and almost always exactly 64 bits).
>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>> CHAR_BIT).
>>
>> What problem are you trying to solve?
> 
> I've got a double array, integer*8 array, logical*8 array, and 
> character*8 array equivalenced in my Fortran code that I am converting 
> to C++.  I want to have the same capability in my C++ code until I get 
> down the road and figure out what I want to do with this code, if anything.
> 
> Thanks,
> Lynn
> 

I don't know Fortran, but if you want something that works like a bool 
and takes 8 bytes, you can either use a uint64_t and be careful that you 
don't put anything other than 0 or 1 in it (that might be fine when you 
have automatically generated code).

Alternatively, it's easy to make a class that acts like a bool:

(Adding concepts to limit the type of T is left as an exercise for the 
reader :-) )


#include <cstdint>

template <class T>
class sized_bool {
private :
     T storage;
public :
     constexpr sized_bool() : storage(0) {}
     constexpr sized_bool(bool b) : storage(b) {}
     constexpr operator bool() const { return storage; }
};

using big_bool = sized_bool<std::uint64_t>;

void tests() {
     static_assert(sizeof(big_bool) == 8);

     constexpr big_bool b1 = 1;
     static_assert(b1 == true);
     static_assert(b1 == 1);
     static_assert(b1 ? 1 : 0);

     constexpr big_bool b2 { false };
     static_assert(b2 == false);
     static_assert(b2 == 0);
     static_assert(b2 ? 0 : 1);

     constexpr big_bool b3 = 123;
     static_assert(b3 == true);
     static_assert(b3 == 1);
     static_assert(b3 ? 1 : 0);

     static_assert(big_bool(100) == 1);
}

[toc] | [prev] | [next] | [standalone]


#86806

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 14:03 -0500
Message-ID<thkkeu$31u9n$5@dont-email.me>
In reply to#86802
On 10/5/2022 2:38 AM, David Brown wrote:
> On 05/10/2022 05:35, Lynn McGuire wrote:
>> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>> How do I specify an 8 byte bool ?  Long long ?
>>>
>>> What for?
>>>
>>> bool is a predefined type, and its size is determined by the 
>>> implementation.
>>>
>>> long long is at least 64 bits (and almost always exactly 64 bits).
>>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>>> CHAR_BIT).
>>>
>>> What problem are you trying to solve?
>>
>> I've got a double array, integer*8 array, logical*8 array, and 
>> character*8 array equivalenced in my Fortran code that I am converting 
>> to C++.  I want to have the same capability in my C++ code until I get 
>> down the road and figure out what I want to do with this code, if 
>> anything.
>>
>> Thanks,
>> Lynn
>>
> 
> I don't know Fortran, but if you want something that works like a bool 
> and takes 8 bytes, you can either use a uint64_t and be careful that you 
> don't put anything other than 0 or 1 in it (that might be fine when you 
> have automatically generated code).
> 
> Alternatively, it's easy to make a class that acts like a bool:
> 
> (Adding concepts to limit the type of T is left as an exercise for the 
> reader :-) )
> 
> 
> #include <cstdint>
> 
> template <class T>
> class sized_bool {
> private :
>      T storage;
> public :
>      constexpr sized_bool() : storage(0) {}
>      constexpr sized_bool(bool b) : storage(b) {}
>      constexpr operator bool() const { return storage; }
> };
> 
> using big_bool = sized_bool<std::uint64_t>;
> 
> void tests() {
>      static_assert(sizeof(big_bool) == 8);
> 
>      constexpr big_bool b1 = 1;
>      static_assert(b1 == true);
>      static_assert(b1 == 1);
>      static_assert(b1 ? 1 : 0);
> 
>      constexpr big_bool b2 { false };
>      static_assert(b2 == false);
>      static_assert(b2 == 0);
>      static_assert(b2 ? 0 : 1);
> 
>      constexpr big_bool b3 = 123;
>      static_assert(b3 == true);
>      static_assert(b3 == 1);
>      static_assert(b3 ? 1 : 0);
> 
>      static_assert(big_bool(100) == 1);
> }

Thanks !

Lynn

[toc] | [prev] | [next] | [standalone]


#86810

From"Fred. Zwarts" <F.Zwarts@KVI.nl>
Date2022-10-05 23:19 +0200
Message-ID<thksdo$14l$1@gioia.aioe.org>
In reply to#86800
Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
> On 10/4/2022 9:58 PM, Keith Thompson wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> How do I specify an 8 byte bool ?  Long long ?
>>
>> What for?
>>
>> bool is a predefined type, and its size is determined by the 
>> implementation.
>>
>> long long is at least 64 bits (and almost always exactly 64 bits).
>> uint64_t and int64_t are exactly 64 bits, but are not guaranteed to
>> exist.  A "byte" is almost always 8 bits, but may be larger (see
>> CHAR_BIT).
>>
>> What problem are you trying to solve?
> 
> I've got a double array, integer*8 array, logical*8 array, and 
> character*8 array equivalenced in my Fortran code that I am converting 
> to C++.  I want to have the same capability in my C++ code until I get 
> down the road and figure out what I want to do with this code, if anything.

The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union.
So, I would probably go for an array of unions. Where each union is a 
union of a double, integer, bool and char[8]. The bool would then 
automatically use the space of the largest type in the union.

[toc] | [prev] | [next] | [standalone]


#86814

From"daniel...@gmail.com" <danielaparker@gmail.com>
Date2022-10-05 18:45 -0700
Message-ID<31f4c07e-1ec1-4cd8-a095-bd58f6af64e9n@googlegroups.com>
In reply to#86810
On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:

> > I've got a double array, integer*8 array, logical*8 array, and 
> > character*8 array equivalenced in my Fortran code that I am converting 
> > to C++.  I want to have the same capability in my C++ code until I get 
> > down the road and figure out what I want to do with this code, if anything.
> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union. 

> So, I would probably go for an array of unions. Where each union is a 
> union of a double, integer, bool and char[8]. The bool would then 
> automatically use the space of the largest type in the union.

I think it would be undefined behaviour to use a C++ union for a FORTRAN 
EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.

Daniel

[toc] | [prev] | [next] | [standalone]


#86815

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 21:05 -0500
Message-ID<thld59$36ts5$1@dont-email.me>
In reply to#86814
On 10/5/2022 8:45 PM, daniel...@gmail.com wrote:
> On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
>> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
> 
>>> I've got a double array, integer*8 array, logical*8 array, and
>>> character*8 array equivalenced in my Fortran code that I am converting
>>> to C++.  I want to have the same capability in my C++ code until I get
>>> down the road and figure out what I want to do with this code, if anything.
>> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union.
> 
>> So, I would probably go for an array of unions. Where each union is a
>> union of a double, integer, bool and char[8]. The bool would then
>> automatically use the space of the largest type in the union.
> 
> I think it would be undefined behaviour to use a C++ union for a FORTRAN
> EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.
> 
> Daniel

I assume that you mean type punning ???
    https://en.wikipedia.org/wiki/Type_punning

Lynn

[toc] | [prev] | [next] | [standalone]


#86816

From"daniel...@gmail.com" <danielaparker@gmail.com>
Date2022-10-05 19:07 -0700
Message-ID<2b2a51d5-f205-409b-8177-bbe6e4836ac8n@googlegroups.com>
In reply to#86815
On Wednesday, October 5, 2022 at 10:05:48 PM UTC-4, Lynn McGuire wrote:
> I assume that you mean type punning ??? 
> https://en.wikipedia.org/wiki/Type_punning 
> 
> Lynn

Just so :-(

Daniel

[toc] | [prev] | [next] | [standalone]


#86818

From"Fred. Zwarts" <F.Zwarts@KVI.nl>
Date2022-10-06 10:39 +0200
Message-ID<thm47a$lem$1@gioia.aioe.org>
In reply to#86814
Op 06.okt..2022 om 03:45 schreef daniel...@gmail.com:
> On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
>> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
> 
>>> I've got a double array, integer*8 array, logical*8 array, and
>>> character*8 array equivalenced in my Fortran code that I am converting
>>> to C++.  I want to have the same capability in my C++ code until I get
>>> down the road and figure out what I want to do with this code, if anything.
>> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union.
> 
>> So, I would probably go for an array of unions. Where each union is a
>> union of a double, integer, bool and char[8]. The bool would then
>> automatically use the space of the largest type in the union.
> 
> I think it would be undefined behaviour to use a C++ union for a FORTRAN
> EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.
I did not read that type punning was used in this Fortran program.

[toc] | [prev] | [next] | [standalone]


#86819

FromDavid Brown <david.brown@hesbynett.no>
Date2022-10-06 14:14 +0200
Message-ID<thmgqu$3br22$1@dont-email.me>
In reply to#86814
On 06/10/2022 03:45, daniel...@gmail.com wrote:
> On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
>> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
> 
>>> I've got a double array, integer*8 array, logical*8 array, and
>>> character*8 array equivalenced in my Fortran code that I am converting
>>> to C++.  I want to have the same capability in my C++ code until I get
>>> down the road and figure out what I want to do with this code, if anything.
>> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union.
> 
>> So, I would probably go for an array of unions. Where each union is a
>> union of a double, integer, bool and char[8]. The bool would then
>> automatically use the space of the largest type in the union.
> 
> I think it would be undefined behaviour to use a C++ union for a FORTRAN
> EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.
> 

I think "type pruning" sounds more fun than mere "type punning".  It 
sounds like a way to clear up a mess of too many types, leaving only the 
pretty ones :-)

I guess the answer here is a class which has a single storage unit 
(uint64_t) with constructors and conversion operators to the different 
types, using memcpy() to make it all legal.  A decent compiler will 
optimise away the memcpy().

Alternatively, if you can limit yourself to gcc, clang, icc, and other 
compilers that copy gcc's attributes, the "may_alias" attribute on a 
type lets you use it for type punning (it gives the type the superpowers 
of a character type for accesses).

#include <cstdint>
#include <cstring>
#include <array>

class DIBC {
private :
     uint64_t storage;
     template <class T> constexpr uint64_t toUint64_t(const T x) {
         static_assert(sizeof(x) == 8);
         uint64_t y;
         std::memcpy(&y, &x, 8);
         return y;
     }
     template <class T> constexpr T fromUint64_t(uint64_t x) {
         static_assert(sizeof(T) == 8);
         T y;
         std::memcpy(&y, &x, 8);
         return y;
     }
public :
     using char8 = std::array<char, 8>;
     constexpr DIBC() : storage(0) {}
     constexpr DIBC(uint64_t x) : storage(x) {}
     constexpr DIBC(int64_t x) : storage(x) {}
     constexpr DIBC(bool x) : storage(x) {}
     constexpr DIBC(char8 x) : storage(toUint64_t(x)) {}

     constexpr operator uint64_t() const { return storage; }
     constexpr operator int64_t() const { return storage; }
     constexpr operator bool() const { return storage; }
     constexpr operator char8 const () { return 
fromUint64_t<char8>(storage); }
};




[toc] | [prev] | [next] | [standalone]


#86821

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-10-06 14:03 +0000
Message-ID<zeB%K.409928$SAT4.26585@fx13.iad>
In reply to#86814
"daniel...@gmail.com" <danielaparker@gmail.com> writes:
>On Wednesday, October 5, 2022 at 5:20:09 PM UTC-4, F.Zwarts wrote:
>> Op 05.okt..2022 om 05:35 schreef Lynn McGuire:
>
>> > I've got a double array, integer*8 array, logical*8 array, and 
>> > character*8 array equivalenced in my Fortran code that I am converting 
>> > to C++.  I want to have the same capability in my C++ code until I get 
>> > down the road and figure out what I want to do with this code, if anything.
>> The Fortran EQUIVALENT is in my mind roughly equivalent to a C++ union. 
>
>> So, I would probably go for an array of unions. Where each union is a 
>> union of a double, integer, bool and char[8]. The bool would then 
>> automatically use the space of the largest type in the union.
>
>I think it would be undefined behaviour to use a C++ union for a FORTRAN 
>EQUIVALENCE statement. C++ (unlike C) does not permit type pruning.

In theory, but not necessarily in practice.  It works just fine with g++.

[toc] | [prev] | [next] | [standalone]


#86807

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 14:10 -0500
Message-ID<thkkrd$31u9n$6@dont-email.me>
In reply to#86797
On 10/4/2022 8:41 PM, Lynn McGuire wrote:
> How do I specify an 8 byte bool ?  Long long ?
> 
> Thanks,
> Lynn

I forgot to mention that this is how I handle the 8 byte logical*8 right 
now:

typedef long long logical8;

Thanks,
Lynn

[toc] | [prev] | [next] | [standalone]


#86808

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-10-05 20:34 +0000
Message-ID<oTl%K.408141$SAT4.349103@fx13.iad>
In reply to#86807
Lynn McGuire <lynnmcguire5@gmail.com> writes:
>On 10/4/2022 8:41 PM, Lynn McGuire wrote:
>> How do I specify an 8 byte bool ?  Long long ?
>> 
>> Thanks,
>> Lynn
>
>I forgot to mention that this is how I handle the 8 byte logical*8 right 
>now:
>
>typedef long long logical8;

Wouldn't it make more sense to convert to LOGICAL*4 or just LOGICAL and
use a byte-width C++ bool instead?   Seems silly to waste 63 bits.

[toc] | [prev] | [next] | [standalone]


#86809

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2022-10-05 16:18 -0500
Message-ID<thksc3$32n95$1@dont-email.me>
In reply to#86808
On 10/5/2022 3:34 PM, Scott Lurndal wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> On 10/4/2022 8:41 PM, Lynn McGuire wrote:
>>> How do I specify an 8 byte bool ?  Long long ?
>>>
>>> Thanks,
>>> Lynn
>>
>> I forgot to mention that this is how I handle the 8 byte logical*8 right
>> now:
>>
>> typedef long long logical8;
> 
> Wouldn't it make more sense to convert to LOGICAL*4 or just LOGICAL and
> use a byte-width C++ bool instead?   Seems silly to waste 63 bits.

The indexes for the arrays are the same value for all four types of 
arrays.  So every array chunk has to be 64 bits whether it is double, 
integer*8, character*8, or logical*8.

And yes, I agree, not efficient.  But, who cares in this day of TB 
drives and GB ram ?  And I want to use the same code base for the 32 bit 
version and the 64 bit version.

Thanks,
Lynn

[toc] | [prev] | [next] | [standalone]


#86811

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-10-05 22:25 +0000
Message-ID<Wvn%K.317747$9Yp5.123410@fx12.iad>
In reply to#86809
Lynn McGuire <lynnmcguire5@gmail.com> writes:
>On 10/5/2022 3:34 PM, Scott Lurndal wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> On 10/4/2022 8:41 PM, Lynn McGuire wrote:
>>>> How do I specify an 8 byte bool ?  Long long ?
>>>>
>>>> Thanks,
>>>> Lynn
>>>
>>> I forgot to mention that this is how I handle the 8 byte logical*8 right
>>> now:
>>>
>>> typedef long long logical8;
>> 
>> Wouldn't it make more sense to convert to LOGICAL*4 or just LOGICAL and
>> use a byte-width C++ bool instead?   Seems silly to waste 63 bits.
>
>The indexes for the arrays are the same value for all four types of 
>arrays.  So every array chunk has to be 64 bits whether it is double, 
>integer*8, character*8, or logical*8.

Presumably the compiler will do the appropriate math using
the index to calculate the memory byte(s) required.

I don't understand your comment, unless you're using byte offsets
intead of normal fortran indexing.

[toc] | [prev] | [next] | [standalone]


#86812

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-10-05 22:27 +0000
Message-ID<nxn%K.317748$9Yp5.261147@fx12.iad>
In reply to#86811
scott@slp53.sl.home (Scott Lurndal) writes:
>Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>On 10/5/2022 3:34 PM, Scott Lurndal wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>> On 10/4/2022 8:41 PM, Lynn McGuire wrote:
>>>>> How do I specify an 8 byte bool ?  Long long ?
>>>>>
>>>>> Thanks,
>>>>> Lynn
>>>>
>>>> I forgot to mention that this is how I handle the 8 byte logical*8 right
>>>> now:
>>>>
>>>> typedef long long logical8;
>>> 
>>> Wouldn't it make more sense to convert to LOGICAL*4 or just LOGICAL and
>>> use a byte-width C++ bool instead?   Seems silly to waste 63 bits.
>>
>>The indexes for the arrays are the same value for all four types of 
>>arrays.  So every array chunk has to be 64 bits whether it is double, 
>>integer*8, character*8, or logical*8.
>
>Presumably the compiler will do the appropriate math using
>the index to calculate the memory byte(s) required.
>
>I don't understand your comment, unless you're using byte offsets
>intead of normal fortran indexing.

Ah never mind, I see your aliasing the same storage four ways.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c++


csiph-web