Skip to content

Commit 19eb1ee

Browse files
1.0.1: Explict/default ctors, GCC 9
1 parent ca0b194 commit 19eb1ee

File tree

9 files changed

+294
-177
lines changed

9 files changed

+294
-177
lines changed

RELEASE_NOTES.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1.0.1 2020 Jan 19
2+
-------------------
3+
* Reg extractors return Bits or Mskd instead of Reg
4+
* Added bool Reg::all(Bits mask, Bits bits)
5+
* Added "explicit" keyword to additional constructors
6+
* "= default" copy constructors (required for GCC 9)
7+
* Version numbering

regbits.hxx

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
// regbits: C++ templates for type-safe bit manipulation
2-
// Copyright (C) 2019 Mark R. Rubin
2+
// Copyright (C) 2019,2020 Mark R. Rubin
33
//
44
// This file is part of regbits.
55
//
6-
// The regbits program is free software: you can redistribute it and/or
7-
// modify it under the terms of the GNU General Public License as
8-
// published by the Free Software Foundation, either version 3 of the
9-
// License, or (at your option) any later version.
6+
// The regbits program is free software: you can redistribute it
7+
// and/or modify it under the terms of the GNU General Public License
8+
// as published by the Free Software Foundation, either version 3 of
9+
// the License, or (at your option) any later version.
1010
//
1111
// The regbits program is distributed in the hope that it will be
1212
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13-
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1414
// General Public License for more details.
1515
//
16-
// You should have received a copy of the GNU General Public
17-
// License (LICENSE.txt) along with the regbits program. If not, see
16+
// You should have received a copy of the GNU General Public License
17+
// (LICENSE.txt) along with the regbits program. If not, see
1818
// <https://www.gnu.org/licenses/gpl.html>
1919

2020

2121
#ifndef regbits_hxx
2222
#define regbits_hxx
2323

