Skip to content

Commit

Permalink
more formats
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Feb 8, 2024
1 parent 92fb6c0 commit 5c4e3ef
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 17 deletions.
112 changes: 112 additions & 0 deletions format.md
@@ -0,0 +1,112 @@
## Value format string syntax

Multibit values can have formated value labels.

Grammar for

```
label_format ::= ["%" format_spec]
format_spec ::= [sign] base
sign ::= "s" | "u"
base ::= "b" | "o" | "d" | "h"
```

### sign

```
"+" indicates that a sign should be used for both positive as well as negative numbers.
"-" indicates that a sign should be used only for negative numbers.
```

### alignment

```
"<" Forces the field to be left-aligned within the available space.
">" Forces the field to be right-aligned within the available space.
"^" Forces the field to be centered within the available space.
```

### base

```
b base 2
B base 2, leading zeros
o base 8
O base 8, leading zeros
d base 10
h base 16, lower-case letters a-f
H base 16, lower-case letters A-F
c ASCII char string
```

### zero / sign extension

A = 0x0723; B = 0xFF05

| format | base | signed | A | A3 | A2 | A1 | B | B3 | B2 | B1 |
|-|-|-|-|-|-|-|-|-|-|-|
| %b | 2 | | 11100100011 | …11 | …1 || 1111111100000101 | …11 | …1 ||
| %o | 8 | | 3443 | …43 | …3 || 177405 | …05 | …5 ||
| %d | 10 | | 1827 | …27 | …7 || 65285 | …85 | …5 ||
| %h | 16 | | 723 | 723 | …3 || ff05 | …05 | …5 ||
| %sb | 2 |v | 11100100011 | …11 | + | + | -11111011 | -…1 | - | - |
| %so | 8 |v | 3443 | …43 | + | + | -373 | -…2 | - | - |
| %sd | 10 |v | 1827 | …27 | + | + | -251 | -…1 | - | - |
| %sh | 16 |v | 723 | 723 | + | + | -fb | -fb | - | - |

### other

* decode floating point numbers
* decode vectors of numbers


### Floating point

* https://en.wikipedia.org/wiki/Floating-point_arithmetic
* https://en.wikipedia.org/wiki/IEEE_754
* https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
* https://en.wikipedia.org/wiki/Minifloat
* https://github.com/hlslibs/ac_types/blob/master/pdfdocs/ac_datatypes_ref.pdf

Fortran

* https://web.math.utk.edu/~vasili/refs/Fortran/AiS-f77/fortran7.10.html

* print floating point numbers in specific form
-

```
.toPrecision
.toExponential
.toFixed
```

### Fixed point
* specify fixed point number format
* specific


* https://en.wikipedia.org/wiki/Q_(number_format)
* https://www.ti.com/lit/ug/spru565b/spru565b.pdf

```
Qf
Qm.f
```

### Complex

### Vectors

### Libs

https://github.com/adamwdraper/Numeral-js

### References

* https://en.wikipedia.org/wiki/Printf

37 changes: 24 additions & 13 deletions lib/format.js
@@ -1,20 +1,31 @@
'use strict';

const format = (fmt, val /*, len*/ ) => {
if (fmt === 'b') {
return val.toString(2);
const format = (fmt, val, len) => {
const m1 = fmt.match(/^%(?<sign>[s])?(?<radix>[bodhH])$/);
len = BigInt(len);
if (m1) {
const p = m1.groups;
const radix = ({b: 2, o: 8, d: 10, h: 16, H: 16})[p.radix];
if (p.sign === 's') {
const sign = (val >> (len - 1n)) & 1n;
if (sign) {
val = val - (2n ** len);
}
}
let res = val.toString(radix);
if (p.radix === 'H') {
return res.toUpperCase();
}
return res;
}
if (fmt === 'o') {
return val.toString(8);
const m2 = fmt.match(/^%A$/);
if (m2) {
let txtRes = '';
for (let i = 0n; i < len; i += 8n) {
txtRes = String.fromCharCode(Number((val >> i) & 0xffn)) + txtRes;
}
return txtRes;
}
if (fmt === 'd') {
return val.toString(10);
}
if (fmt === 'h') {
return val.toString(16);
}

return val.toString();
};


Expand Down
27 changes: 23 additions & 4 deletions test/format.js
Expand Up @@ -4,10 +4,29 @@ const {expect} = require('chai');
const format = require('../lib/format.js');

const tt = [
{fmt: 'b', val: 12345678n, len: 16, res: '101111000110000101001110'},
{fmt: 'o', val: 12345678n, len: 16, res: '57060516'},
{fmt: 'd', val: 12345678n, len: 16, res: '12345678'},
{fmt: 'h', val: 12345678n, len: 16, res: 'bc614e'}
{fmt: '%b', val: 12345678n, len: 24, res: '101111000110000101001110'},
{fmt: '%o', val: 12345678n, len: 24, res: '57060516'},
{fmt: '%d', val: 12345678n, len: 24, res: '12345678'},
{fmt: '%h', val: 12345678n, len: 24, res: 'bc614e'},
{fmt: '%H', val: 12345678n, len: 24, res: 'BC614E'},

{fmt: '%sb', val: 5n, len: 3, res: '-11'},
{fmt: '%so', val: 5n, len: 3, res: '-3'},
{fmt: '%sd', val: 5n, len: 3, res: '-3'},
{fmt: '%sh', val: 5n, len: 3, res: '-3'},

{fmt: '%sb', val: 12345678n, len: 24, res: '-10000111001111010110010'},
{fmt: '%so', val: 12345678n, len: 24, res: '-20717262'},
{fmt: '%sd', val: 12345678n, len: 24, res: '-4431538'},
{fmt: '%sh', val: 12345678n, len: 24, res: '-439eb2'},
{fmt: '%sH', val: 12345678n, len: 24, res: '-439EB2'},

{fmt: '%sb', val: 12345678n, len: 42, res: '101111000110000101001110'},
{fmt: '%so', val: 12345678n, len: 42, res: '57060516'},
{fmt: '%sd', val: 12345678n, len: 42, res: '12345678'},
{fmt: '%sh', val: 12345678n, len: 42, res: 'bc614e'},

{fmt: '%A', val: 0x4142434445n, len: 40, res: 'ABCDE'},

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (12)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected trailing comma

Check warning on line 29 in test/format.js

View workflow job for this annotation

GitHub Actions / build (19)

Unexpected trailing comma
];

describe('format', () => {
Expand Down

0 comments on commit 5c4e3ef

Please sign in to comment.