Convert to type-generic math (#2050)

* [Optimization] Convert to type-generic math

* add std:: prefix in obj-c++ files

* more cleanup

* revert test changes

* convert min and max back to fmin/fmax
This commit is contained in:
Hannah Troisi
2016-08-09 15:31:37 -07:00
committed by Adlai Holler
parent aba05a747c
commit 893e601e81
14 changed files with 62 additions and 39 deletions

View File

@@ -11,6 +11,7 @@
#import "ASEditableTextNode.h"
#import <objc/message.h>
#import <tgmath.h>
#import "ASDisplayNode+Subclasses.h"
#import "ASEqualityHelpers.h"
@@ -238,9 +239,9 @@
{
ASTextKitComponents *displayedComponents = [self isDisplayingPlaceholder] ? _placeholderTextKitComponents : _textKitComponents;
CGSize textSize = [displayedComponents sizeForConstrainedWidth:constrainedSize.width];
CGFloat width = ceilf(textSize.width + _textContainerInset.left + _textContainerInset.right);
CGFloat height = ceilf(textSize.height + _textContainerInset.top + _textContainerInset.bottom);
return CGSizeMake(fminf(width, constrainedSize.width), fminf(height, constrainedSize.height));
CGFloat width = std::ceil(textSize.width + _textContainerInset.left + _textContainerInset.right);
CGFloat height = std::ceil(textSize.height + _textContainerInset.top + _textContainerInset.bottom);
return CGSizeMake(std::fmin(width, constrainedSize.width), std::fmin(height, constrainedSize.height));
}
- (void)layout

View File

@@ -12,7 +12,10 @@
#if TARGET_OS_TV
#import "ASImageNode+tvOS.h"
#import <GLKit/GLKit.h>
#import <tgmath.h>
#import "ASDisplayNodeExtras.h"
@implementation ASImageNode (tvOS)
@@ -75,8 +78,8 @@
// BUT we apply our transforms to *view since we want to apply
// the transforms to the root view (L: 107)
CGPoint point = [touch locationInView:self.view];
float pitch = 0;
float yaw = 0;
CGFloat pitch = 0;
CGFloat yaw = 0;
BOOL topHalf = NO;
if (point.y > CGRectGetHeight(self.view.frame)) {
pitch = 15;
@@ -100,7 +103,7 @@
if (yaw > 0) {
yaw = -yaw;
} else {
yaw = fabsf(yaw);
yaw = fabs(yaw);
}
}

View File

@@ -10,6 +10,8 @@
#import "ASImageNode.h"
#import <tgmath.h>
#import "_ASDisplayLayer.h"
#import "ASAssert.h"
#import "ASDisplayNode+Subclasses.h"
@@ -313,7 +315,7 @@ struct ASImageNodeDrawParameters {
CGSize imageSize = image.size;
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(floorf(bounds.size.width * contentsScale), floorf(bounds.size.height * contentsScale));
CGSize boundsSizeInPixels = CGSizeMake(std::floor(bounds.size.width * contentsScale), std::floor(bounds.size.height * contentsScale));
if (_debugLabelNode) {
CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height);

View File

@@ -10,6 +10,9 @@
#if TARGET_OS_IOS
#import "ASMapNode.h"
#import <tgmath.h>
#import "ASDisplayNodeInternal.h"
#import "ASDisplayNode+Subclasses.h"
#import "ASDisplayNodeExtras.h"
@@ -320,16 +323,16 @@
CLLocationCoordinate2D bottomRightCoord = CLLocationCoordinate2DMake(90, -180);
for (id<MKAnnotation> annotation in annotations) {
topLeftCoord = CLLocationCoordinate2DMake(fmax(topLeftCoord.latitude, annotation.coordinate.latitude),
fmin(topLeftCoord.longitude, annotation.coordinate.longitude));
bottomRightCoord = CLLocationCoordinate2DMake(fmin(bottomRightCoord.latitude, annotation.coordinate.latitude),
fmax(bottomRightCoord.longitude, annotation.coordinate.longitude));
topLeftCoord = CLLocationCoordinate2DMake(std::fmax(topLeftCoord.latitude, annotation.coordinate.latitude),
std::fmin(topLeftCoord.longitude, annotation.coordinate.longitude));
bottomRightCoord = CLLocationCoordinate2DMake(std::fmin(bottomRightCoord.latitude, annotation.coordinate.latitude),
std::fmax(bottomRightCoord.longitude, annotation.coordinate.longitude));
}
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5,
topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5),
MKCoordinateSpanMake(fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 2,
fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 2));
MKCoordinateSpanMake(std::fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 2,
std::fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 2));
return region;
}

View File

@@ -12,6 +12,7 @@
#import "ASTextNode+Beta.h"
#include <mutex>
#import <tgmath.h>
#import "_ASDisplayLayer.h"
#import "ASDisplayNode+Subclasses.h"
@@ -524,7 +525,7 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ];
[renderer enumerateTextIndexesAtPosition:point usingBlock:^(NSUInteger characterIndex, CGRect glyphBoundingRect, BOOL *stop) {
CGPoint glyphLocation = CGPointMake(CGRectGetMidX(glyphBoundingRect), CGRectGetMidY(glyphBoundingRect));
CGFloat currentDistance = sqrtf(powf(point.x - glyphLocation.x, 2.f) + powf(point.y - glyphLocation.y, 2.f));
CGFloat currentDistance = std::sqrt(std::pow(point.x - glyphLocation.x, 2.f) + std::pow(point.y - glyphLocation.y, 2.f));
if (currentDistance >= minimumGlyphDistance) {
// If the distance computed from the touch to the glyph location is
// not the minimum among the located link attributes, we can just skip

View File

@@ -11,6 +11,7 @@
#import "ASHighlightOverlayLayer.h"
#import <UIKit/UIKit.h>
#import <tgmath.h>
#import "ASInternalHelpers.h"
@@ -86,7 +87,7 @@ static const UIEdgeInsets padding = {2, 4, 1.5, 4};
if (targetLayer != nil) {
rect = [self convertRect:rect fromLayer:targetLayer];
}
rect = CGRectMake(roundf(rect.origin.x), roundf(rect.origin.y), roundf(rect.size.width), roundf(rect.size.height));
rect = CGRectMake(std::round(rect.origin.x), std::round(rect.origin.y), std::round(rect.size.width), std::round(rect.size.height));
CGFloat minX = rect.origin.x - padding.left;
CGFloat maxX = CGRectGetMaxX(rect) + padding.right;

View File

@@ -10,6 +10,8 @@
#import "ASAsciiArtBoxCreator.h"
#import <tgmath.h>
static const NSUInteger kDebugBoxPadding = 2;
typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
@@ -69,7 +71,7 @@ typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
for (NSString *child in children) {
NSMutableArray *lines = [[child componentsSeparatedByString:@"\n"] mutableCopy];
NSUInteger topPadding = ceilf((CGFloat)(lineCountPerChild - [lines count])/2.0);
NSUInteger topPadding = ceil((CGFloat)(lineCountPerChild - [lines count])/2.0);
NSUInteger bottomPadding = (lineCountPerChild - [lines count])/2.0;
NSUInteger lineLength = [lines[0] length];
@@ -98,7 +100,7 @@ typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
NSUInteger totalLineLength = [concatenatedLines[0] length];
if (totalLineLength < [parent length]) {
NSUInteger difference = [parent length] + (2 * kDebugBoxPadding) - totalLineLength;
NSUInteger leftPadding = ceilf((CGFloat)difference/2.0);
NSUInteger leftPadding = ceil((CGFloat)difference/2.0);
NSUInteger rightPadding = difference/2;
NSString *leftString = [@"|" debugbox_stringByAddingPadding:@" " count:leftPadding location:PIDebugBoxPaddingLocationEnd];
@@ -137,7 +139,7 @@ typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
if (maxChildLength < [parent length]) {
NSUInteger difference = [parent length] + (2 * kDebugBoxPadding) - maxChildLength;
leftPadding = ceilf((CGFloat)difference/2.0);
leftPadding = ceil((CGFloat)difference/2.0);
rightPadding = difference/2;
}
@@ -147,7 +149,7 @@ typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
for (NSString *child in children) {
NSMutableArray *lines = [[child componentsSeparatedByString:@"\n"] mutableCopy];
NSUInteger leftLinePadding = ceilf((CGFloat)(maxChildLength - [lines[0] length])/2.0);
NSUInteger leftLinePadding = ceil((CGFloat)(maxChildLength - [lines[0] length])/2.0);
NSUInteger rightLinePadding = (maxChildLength - [lines[0] length])/2.0;
for (NSString *line in lines) {
@@ -171,7 +173,7 @@ typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation)
NSUInteger totalLineLength = [boxStrings[0] length];
[boxStrings addObject:[NSString debugbox_stringWithString:@"-" repeatedCount:totalLineLength]];
NSUInteger leftPadding = ceilf(((CGFloat)(totalLineLength - [parent length]))/2.0);
NSUInteger leftPadding = ceil(((CGFloat)(totalLineLength - [parent length]))/2.0);
NSUInteger rightPadding = (totalLineLength - [parent length])/2;
NSString *topLine = [parent debugbox_stringByAddingPadding:@"-" count:leftPadding location:PIDebugBoxPaddingLocationFront];

View File

@@ -11,6 +11,7 @@
#import "ASRatioLayoutSpec.h"
#import <algorithm>
#import <tgmath.h>
#import <vector>
#import "ASAssert.h"
@@ -64,7 +65,7 @@
// Choose the size closest to the desired ratio.
const auto &bestSize = std::max_element(sizeOptions.begin(), sizeOptions.end(), [&](const CGSize &a, const CGSize &b){
return fabs((a.height / a.width) - _ratio) > fabs((b.height / b.width) - _ratio);
return std::fabs((a.height / a.width) - _ratio) > std::fabs((b.height / b.width) - _ratio);
});
// If there is no max size in *either* dimension, we can't apply the ratio, so just pass our size range through.

View File

@@ -10,6 +10,8 @@
#import "ASStackPositionedLayout.h"
#import <tgmath.h>
#import "ASInternalHelpers.h"
#import "ASLayoutSpecUtilities.h"
@@ -105,16 +107,16 @@ ASStackPositionedLayout ASStackPositionedLayout::compute(const ASStackUnposition
case ASStackLayoutJustifyContentStart:
return stackedLayout(style, 0, unpositionedLayout, constrainedSize);
case ASStackLayoutJustifyContentCenter:
return stackedLayout(style, floorf(violation / 2), unpositionedLayout, constrainedSize);
return stackedLayout(style, std::floor(violation / 2), unpositionedLayout, constrainedSize);
case ASStackLayoutJustifyContentEnd:
return stackedLayout(style, violation, unpositionedLayout, constrainedSize);
case ASStackLayoutJustifyContentSpaceBetween: {
const auto numOfSpacings = numOfItems - 1;
return stackedLayout(style, 0, floorf(violation / numOfSpacings), fmodf(violation, numOfSpacings), unpositionedLayout, constrainedSize);
return stackedLayout(style, 0, std::floor(violation / numOfSpacings), std::fmod(violation, numOfSpacings), unpositionedLayout, constrainedSize);
}
case ASStackLayoutJustifyContentSpaceAround: {
// Spacing between items are twice the spacing on the edges
CGFloat spacingUnit = floorf(violation / (numOfItems * 2));
CGFloat spacingUnit = std::floor(violation / (numOfItems * 2));
return stackedLayout(style, spacingUnit, spacingUnit * 2, 0, unpositionedLayout, constrainedSize);
}
}

View File

@@ -10,6 +10,7 @@
#import "ASStackUnpositionedLayout.h"
#import <tgmath.h>
#import <numeric>
#import "ASLayoutSpecUtilities.h"
@@ -87,7 +88,7 @@ static void stretchChildrenAlongCrossDimension(std::vector<ASStackUnpositionedIt
// restretch all stretchable children along the cross axis using the new min. set their max size to childCrossMax,
// not crossMax, so that if any of them would choose a larger size just because the min size increased (weird!)
// they are forced to choose the same width as all the other children.
if (alignItems == ASStackLayoutAlignItemsStretch && fabs(cross - childCrossMax) > 0.01) {
if (alignItems == ASStackLayoutAlignItemsStretch && std::fabs(cross - childCrossMax) > 0.01) {
l.layout = crossChildLayout(child, style, stack, stack, childCrossMax, childCrossMax);
}
}
@@ -182,7 +183,7 @@ static const CGFloat kViolationEpsilon = 0.01;
*/
static std::function<BOOL(const ASStackUnpositionedItem &)> isFlexibleInViolationDirection(const CGFloat violation)
{
if (fabs(violation) < kViolationEpsilon) {
if (std::fabs(violation) < kViolationEpsilon) {
return [](const ASStackUnpositionedItem &l) { return NO; };
} else if (violation > 0) {
return [](const ASStackUnpositionedItem &l) { return l.child.flexGrow; };
@@ -263,7 +264,7 @@ static void flexChildrenAlongStackDimension(std::vector<ASStackUnpositionedItem>
}
// Each flexible child along the direction of the violation is expanded or contracted equally
const CGFloat violationPerFlexChild = floorf(violation / flexibleChildren);
const CGFloat violationPerFlexChild = std::floor(violation / flexibleChildren);
// If the floor operation above left a remainder we may have a remainder after deducting the adjustments from all the
// contributions of the flexible children.
const CGFloat violationRemainder = violation - (violationPerFlexChild * flexibleChildren);

View File

@@ -32,8 +32,8 @@ extern void ASDisplayNodeSetupLayerContentsWithResizableImage(CALayer *layer, UI
UIEdgeInsets insets = [image capInsets];
// These are lifted from what UIImageView does by experimentation. Without these exact values, the stretching is slightly off.
const float halfPixelFudge = 0.49f;
const float otherPixelFudge = 0.02f;
const CGFloat halfPixelFudge = 0.49f;
const CGFloat otherPixelFudge = 0.02f;
// Convert to unit coordinates for the contentsCenter property.
CGRect contentsCenter = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
if (insets.left > 0 || insets.right > 0) {

View File

@@ -8,12 +8,15 @@
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "ASTextKitContext.h"
#import "ASTextKitFontSizeAdjuster.h"
#import "ASLayoutManager.h"
#import "ASTextKitFontSizeAdjuster.h"
#import <tgmath.h>
#import <mutex>
#import "ASTextKitContext.h"
#import "ASLayoutManager.h"
//#define LOG(...) NSLog(__VA_ARGS__)
#define LOG(...)
@@ -48,7 +51,7 @@
[attrString enumerateAttributesInRange:NSMakeRange(0, attrString.length) options:0 usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
if (attrs[NSFontAttributeName] != nil) {
UIFont *font = attrs[NSFontAttributeName];
font = [font fontWithSize:roundf(font.pointSize * scaleFactor)];
font = [font fontWithSize:std::round(font.pointSize * scaleFactor)];
[attrString removeAttribute:NSFontAttributeName range:range];
[attrString addAttribute:NSFontAttributeName value:font range:range];
}
@@ -166,7 +169,7 @@
// adjust here so we start at the proper place in our scale array if we have too many lines
scaleIndex++;
if (ceilf(longestWordSize.width * [scaleFactor floatValue]) <= _constrainedSize.width) {
if (std::ceil(longestWordSize.width * [scaleFactor floatValue]) <= _constrainedSize.width) {
// we fit! we are done
break;
}

View File

@@ -11,6 +11,7 @@
#import "ASTextKitRenderer+Positioning.h"
#import <CoreText/CoreText.h>
#import <tgmath.h>
#import "ASAssert.h"
@@ -163,7 +164,7 @@ static const CGFloat ASTextKitRendererTextCapHeightPadding = 1.3;
[self enumerateTextIndexesAtPosition:position usingBlock:^(NSUInteger characterIndex, CGRect glyphBoundingRect, BOOL *stop) {
CGPoint glyphLocation = CGPointMake(CGRectGetMidX(glyphBoundingRect), CGRectGetMidY(glyphBoundingRect));
CGFloat currentDistance = sqrtf(powf(position.x - glyphLocation.x, 2.f) + powf(position.y - glyphLocation.y, 2.f));
CGFloat currentDistance = std::sqrt(std::pow(position.x - glyphLocation.x, 2.f) + std::pow(position.y - glyphLocation.y, 2.f));
if (currentDistance < minimumGlyphDistance) {
minimumGlyphDistance = currentDistance;
minimumGlyphCharacterIndex = characterIndex;

View File

@@ -10,6 +10,8 @@
#import "ASTextKitShadower.h"
#import <tgmath.h>
static inline CGSize _insetSize(CGSize size, UIEdgeInsets insets)
{
return UIEdgeInsetsInsetRect({.size = size}, insets).size;
@@ -87,11 +89,11 @@ static inline UIEdgeInsets _invertInsets(UIEdgeInsets insets)
// min values are expected to be negative for most typical shadowOffset and
// blurRadius settings:
shadowPadding.top = fminf(0.0f, _shadowOffset.height - _shadowRadius);
shadowPadding.left = fminf(0.0f, _shadowOffset.width - _shadowRadius);
shadowPadding.top = std::fmin(0.0f, _shadowOffset.height - _shadowRadius);
shadowPadding.left = std::fmin(0.0f, _shadowOffset.width - _shadowRadius);
shadowPadding.bottom = fminf(0.0f, -_shadowOffset.height - _shadowRadius);
shadowPadding.right = fminf(0.0f, -_shadowOffset.width - _shadowRadius);
shadowPadding.bottom = std::fmin(0.0f, -_shadowOffset.height - _shadowRadius);
shadowPadding.right = std::fmin(0.0f, -_shadowOffset.width - _shadowRadius);
_calculatedShadowPadding = shadowPadding;
}