Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
78df885f
Commit
78df885f
authored
7 years ago
by
Hauke Petersen
Browse files
Options
Downloads
Patches
Plain Diff
sys/fmt: use also positive scaling for s32_dfp
parent
a19cb843
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sys/fmt/fmt.c
+37
-29
37 additions, 29 deletions
sys/fmt/fmt.c
sys/include/fmt.h
+14
-31
14 additions, 31 deletions
sys/include/fmt.h
with
51 additions
and
60 deletions
sys/fmt/fmt.c
+
37
−
29
View file @
78df885f
...
...
@@ -263,48 +263,56 @@ size_t fmt_s16_dec(char *out, int16_t val)
return
fmt_s32_dec
(
out
,
val
);
}
size_t
fmt_s16_dfp
(
char
*
out
,
int16_t
val
,
unsigned
fp_digits
)
size_t
fmt_s16_dfp
(
char
*
out
,
int16_t
val
,
int
fp_digits
)
{
return
fmt_s32_dfp
(
out
,
val
,
fp_digits
);
}
size_t
fmt_s32_dfp
(
char
*
out
,
int32_t
val
,
unsigned
fp_digits
)
size_t
fmt_s32_dfp
(
char
*
out
,
int32_t
val
,
int
fp_digits
)
{
assert
(
fp_digits
<
TENMAP_SIZE
);
assert
(
fp_digits
>
-
(
int
)
TENMAP_SIZE
);
int32_t
absolute
,
divider
;
unsigned
div_len
,
len
,
pos
=
0
;
char
tmp
[
9
];
unsigned
pos
=
0
;
if
(
fp_digits
==
0
)
{
return
fmt_s32_dec
(
out
,
val
);
pos
=
fmt_s32_dec
(
out
,
val
);
}
if
(
val
<
0
)
{
else
if
(
fp_digits
>
0
)
{
pos
=
fmt_s32_dec
(
out
,
val
);
if
(
out
)
{
out
[
pos
++
]
=
'-'
;
memset
(
&
out
[
pos
],
'0'
,
fp_digits
)
;
}
val
=
-
val
;
}
uint32_t
e
=
_tenmap
[
fp_digits
];
absolute
=
(
val
/
e
);
divider
=
val
-
(
absolute
*
e
);
pos
+=
fmt_s32_dec
(
&
out
[
pos
],
absolute
);
if
(
!
out
)
{
return
pos
+
1
+
fp_digits
;
/* abs len + decimal point + divider */
pos
+=
fp_digits
;
}
else
{
fp_digits
*=
-
1
;
uint32_t
e
=
_tenmap
[
fp_digits
];
int32_t
abs
=
(
val
/
(
int32_t
)
e
);
int32_t
div
=
val
-
(
abs
*
e
);
/* the divisor should never be negative */
if
(
div
<
0
)
{
div
*=
-
1
;
}
/* handle special case for negative number with zero as absolute value */
if
((
abs
==
0
)
&&
(
val
<
0
))
{
if
(
out
)
{
out
[
pos
]
=
'-'
;
}
pos
++
;
}
out
[
pos
++
]
=
'.'
;
len
=
pos
+
fp_digits
;
div_len
=
fmt_s32_dec
(
tmp
,
divider
);
while
(
pos
<
(
len
-
div_len
))
{
out
[
pos
++
]
=
'0'
;
}
for
(
size_t
i
=
0
;
i
<
div_len
;
i
++
)
{
out
[
pos
++
]
=
tmp
[
i
];
if
(
!
out
)
{
/* compensate for the decimal point character... */
pos
+=
fmt_s32_dec
(
NULL
,
abs
)
+
1
;
}
else
{
pos
+=
fmt_s32_dec
(
&
out
[
pos
],
abs
);
out
[
pos
++
]
=
'.'
;
unsigned
div_len
=
fmt_s32_dec
(
&
out
[
pos
],
div
);
fmt_lpad
(
&
out
[
pos
],
div_len
,
(
size_t
)
fp_digits
,
'0'
);
}
pos
+=
fp_digits
;
}
return
pos
;
...
...
This diff is collapsed.
Click to expand it.
sys/include/fmt.h
+
14
−
31
View file @
78df885f
...
...
@@ -225,60 +225,43 @@ size_t fmt_s16_dec(char *out, int16_t val);
/**
* @brief Convert 16-bit fixed point number to a decimal string
*
* The input for this function is a signed 16-bit integer holding the fixed
* point value as well as an unsigned integer defining the position of the
* decimal point, so this value defines the number of decimal digits after the
* decimal point.
*
* The resulting string will always be patted with zeros after the decimal point.
*
* For example: if @p val is -3548 and @p fp_digits is 2, the resulting string
* will be "-35.48". For @p val := 12010 and @p fp_digits := 3 the result will
* be "12.010".
*
* Will add a leading "-" if @p val is negative.
*
* If @p out is NULL, will only return the number of bytes that would have
* been written.
*
* @pre fp_digits < 8 (TENMAP_SIZE)
* See fmt_s32_dfp() for more details
*
* @param[out] out Pointer to the output buffer, or NULL
* @param[in] val Fixed point value
* @param[in] fp_digits Number of digits after the decimal point
* @param[in] fp_digits Number of digits after the decimal point, MUST be
* >= -7
*
* @return Length of the resulting string
*/
size_t
fmt_s16_dfp
(
char
*
out
,
int16_t
val
,
unsigned
fp_digits
);
size_t
fmt_s16_dfp
(
char
*
out
,
int16_t
val
,
int
fp_digits
);
/**
* @brief Convert 32-bit fixed point number to a decimal string
*
* The input for this function is a signed 32-bit integer holding the fixed
* point value as well as an
unsigned
integer defining the position of the
*
decimal point, so this value defines the number of decimal digits after th
e
*
decimal point
.
* point value as well as an integer defining the position of the
decimal point.
*
This value is used to shift the decimal point to the right (positive valu
e
*
of @p fp_digits) or to the left (negative value of @p fp_digits)
.
*
* Will add a leading "-" if @p val is negative.
*
* The resulting string will always be patted with zeros after the decimal point.
*
* For example: if @p val is -314159 and @p fp_digits is 5, the resulting string
* will be "-3.14159". For @p val := 16777215 and @p fp_digits := 6 the result
* will be "16.777215".
*
* If @p out is NULL, will only return the number of bytes that would have
* been written.
* For example: if @p val is -3548 and @p fp_digits is -2, the resulting string
* will be "-35.48". The same value for @p val with @p fp_digits of 2 will
* result in "-354800".
*
* @pre fp_digits
<
8 (TENMAP_SIZE)
* @pre fp_digits
> -
8 (TENMAP_SIZE)
*
* @param[out] out Pointer to the output buffer, or NULL
* @param[in] val Fixed point value
* @param[in] fp_digits Number of digits after the decimal point
* @param[in] fp_digits Number of digits after the decimal point, MUST be
* >= -7
*
* @return Length of the resulting string
*/
size_t
fmt_s32_dfp
(
char
*
out
,
int32_t
val
,
unsigned
fp_digits
);
size_t
fmt_s32_dfp
(
char
*
out
,
int32_t
val
,
int
fp_digits
);
/**
* @brief Format float to string
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment