Skip to content

Commit 360a3f3

Browse files
tniessenruyadorno
authored andcommitted
src: get rid of fp arithmetic in ParseIPv4Host
Even though most compiler should not actually emit FPU instructions, it is unnecessary to use floating-point arithmetic for powers of 2. Also change some signed counters to unsigned integers. PR-URL: #46326 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 34d70ce commit 360a3f3

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/node_url.cc

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "util-inl.h"
77

88
#include <algorithm>
9-
#include <cmath>
109
#include <cstdio>
1110
#include <numeric>
1211
#include <string>
@@ -477,18 +476,18 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) {
477476
const char* pointer = input;
478477
const char* mark = input;
479478
const char* end = pointer + length;
480-
int parts = 0;
479+
unsigned int parts = 0;
481480
uint32_t val = 0;
482481
uint64_t numbers[4];
483-
int tooBigNumbers = 0;
482+
unsigned int tooBigNumbers = 0;
484483
if (length == 0)
485484
return;
486485

487486
while (pointer <= end) {
488487
const char ch = pointer < end ? pointer[0] : kEOL;
489488
int64_t remaining = end - pointer - 1;
490489
if (ch == '.' || ch == kEOL) {
491-
if (++parts > static_cast<int>(arraysize(numbers))) return;
490+
if (++parts > arraysize(numbers)) return;
492491
if (pointer == mark)
493492
return;
494493
int64_t n = ParseIPv4Number(mark, pointer);
@@ -510,18 +509,15 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) {
510509
// If any but the last item in numbers is greater than 255, return failure.
511510
// If the last item in numbers is greater than or equal to
512511
// 256^(5 - the number of items in numbers), return failure.
513-
if (tooBigNumbers > 1 ||
514-
(tooBigNumbers == 1 && numbers[parts - 1] <= 255) ||
515-
numbers[parts - 1] >= pow(256, static_cast<double>(5 - parts))) {
512+
if (tooBigNumbers > 1 || (tooBigNumbers == 1 && numbers[parts - 1] <= 255) ||
513+
numbers[parts - 1] >= UINT64_C(1) << (8 * (5 - parts))) {
516514
return;
517515
}
518516

519517
type_ = HostType::H_IPV4;
520518
val = static_cast<uint32_t>(numbers[parts - 1]);
521-
for (int n = 0; n < parts - 1; n++) {
522-
double b = 3 - n;
523-
val +=
524-
static_cast<uint32_t>(numbers[n]) * static_cast<uint32_t>(pow(256, b));
519+
for (unsigned int n = 0; n < parts - 1; n++) {
520+
val += static_cast<uint32_t>(numbers[n]) << (8 * (3 - n));
525521
}
526522

527523
value_.ipv4 = val;

0 commit comments

Comments
 (0)