Skip to content

Commit 1508d90

Browse files
tniessenjuanarbol
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 <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 110ead9 commit 1508d90

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/node_url.cc

+7-12
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,19 +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)))
492-
return;
490+
if (++parts > arraysize(numbers)) return;
493491
if (pointer == mark)
494492
return;
495493
int64_t n = ParseIPv4Number(mark, pointer);
@@ -511,18 +509,15 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) {
511509
// If any but the last item in numbers is greater than 255, return failure.
512510
// If the last item in numbers is greater than or equal to
513511
// 256^(5 - the number of items in numbers), return failure.
514-
if (tooBigNumbers > 1 ||
515-
(tooBigNumbers == 1 && numbers[parts - 1] <= 255) ||
516-
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))) {
517514
return;
518515
}
519516

520517
type_ = HostType::H_IPV4;
521518
val = static_cast<uint32_t>(numbers[parts - 1]);
522-
for (int n = 0; n < parts - 1; n++) {
523-
double b = 3 - n;
524-
val +=
525-
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));
526521
}
527522

528523
value_.ipv4 = val;

0 commit comments

Comments
 (0)