-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce
<REAL>_TRUNC_<INTEGER>
This commit introduces the `<REAL>_TRUNC_<INTEGER>` library functions, which under the hood use the compilers built-in implicit conversion feature.
- Loading branch information
Showing
5 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
f = open("real_trunc_integer.st", "w") | ||
|
||
types_from = [ | ||
"REAL", | ||
"LREAL", | ||
] | ||
|
||
types_to = [ | ||
"SINT", | ||
"USINT", | ||
"INT", | ||
"UINT", | ||
"DINT", | ||
"UDINT", | ||
"LINT", | ||
"ULINT", | ||
] | ||
|
||
template = """(******************** | ||
* | ||
* Converts a {0} to {1} | ||
* | ||
*********************) | ||
FUNCTION {0}_TRUNC_{1} : {1} | ||
VAR_INPUT | ||
in : {0}; | ||
END_VAR | ||
{0}_TRUNC_{1} := in; | ||
END_FUNCTION | ||
""" | ||
|
||
|
||
f.write(template.format("LREAL", "REAL")) | ||
|
||
for type_from in types_from: | ||
for type_to in types_to: | ||
f.write(template.format(type_from, type_to)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
(******************** | ||
* | ||
* Converts a LREAL to REAL | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_REAL : REAL | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_REAL := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to SINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_SINT : SINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_SINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to USINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_USINT : USINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_USINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to INT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_INT : INT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_INT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to UINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_UINT : UINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_UINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to DINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_DINT : DINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_DINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to UDINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_UDINT : UDINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_UDINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to LINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_LINT : LINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_LINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a REAL to ULINT | ||
* | ||
*********************) | ||
FUNCTION REAL_TRUNC_ULINT : ULINT | ||
VAR_INPUT | ||
in : REAL; | ||
END_VAR | ||
|
||
REAL_TRUNC_ULINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to SINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_SINT : SINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_SINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to USINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_USINT : USINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_USINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to INT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_INT : INT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_INT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to UINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_UINT : UINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_UINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to DINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_DINT : DINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_DINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to UDINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_UDINT : UDINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_UDINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to LINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_LINT : LINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_LINT := in; | ||
END_FUNCTION | ||
|
||
(******************** | ||
* | ||
* Converts a LREAL to ULINT | ||
* | ||
*********************) | ||
FUNCTION LREAL_TRUNC_ULINT : ULINT | ||
VAR_INPUT | ||
in : LREAL; | ||
END_VAR | ||
|
||
LREAL_TRUNC_ULINT := in; | ||
END_FUNCTION | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
f = open("real_trunc_integer.st", "w") | ||
|
||
types_from = [ | ||
"REAL", | ||
"LREAL", | ||
] | ||
|
||
types_to = [ | ||
"SINT", | ||
"USINT", | ||
"INT", | ||
"UINT", | ||
"DINT", | ||
"UDINT", | ||
"LINT", | ||
"ULINT", | ||
] | ||
|
||
template = " printf('%d$N', {0}_TRUNC_{1}(4.9)); // CHECK: 4\n" | ||
|
||
f.write("// RUN: (%COMPILE %s && %RUN) | %CHECK %s\n") | ||
f.write("FUNCTION main\n") | ||
f.write(template.format("LREAL", "REAL")) | ||
for type_from in types_from: | ||
for type_to in types_to: | ||
f.write(template.format(type_from, type_to)) | ||
f.write("END_FUNCTION") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: (%COMPILE %s && %RUN) | %CHECK %s | ||
FUNCTION main | ||
printf('%d$N', REAL_TRUNC_SINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_USINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_INT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_UINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_DINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_UDINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_LINT(4.9)); // CHECK: 4 | ||
printf('%d$N', REAL_TRUNC_ULINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_SINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_USINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_INT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_UINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_DINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_UDINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_LINT(4.9)); // CHECK: 4 | ||
printf('%d$N', LREAL_TRUNC_ULINT(4.9)); // CHECK: 4 | ||
END_FUNCTION |