24+
#define REGBITS_MAJOR_VERSION 1
25+
#define REGBITS_MINOR_VERSION 0
26+
#define REGBITS_MICRO_VERSION 1
27+
28+
2429
namespace regbits {
2530

2631
// forward refs
@@ -42,6 +47,7 @@ template<typename WORD, typename CLSS> class Pos {
4247

4348
// constructors
4449
//
50+
explicit
4551
constexpr
4652
Pos()
4753
: _pos(static_cast<WORD>(0))
@@ -60,6 +66,8 @@ template<typename WORD, typename CLSS> class Pos {
6066
const Pos<WORD, CLSS> &other)
6167
: _pos(other._pos)
6268
{}
69+
#else
70+
Pos(const Pos<WORD, CLSS> &other) = default;
6371
#endif
6472

6573

@@ -142,6 +150,7 @@ template<typename WORD, typename CLSS> class Bits {
142150

143151
// constructors
144152
//
153+
explicit
145154
constexpr
146155
Bits()
147156
: _bits(static_cast<WORD>(0))
@@ -182,6 +191,8 @@ template<typename WORD, typename CLSS> class Bits {
182191
const Bits<WORD, CLSS> &other)
183192
: _bits(other._bits)
184193
{}
194+
#else
195+
Bits(const Bits<WORD, CLSS> &other) = default;
185196
#endif
186197

187198
// for passing constexpr instance by value without requiring storage
@@ -348,6 +359,7 @@ template<typename WORD, typename CLSS> class Mskd {
348359

349360
// constructors
350361
//
362+
explicit
351363
constexpr
352364
Mskd()
353365
: _mask(static_cast<WORD>(0)),
@@ -385,6 +397,8 @@ template<typename WORD, typename CLSS> class Mskd {
385397
: _mask(other._mask),
386398
_bits(other._bits)
387399
{}
400+
#else
401+
Mskd(const Mskd<WORD, CLSS> &other) = default;
388402
#endif
389403

390404
// for passing constexpr instance by value without requiring storage
@@ -585,6 +599,7 @@ template<typename WORD, typename CLSS> class Shft {
585599

586600
// constructors
587601
//
602+
explicit
588603
constexpr
589604
Shft()
590605
: _mask(static_cast<WORD>(0)),
@@ -606,6 +621,8 @@ template<typename WORD, typename CLSS> class Shft {
606621
: _mask(other._mask),
607622
_pos (other._pos )
608623
{}
624+
#else
625+
Shft(const Shft<WORD, CLSS> &other) = default;
609626
#endif
610627

611628
// for passing constexpr instance by value without requiring storage
@@ -690,6 +707,8 @@ template<typename WORD, typename CLSS> class Reg {
690707
const Reg<WORD, CLSS> &other)
691708
: _word(other._word)
692709
{}
710+
#else
711+
Reg(const Reg<WORD, CLSS> &other) = default;
693712
#endif
694713

695714
// for passing constexpr instance by value without requiring storage
@@ -704,6 +723,15 @@ template<typename WORD, typename CLSS> class Reg {
704723
return Reg<WORD, CLSS>(_word);
705724
}
706725

726+
727+
// for passing all bits off
728+
static
729+
Bits<WORD, CLSS> zero()
730+
{
731+
return Bits<WORD, CLSS>(0);
732+
}
733+
734+
707735
// assignments
708736
//
709737
void operator=(const WORD word) volatile { _word = word; }
@@ -794,15 +822,17 @@ template<typename WORD, typename CLSS> class Reg {
794822

795823
// extractors
796824
//
797-
Reg<WORD, CLSS> operator &(Bits<WORD, CLSS> bits) volatile const
798-
{ return Reg<WORD, CLSS>(_word & bits._bits); }
799-
Reg<WORD, CLSS> operator &(Bits<WORD, CLSS> bits) const
800-
{ return Reg<WORD, CLSS>(_word & bits._bits); }
801-
802-
Reg<WORD, CLSS> operator &(Mskd<WORD, CLSS> mskd) volatile const
803-
{ return Reg<WORD, CLSS>(_word & mskd._mask); }
804-
Reg<WORD, CLSS> operator &(Mskd<WORD, CLSS> mskd) const
805-
{ return Reg<WORD, CLSS>(_word & mskd._mask); }
825+
Bits<WORD, CLSS> operator&(Bits<WORD, CLSS> bits) volatile const
826+
{ return Bits<WORD, CLSS>(_word & bits._bits); }
827+
Bits<WORD, CLSS> operator&(Bits<WORD, CLSS> bits) const
828+
{ return Bits<WORD, CLSS>(_word & bits._bits); }
829+
830+
Mskd<WORD, CLSS> operator&(Mskd<WORD, CLSS> mskd) volatile const
831+
{ return Mskd<WORD, CLSS>(_word & mskd._mask,
832+
mskd._mask); }
833+
Mskd<WORD, CLSS> operator&(Mskd<WORD, CLSS> mskd) const
834+
{ return Mskd<WORD, CLSS>(_word & mskd._mask,
835+
mskd._mask); }
806836

807837
WORD operator>>(const Shft<WORD, CLSS> shft) volatile const
808838
{ return (_word & shft._mask) >> shft._pos._pos; }
@@ -841,6 +871,21 @@ template<typename WORD, typename CLSS> class Reg {
841871
bool all(const Bits<WORD, CLSS> bits) const
842872
{ return (_word & bits._bits) == bits._bits; }
843873

874+
bool all(
875+
const Bits<WORD, CLSS> mask,
876+
const Bits<WORD, CLSS> bits)
877+
volatile const
878+
{
879+
return (_word & mask._bits) == bits._bits;
880+
}
881+
bool all(
882+
const Bits<WORD, CLSS> mask,
883+
const Bits<WORD, CLSS> bits)
884+
const
885+
{
886+
return (_word & mask._bits) == bits._bits;
887+
}
888+
844889
bool all(const Mskd<WORD, CLSS> mskd) volatile const
845890
{ return (_word & mskd._mask) == mskd._bits; }
846891
bool all(const Mskd<WORD, CLSS> mskd) const

unittest/bad/bits_implicit.cxx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// regbits: C++ templates for type-safe bit manipulation
2+
// Copyright (C) 2019 Mark R. Rubin
3+
//
4+
// This file is part of regbits.
5+
//
6+
// The regbits program is free software: you can redistribute it and/or
7+
// modify it under the terms of the GNU General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// The regbits program is distributed in the hope that it will be
12+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13+
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
// General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public
17+
// License (LICENSE.txt) along with the regbits program. If not, see
18+
// <https://www.gnu.org/licenses/gpl.html>
19+
20+
21+
#include "mcu_regbits.hxx"
22+
23+
using namespace mcu;
24+
25+
void pass_bits(
26+
Timer::Control::bits_t bits)
27+
{
28+
return timer1->control = bits;
29+
}
30+
31+
32+
33+
void bits_implicit()
34+
{
35+
#ifdef GOOD
36+
pass_bits(Timer::Control::bits_t(0));
37+
#else
38+
pass_bits(0);
39+
#endif
40+
}

unittest/bitfield.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ __attribute__((noinline)) void bits_val(
484484
const enum SERIAL_CONFIG bit ,
485485
const uint32_t value)
486486
{
487+
SERIAL2->config.word = 0;
488+
487489
switch (bit) {
488490
case MODE:
489491
SERIAL2->config.mode = value;
@@ -502,21 +504,20 @@ const uint32_t value)
502504
}
503505
}
504506

505-
506-
507507
__attribute__((noinline)) void call_bits_val_const()
508508
{
509509
bits_val(POLARITY, SERIAL_CONFIG_POLARITY);
510510
}
511511

512-
513-
514512
__attribute__((noinline)) void call_bits_val_var()
515513
{
516514
bits_val(POLARITY, SERIAL_CONFIG_POLARITY);
517515
}
518516

519-
517+
__attribute__((noinline)) void call_bits_val_zero()
518+
{
519+
bits_val(POLARITY, 0);
520+
}
520521

521522
__attribute__((noinline)) void constexpr_bits_array()
522523
{
@@ -541,6 +542,8 @@ __attribute__((noinline)) void bits_ref(
541542
const enum SERIAL_CONFIG *bit ,
542543
const uint32_t *value)
543544
{
545+
SERIAL2->config.word = 0;
546+
544547
switch (*bit) {
545548
case MODE:
546549
SERIAL2->config.mode = *value;
@@ -559,8 +562,6 @@ const uint32_t *value)
559562
}
560563
}
561564

562-
563-
564565
__attribute__((noinline)) void call_bits_ref_const()
565566
{
566567
const enum SERIAL_CONFIG config = MODE ;
@@ -569,8 +570,6 @@ __attribute__((noinline)) void call_bits_ref_const()
569570
bits_ref(&config, &value);
570571
}
571572

572-
573-
574573
__attribute__((noinline)) void call_bits_ref_var()
575574
{
576575
const enum SERIAL_CONFIG config = MODE ;
@@ -579,6 +578,14 @@ __attribute__((noinline)) void call_bits_ref_var()
579578
bits_ref(&config, &value);
580579
}
581580

581+
__attribute__((noinline)) void call_bits_ref_zero()
582+
{
583+
const enum SERIAL_CONFIG config = MODE;
584+
const uint32_t value = 0 ;
585+
586+
bits_ref(&config, &value);
587+
}
588+
582589

583590

584591
__attribute__((noinline)) void mskd_val(
@@ -1708,6 +1715,8 @@ __attribute__((noinline)) void run()
17081715
timer1_prescale = (ptr_t)(&TIMER1 ->prescale.word),
17091716
gpio1_words_3 = (ptr_t)(&GPIO1 ->words[3] );
17101717

1718+
uint32_t testnum = 0;
1719+
17111720
#include "do_tests.inl"
17121721

17131722
} // run()

0 commit comments

Comments
 (0)